本地环境:macos+xampp+php7
创建了 2 个文件 demo.php and demo-form.php ,放在 xampp/htdocs/simpleform 目录下
demo.php 的代码为:
<?php
/**
* Created by PhpStorm.
* User: sulayman
* Date: 2019/7/8
* Time: 8:37 PM
*/
define('DB_Name', 'form1');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$link){
die('could not connect:'.mysqli_error($link));
}
$db_selected = mysqli_select_db($link, DB_Name);
if(!$db_selected){
die('cannot use'.DB_Name.':'.mysqli_error($link));
}
//echo "you conneted the database successfully";
$value = $_POST["input1"];
if (isset($_POST['input1'])) {
$value = $_POST['input1'];
}
$sql = "INSERT INTO demo(input1) VALUES ('$value')";
if (!mysqli_query($link,$sql)){
die('error:'.mysqli_error());
}
mysqli_close($link);
demo-form.php 的代码为:
<form action = "demo.php" method = "post" >
<p> Input 1: </p>
<input type="text" name="input1">
<input type="submit" value="submit">
</form>
phpmyadmin 的配置
https://i.ibb.co/bdHsq9R/2019-07-09-12-43-43.png
如何解决这 2 个错误:
Notice: Undefined variable: value in /Applications/XAMPP/xamppfiles/htdocs/simpleform/demo.php on line 36
Warning: mysqli_error() expects parameter 1 to be mysqli, string given in /Applications/XAMPP/xamppfiles/htdocs/simpleform/demo.php on line 40 error:
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.