php mysql;将数据写入数据库

时间:2012-01-13 20:32:32

标签: php mysql forms insert

在向数据库添加一些数据时遇到一些麻烦。我有一个文件new_entry.php这是一个表单,它发布了添加到insert_new.php的数据。

每次填写并提交数据时,数据都没有进入数据库,并显示错误消息“无法将数据添加到表格中”出现了什么想法?

NEW_ENTRY.PHP

<body>
 <form method="post" action="insert_new.php"><!-- form sent to insert_new.php-->
  Section: <input type="text" name="section"/><br />
  Food: <input type="text" name="food"/><br />
  Description: <input type="text" name="description"/><br />
  Price: <input type="text" name="price"/><br />
  <br />
  <input type="submit" value="submit"/>
</form>
</body>

INSERT_NEW.PHP

 <?php 
include 'library/connect.php';//connect to databse
$section = $_REQUEST["section"]; // get data from the HTML form on new student form
$food = $_REQUEST["food"];
$description = $_REQUEST["description"];
$price = $_REQUEST["price"];

mysql_query ("INSERT INTO food_menu (section, food, description, price) VALUES ('$section', '$food', '$description', $price)")/* insert the data to the food_menu table*/
    or die ("Could not add the data to table");//error message

header('Location:index.php');//auto redirect to view page
include 'library/closedb.php';
?>

2 个答案:

答案 0 :(得分:0)

第一:不要这样做。你真的需要研究SQL注入,否则你会非常抱歉。

其次,你的价格没有数字验证(假设它进入了一个数字列)......这也很糟糕......如果有人投入美元符号或什么东西会怎么样?

接下来,请发布您的表定义和连接代码(而不是连接值)。

如果您执行以下操作,也可以获得更多反馈:

or die (mysql_error());//error message

答案 1 :(得分:0)

您的MySQL查询结尾处似乎有一个错误,靠近价格

请用现有代码替换下面的代码:

mysql_query ("INSERT INTO food_menu (section, food, description, price) VALUES ('$section', '$food', '$description', '$price')")

请告诉我结果。