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