Hi,

I have created a mysql database containing menu dishes for a restaurant.

The idea is to store all dishes on the database whether the dish is being sold or has been sold in the past. Each dish (database row) has a simple indicator (yes/no) to determine if the row should be displayed for the current menu.

My issue is that when the chef wants to change the menu they will have to change the indicator on the stored row from off to on and vice versa.

To help with this I have built a simple query and form to present the data to the chef, however the form is mainly constructed of PHP variables resulting from the query.

i.e.

$query = "SELECT * FROM menu";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
		$id = $row['id'];
		$type = $row['type'];
		$item = $row['item'];
		$description = $row['description'];
		$price = $row['price'];
		$addmenu = $row['addmenu'];
		$string = 'yes';

if (strstr($addmenu,$string)) {
echo "<tr><td>".$id."</td><td>".$type."</td><td>".$item."</td><td>".$description."</td><td>".$price."</td><td><input type=checkbox  checked=checked name=".$id."</td></tr>";
}
else
{
echo "<tr><td>".$id."</td><td>".$type."</td><td>".$item."</td><td>".$description."</td><td>".$price."</td><td><input type=checkbox name=".$id."</td></tr>";
}}

This outputs a nice display showing the status of each database row.

my intention is for the chef to change the value of the checkboxes and repost the form so I can update the database with some very simple sql.

However, as each checkbox name is a php variable, I do not know how to pass the real value to process.

Also, I cannot predetermine the checkbox name as the user can add new rows to the database as chef creates new dishes.

Lastly, if it is possible to pass the php variable as a value for processing, i'm guessing I will have to create some sort of loop to identify each instance of of the variable/value.

Thanks for your time.

Dani AI

Generated

— your idea of generating a checkbox per dish is fine; the usual, reliable pattern is to give each checkbox a predictable name (use an array-style name so PHP returns an associative array keyed by the dish id) and then update the database from that array on submit. As noted, iterating the submitted data is the right direction, and is correct that form fields arrive as name=>value pairs in $_POST.

A compact, safe workflow:

  • Render checkboxes as name="menu[<id>]" with a value like 1. (Unchecked boxes simply won’t appear in $_POST['menu'].)
  • On submit, sanitize IDs, start a transaction, reset the menu flags, then set the flagged IDs to on with a single parameterized UPDATE using WHERE id IN (...). This keeps SQL injection risk low and uses few queries.

Example server-side snippet (uses PDO and prepared statements):

/* $pdo is a PDO instance */
$posted = isset($_POST['menu']) ? $_POST['menu'] : [];
$ids = array_map('intval', array_keys($posted));

$pdo->beginTransaction();
$pdo->exec("UPDATE menu SET addmenu = 0");
if (!empty($ids)) {
  $placeholders = implode(',', array_fill(0, count($ids), '?'));
  $stmt = $pdo->prepare("UPDATE menu SET addmenu = 1 WHERE id IN ($placeholders)");
  $stmt->execute($ids);
}
$pdo->commit();

Notes and cautions: prefer a boolean/tinyint column instead of text like "yes"/"no"; always cast IDs (intval) and use prepared statements; consider a CSRF token and permission checks for the chef UI; avoid the deprecated mysql_* extension. For details on form handling and safe DB updates see the PHP forms guide and PDO prepared-statement docs: PHP forms tutorial and PDO prepared statements.

Recommended Answers

All 3 Replies

for each through the $_POST $_GET $_REQUEST arrays (however the form is sent)

the form will have valid name=>value pairs in its submitted data, that will match rows in the database, regardless of how they are generated

<?php
echo "<tr><td>".$id."</td><td>".$type."</td>
<td>".$item."</td><td>".$description."</td>
<td>".$price."</td>
<td><input type=checkbox name=".$id." value='serving'></td></tr>";
/* when processing form input
update table set serving="" */
foreach ( $_POST as $menuitem => $serving ) {/* do something with the sql table, 
probably/possibly 
update table set set serving='serving' where menuitem=$menuitem
Menu print is select * where serving='serving' (everything in the submitted form unchecked boxes dont submit) 
print this select on a menu */ }
?>

then future printings will be as the chef set, till the chef changes it again

Member Avatar for Member #334542

Almost form values are passed as variable with the name of your element and as almost said you can pick up that in another page using $_POST or $_GET.

Many thanks,

This is a fantastic function and so easy to work with.

Apart from the PHP manual (which I find a little difficult as I do not understand all of the terminology), can you suggest any good reading for PHP. I really think it's worth me investing in a good reference book.

Again many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.