我正在做简单的表单,在数据库表中插入值,这是代码
new_subject,php
<form action="create_subject.php" method="post">
<p>Menu name:
<input type="text" name="menu_name" value="" />
<p>Position:
<select name="position">
<?php
$subject_set= find_all_subjects();
$subject_count=mysqli_num_rows($subject_set);
for($count=1; $count<=$subject_count+1; $count++){
echo "<option value='{$count}' >{$count}</option>";
}
?>
</select>
</p>
<p>Visible
<input type="radio" value="0" name="visible" />No
<input type="radio" value="1" name="visible" />Yes
</p>
<input type="submit" value="Submit" name="submit" />
<br/> <br/>
<a href="manage_content.php">Cancel</a>
</form>
create_subject.php
<?php
if(isset($_POST['submit'])){
$menu_name= $_POST["menu_name"];
$position = (int) $_POST["position"];
$visible= (int)$_POST["visible"];
$query="INSERT INTO subjects (menu_name , position , visible) VALUES($menu_name, $position, $visible)";
$result= mysqli_query($connection, $query);
if($result){
header("Location : manage_content.php");
exit();
}
else {
echo 'There was some problem ' ;
echo mysqli_error($connection);
}
}
else {
header('Location: new_subject.php');
exit();
}
?>
现在这是错误 有一些问题未知字段'name'// name是我在文本框中输入的值。有人请帮我解决这个问题。如果我没有输入文本框的值,则其他值已成功输入数据库
答案 0 :(得分:3)
作为使易于阅读和维护的sql语句的提示使用sprintf():
$query= sprintf("INSERT INTO subjects (menu_name , position , visible) VALUES('%s', '%s', '%s')",$menu_name, $position, $visible);
或
$sql = "INSERT INTO subjects (menu_name , position , visible) VALUES('%s', '%s', '%s')";
$query = sprintf($sql, $menu_name, $position, $visible);
答案 1 :(得分:2)
将单引号括在它将解决问题的值
$query="INSERT INTO subjects (menu_name , position , visible) VALUES('$menu_name', '$position', '$visible')";