以下是连接到mysql数据库的简单表单的代码
<html>
<head>
<title>CS90SI Project 2</title>
<script src="validation.js"></script>
</head>
<body>
<?php if($_SERVER["REQUEST_METHOD"] != "POST"){ ?>
<h1>My Favorite Foods</h1>
<form action="index.php" method="post" id="foodForm">
Name: <input type="text" name="foodname" id="nameField"></input><br />
Type: <select name="foodtype" id="typeField">
<option value="fruit">Fruit</option>
<option value="vegetable">Vegetable</option>
<option value="dairy">Dairy</option>
<option value="meat">Meat</option>
<option value="grain">Grain</option>
<option value="other">Other</option>
</select><br />
Number of Calories: <input type="text" name="foodcals" id="calsField"></input><br />
Healthy? <input type="checkbox" name="foodhealth" value="healthy" id="healthyField"></input><br />
<?php if(isset($_POST['foodhealth'])) echo "checked='checked'"; ?>
Additional Notes:<br />
<textarea name="foodnotes" id="notesField"></textarea><br />
<input type="submit" value="Add" onclick="return validateForm();"></input>
</form>
<?php }else{ ?>
<!-- form handling and output printing stuff goes here -->
<?php
if(isset($_POST['foodhealth'])) {
$foodhealth = true;
} else {
$foodhealth = false;
}
$insert = "INSERT INTO Foods(Name, Type, NumCals, Healthy, Notes) VALUES('$_POST[foodname]','$_POST[foodtype]','$_POST[foodcals]','$_POST[foodhealth]','$_POST[foodnotes]')";
$con = mysqli_connect("localhost","root");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con, "mydb");
$result = mysqli_query($con, $insert);
echo "Food added successfully.";
while ($row = mysqli_fetch_array($result)) {
echo $row['Name'] . ", " . $row['Type'] . ", " . $row['NumCals'] . ", " . $row['Healthy'] . ", " . $row['Notes'];
echo "<br>";
}
mysqli_free_result($result);
mysqli_close($con);
} ?>
</body>
</html>
我提交数据并收到以下错误:
Notice: Undefined index: foodhealth in /Applications/XAMPP/xamppfiles/htdocs/vsingal5_hw2/index.php on line 38
Food added successfully.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /Applications/XAMPP/xamppfiles/htdocs/vsingal5_hw2/index.php on line 47
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in /Applications/XAMPP/xamppfiles/htdocs/vsingal5_hw2/index.php on line 51
不确定为什么会发生这种情况,为复选框设置bool是否有问题,以及mysqli_fetch_array有什么问题?
答案 0 :(得分:0)
如果用户未选中Healthy?
框,则不会设置$_POST['foodhealth']
。您已在if (isset($_POST['foodhealth']))
代码中检查了该内容。但是,在您的INSERT
声明中,无论如何都要尝试使用$_POST['foodhealth']
。您应该使用之前设置的$foodhealth
变量。
然后在致电mysqli_query()
后,您需要检查它是否成功。
if ($result) {
echo 'Food added successfully'
} else {
echo mysqli_error($con);
}
第三,当查询是INSERT时,你不能调用mysqli_fetch_array()
,它不会返回任何行。您只能在执行SELECT时获取。
答案 1 :(得分:0)
你在这里缺少一堆引号:
VALUES('$_POST[foodname]','$_POST[foodtype]','$_POST[foodcals]','$_POST[foodhealth]','$_POST[foodnotes]')";
他们应该是:
VALUES('{$_POST['foodname']}','{$_POST['foodtype']}','{$_POST['foodcals']}','{$_POST['foodhealth']}','{$_POST['foodnotes']}')";
将变量括在{}