简体   繁体   English

php mysql; 将数据写入数据库

[英]php mysql; writing data to database

having a bit of trouble adding some data to a database. 将一些数据添加到数据库时遇到麻烦。 I have the file new_entry.php which is a form, which posts the data added to insert_new.php. 我有一个文件new_entry.php,它是一种表单,用于发布添加到insert_new.php的数据。

Every time the fields are filled in and submitted the data does not go to the database with the error message "Could not add the data to table" appearing..any ideas? 每次填写并提交字段时,数据都不会进入数据库,并显示错误消息“无法将数据添加到表中”。

NEW_ENTRY.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 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';
?>

First: Don't do this. 第一:不要这样做。 You really need to research SQL Injection or you will be very sorry. 您确实需要研究SQL注入,否则将非常抱歉。

Secondly, your price has no numeric validation (assuming it's going into a numeric column)... this is also bad... what if someone put in a dollar sign or something? 其次,您的价格没有数字验证(假设它进入数字列)...这也很糟糕...如果有人放入美元符号之类的东西怎么办?

Next, please post your table definition and connection code (not the connection values). 接下来,请发布您的表定义和连接代码(而不是连接值)。

You can also get more feedback if you do something like: 如果您执行以下操作,则还可以获得更多反馈:

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

It seems that you have a mistake at the end of your MySQL query near price . 价格接近MySQL的查询结尾处,您似乎有一个错误。

Please replace the code below with existing line: 请使用现有的行替换下面的代码:

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

Tell me the result please. 请告诉我结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM