简体   繁体   中英

why is this not updating the MySQL database?

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&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>
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.

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

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

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.

have you tried to commit the transaction?

http://www.php.net/manual/en/mysqli.commit.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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