为什么这不更新 MySQL 数据库?

发布于 2025-01-06 01:41:57 字数 1940 浏览 1 评论 0原文

由于某种原因,这还没有插入到数据库中。我很困惑。它运行良好。我不知道出了什么问题。

它应该更新名为 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&nbsp;|&nbsp; 
Vitamin K (mcg)&nbsp;|&nbsp;
Fiber (g)&nbsp;|&nbsp;
Calories&nbsp;|&nbsp;
Protein (g)&nbsp;|&nbsp;
Carbs &nbsp;|&nbsp;
Sugar (g)&nbsp;|&nbsp;
Sodium (mg)&nbsp;|&nbsp;
Cholesterol &nbsp;|&nbsp;
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空城之時有危險 2025-01-13 01:41:57
INSERT INTO users_food(col1, col2) VALUES (value1, value2)

不要搞乱自动增量字段。让数据库来处理它。并使用您想要插入的字段...并

or die(mysql_error())

在您的 sql 查询中使用。

另外在 phpmyadmin 中运行查询并查看它是否插入。

INSERT INTO users_food(col1, col2) VALUES (value1, value2)

don't mess with autoincrement fields. Let the database handle it. And use the fields which you want to insert.... And use

or die(mysql_error())

in your sql query.

Plus run the query in phpmyadmin and see it inserts or not.

凉城 2025-01-13 01:41:57

row["id"] 不在 post 数组中。 post 数组仅包含数字(id)。

row["id"] isn't in the post array. the post array only contains numbers (the ids).

无风消散 2025-01-13 01:41:57

在您进行任何进一步的编码之前,我强烈建议您研究一些有关 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.

入画浅相思 2025-01-13 01:41:57

您是否尝试过提交交易?

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

听风吹 2025-01-13 01:41:57

SQL UPDATE 语法

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

SQL UPDATE 示例

UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'

来源: http://www.w3schools.com/sql/sql_update.asp

SQL UPDATE Syntax

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

SQL UPDATE Example

UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'

credit : http://www.w3schools.com/sql/sql_update.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文