为什么这不更新 MySQL 数据库?
由于某种原因,这还没有插入到数据库中。我很困惑。它运行良好。我不知道出了什么问题。
它应该更新名为 Vitamink 的数据库中名为 user_foods 的表,但事实并非如此。连接正确。那不是问题。
<?php include 'top.php'; ?>
</head>
<div id="container">
<?php include 'header.php'; ?>
<?php include 'nav.php'; ?>
<div id="content-container">
<div id="content_for_site">
<h2>Find A Food</h2> <br />
<?php
$query = "SELECT
`id`, `user_submitted_id`, `approved`, `name`, `source`, `vit_k`, `cal`,`protein`, `fiber`, `carbs`, `sugar`, `sodium`, `chol`
FROM `foods`
";
$query_run = mysql_query($query);
?>
<br /><br />
Food |
Vitamin K (mcg) |
Fiber (g) |
Calories |
Protein (g) |
Carbs |
Sugar (g) |
Sodium (mg) |
Cholesterol |
Source
<br />
<form action="find-a-meal.php" method="POST">
<?php
while ($row = mysql_fetch_assoc($query_run)){
?>
<input type="checkbox" name="<?php
echo $row["id"];
?>"<?php
echo ' value="'.$row["id"].'" />'
.$row["name"]." | ".
$row["vit_k"]." | ".
$row["fiber"]." | ".
$row["cal"]." | ".
$row["protein"]." | ".
$row["carbs"]." | ".
$row["sugar"]." | ".
$row["sodium"]." | ".
$row["chol"]." | ".
$row["source"].
'<br />';
}
?>
<br /><br />
<input type="submit" value="Add Food to my Meal Planner Queue">
</form>
<?php
// send the foods to the mysql database
if (isset($_POST[$row["id"]]) && !empty($_POST[$row["id"]])){
$id = $_POST[$row["id"]];
$query = "INSERT INTO `users_foods` VALUES('','".$_SESSION['user_id']."','".$id."','','','','')";
$query_run = mysql_query($query);
}
?>
</div>
for some reason this hasn't been inserting into the database. I'm puzzled. It runs fine. I have no idea what's wrong.
It should be updating a table called user_foods in a database called vitamink, but it isn't. It's connected properly. That's not the problem.
<?php include 'top.php'; ?>
</head>
<div id="container">
<?php include 'header.php'; ?>
<?php include 'nav.php'; ?>
<div id="content-container">
<div id="content_for_site">
<h2>Find A Food</h2> <br />
<?php
$query = "SELECT
`id`, `user_submitted_id`, `approved`, `name`, `source`, `vit_k`, `cal`,`protein`, `fiber`, `carbs`, `sugar`, `sodium`, `chol`
FROM `foods`
";
$query_run = mysql_query($query);
?>
<br /><br />
Food |
Vitamin K (mcg) |
Fiber (g) |
Calories |
Protein (g) |
Carbs |
Sugar (g) |
Sodium (mg) |
Cholesterol |
Source
<br />
<form action="find-a-meal.php" method="POST">
<?php
while ($row = mysql_fetch_assoc($query_run)){
?>
<input type="checkbox" name="<?php
echo $row["id"];
?>"<?php
echo ' value="'.$row["id"].'" />'
.$row["name"]." | ".
$row["vit_k"]." | ".
$row["fiber"]." | ".
$row["cal"]." | ".
$row["protein"]." | ".
$row["carbs"]." | ".
$row["sugar"]." | ".
$row["sodium"]." | ".
$row["chol"]." | ".
$row["source"].
'<br />';
}
?>
<br /><br />
<input type="submit" value="Add Food to my Meal Planner Queue">
</form>
<?php
// send the foods to the mysql database
if (isset($_POST[$row["id"]]) && !empty($_POST[$row["id"]])){
$id = $_POST[$row["id"]];
$query = "INSERT INTO `users_foods` VALUES('','".$_SESSION['user_id']."','".$id."','','','','')";
$query_run = mysql_query($query);
}
?>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要搞乱自动增量字段。让数据库来处理它。并使用您想要插入的字段...并
在您的 sql 查询中使用。
另外在 phpmyadmin 中运行查询并查看它是否插入。
don't mess with autoincrement fields. Let the database handle it. And use the fields which you want to insert.... And use
in your sql query.
Plus run the query in phpmyadmin and see it inserts or not.
row["id"] 不在 post 数组中。 post 数组仅包含数字(id)。
row["id"] isn't in the post array. the post array only contains numbers (the ids).
在您进行任何进一步的编码之前,我强烈建议您研究一些有关 mysqli 和 SQL 注入预防的信息。希望这能帮助您编写安全的网络应用程序。
Before you do any further coding, I would highly recommend you to research a bit about mysqli and SQL Injection prevention. Hopefully this will help you code a secure webapp.
您是否尝试过提交交易?
http://www.php.net/manual/en/mysqli.commit.php
have you tried to commit the transaction?
http://www.php.net/manual/en/mysqli.commit.php
SQL UPDATE 语法
SQL UPDATE 示例
来源: http://www.w3schools.com/sql/sql_update.asp
SQL UPDATE Syntax
SQL UPDATE Example
credit : http://www.w3schools.com/sql/sql_update.asp