Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using an update SQL statement to update the MYSQL database, however, each button seems to be bringing back the same ID (The first ID of the first item displayed on the page) I have echoed each MealID and each is correct, so there must be something wrong somewhere that isn't getting different IDs for each button.

See my code below. Can anyone see the problem here?

-Loop to display items with buttons.

What I have tried:

-Loop to display items with buttons.
while($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC) and ($cnt < $max)) 
{
  echo "".$dbRow["Name"]."".$dbRow['Picture']."' width='150' height='150'/>"."<br><br>".$dbRow["Instructions"]."

<form method='POST'><input type='hidden' name='MealID' value='".$dbRow['MealID']."'><input type='submit' name='submit' value='Complete'>";


//UPDATE SQL STATEMENT - (getting SAME value for each button)
$value = $_POST['MealID'];
//var_dump($value);

if(($_POST['MealID'])) {
$dbQuery=$conn->prepare("UPDATE Users SET Display = 1 WHERE 
UserID=:UserID AND MealID=$value"); 
$dbParams = array(':UserID'=>$UserID);
$dbQuery->execute($dbParams);
}
Posted
Updated 4-Apr-18 1:05am
v3

1 solution

Everytime you submit your Form, all inputs will be send. Now, you have probably multiple by the same name: e.g. 'MealID'. So, whenever you ask
PHP
$value = $_POST['MealID'];
you will get the first entry.

Make sure that the Names of the inputs are different, or that you create a separate Form per row. For example:
HTML
<form method='POST'><input type='hidden' name='MealID' value='Meal1'><input type='submit' name='submit' value='Complete'></form>
<form method='POST'><input type='hidden' name='MealID' value='Meal2'><input type='submit' name='submit' value='Complete'></form>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900