0

这是连接到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

不知道为什么会发生这种情况,为复选框设置的布尔是否有问题,mysqli_fetch_array 有什么问题?

4

2 回答 2

0

您在这里缺少一堆引号:

 VALUES('$_POST[foodname]','$_POST[foodtype]','$_POST[foodcals]','$_POST[foodhealth]','$_POST[foodnotes]')";

他们应该是:

 VALUES('{$_POST['foodname']}','{$_POST['foodtype']}','{$_POST['foodcals']}','{$_POST['foodhealth']}','{$_POST['foodnotes']}')";

将变量包含在{}

于 2013-10-06T02:09:47.263 回答
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 时获取。

于 2013-10-06T01:54:03.630 回答