Jump to content

checkboxes, and retrieving data and updating rows


anser316

Recommended Posts

Hi, im having a problem with my code. I am trying to build an ordering system, where it shows the products that need to be ordered, which i have done. It should allow you to tick the products you want to order, which i have also done. The rows selected should then send to another form, and show its details. On this form it should also change one of the the attributes in the table called 'status' from NO STATUS to ORDERED.

 

The problem i am having, is that on the second page, it only shows the drug_id, not the others, i think its because its the value of the checked box. Hence, im also having difficulty with looping an update statement, but il never get that to work until i get the first part right.

 

This code is for the first form - order

 

$result2 =mysql_query("SELECT i.drug_id,i.branch_id, d.drug_name,i.total_stock,i.reorder_level, s.supplier_name
FROM drugs d, supplier s, branch_items i,branch b
WHERE d.supplier_id=s.supplier_id
AND d.drug_id=i.drug_id
AND b.branch_id=i.branch_id
AND i.total_stock<=i.reorder_level
ORDER BY total_stock");

echo "<BR><br>";
echo "<b>Minimum Stock Level Met - Reorder Needed</b><p>";
echo "<form action= conorder.php method=POST>";
echo "<table border='1'>";
echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>SUPPLIER</th></tr>";
while($row = mysql_fetch_array( $result2 )) {
echo "<tr><td>"; 
echo $row['drug_id'];
echo "</td><td>";
echo $row['branch_id'];
echo "</td><td>";
echo $row['drug_name'];
echo "</td><td>";
echo $row['total_stock'];
echo "</td><td>";
echo $row['reorder_level'];
echo "</td><td>";
echo $row['supplier_name'];
echo "</td><td>";
echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>";
echo "</td></tr>"; 
}
echo "</table>";
echo "<input type =submit value= Submit>";
echo "<input type = reset>";
echo "</form>";

 

This code is for the second form, that retrieves the data - conorder

if (isset($_POST['ticked'])){
echo "chosen<br>";
echo "<table border='1'>";
echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th> <th>BRANCH</th><th>Reorder Level</th><th>Reorder Quantity</th><th>Total Stock</th><th>Status</th></tr>";
foreach ($_POST['ticked'] as $drug_id){

echo "<tr><td>"; 
echo $drug_id;
echo "</td><td>";
echo "$_POST[branch_id]";
echo "</td><td>";
echo $_POST[drug_name];
echo "</td><td>";
echo $_POST[$branch_details];
echo "</td><td>";
echo $_POST[$reorder_level];
echo "</td><td>";
echo $_POST[$reorder_quantity];
echo "</td><td>";
echo $_POST[$total_stock];
echo "</td><td>";
echo $_POST[$status];
echo "</td></tr>"; 
}
}


//$size_array=count($_POST[drug_id]);
//echo "$size_array";

if (isset($_POST['ticked'])){
foreach ($_POST['ticked'] as $drug_id){
$result =mysql_query("UPDATE branch_items b SET
status =ORDERED
WHERE drug_id=".$_POST [drug_id]."
AND branch_id=".$_POST [branch_id]."");
}}

echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>";

 

Should be

echo "<input type=checkbox name=ticked[] value='{$row[drug_id]}'>";

And I don't get in part 2, did you mean to set half of the array id's called by variables (like echo $_POST[$status];) because for one, $status hasn't been set, and for two, nothing of name 'status' was POSTed by the previous form :x

Right, well gotcha.

 

echo "<BR><br>";
echo "<b>Minimum Stock Level Met - Reorder Needed</b><p>";
echo "<form action= conorder.php method=POST>";
echo "<table border='1'>";
<?php

// ...

echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>SUPPLIER</th></tr>";
while($row = mysql_fetch_array( $result2 )) {
echo "<tr><td>"; 
echo $row['drug_id'];
echo "</td><td>";
echo $row['branch_id'];
echo "</td><td>";
echo $row['drug_name'];
echo "</td><td>";
echo $row['total_stock'];
echo "</td><td>";
echo $row['reorder_level'];
echo "</td><td>";
echo $row['supplier_name'];
echo "</td><td>";
echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>";
echo "</td></tr>"; 
}
echo "</table>";
echo "<input type =submit value= Submit>";
echo "<input type = reset>";
echo "</form>";

// ...

?>

 

echo "<tr><td>";

echo $row['drug_id'];

echo "</td><td>";

 

Thing is, all you are doing is taking the values from the database and putting them into a table. To actually post them values to another page, they need to be in input boxes too, e.g.

 

echo "<input type='hidden' name='drug_id' value= '$row[drug_id]' />$row['drug_id']";

or

echo "<input type='text' name='drug_id' value= '$row[drug_id]' DISABLED />";

 

it should work then.

sorry i dont really follow

 

what is this for: "/>$row['drug_id']""

 

i put this in

echo "<input type='hidden' name='drug_id' value= '$row[drug_id]'

echo "<input type='hidden' name='branch_id' value= '$row[branch_id]'

 

instead of

echo $row['drug_id'];

echo $row['branch_id'];

 

am i supposed to put them at the end after creating checkbox, because the values disappeared.

sorry i dont really follow

 

what is this for: "/>$row['drug_id']""

 

i put this in

echo "<input type='hidden' name='drug_id' value= '$row[drug_id]'

echo "<input type='hidden' name='branch_id' value= '$row[branch_id]'

 

instead of

echo $row['drug_id'];

echo $row['branch_id'];

 

am i supposed to put them at the end after creating checkbox, because the values disappeared.

 

inputs that are 'hidden' aren't displayed on the page, but are posted when you submit the form.

echo "<input type='hidden' name='drug_id' value= '$row[drug_id]' />$row['drug_id']";

That creates the hidden input, then shows the value in the table after the hidden input.

The hidden input is only for the purpose of transferring the value to the page when the form is submitted (via POST), it then displays the value on the page after the hidden input.

 

Here, this should make sense.

 

 

<?php
// ...
$result2 =mysql_query("SELECT i.drug_id,i.branch_id, d.drug_name,i.total_stock,i.reorder_level, s.supplier_name
FROM drugs d, supplier s, branch_items i,branch b
WHERE d.supplier_id=s.supplier_id
AND d.drug_id=i.drug_id
AND b.branch_id=i.branch_id
AND i.total_stock<=i.reorder_level
ORDER BY total_stock");

echo "<BR><br>";
echo "<b>Minimum Stock Level Met - Reorder Needed</b><p>";
echo "<form action='conorder.php' method='POST'>";
echo "<table border='1'>";
echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>SUPPLIER</th></tr>";
while($row = mysql_fetch_array( $result2 )) {
echo "<tr><td>";
echo "<input type='hidden' name=drug_id[] value='$row[drug_id]'>";
echo $row['drug_id'];
echo "</td><td>";
echo "<input type='hidden' name=branch_id[] value='$row[branch_id]'>";
echo $row['branch_id'];
echo "</td><td>";
echo "<input type='hidden' name=drug_name[] value='$row[drug_name]'>";
echo $row['drug_name'];
echo "</td><td>";
echo "<input type='hidden' name=total_stock[] value='$row[total_stock]'>";
echo $row['total_stock'];
echo "</td><td>";
echo "<input type='hidden' name=reorder_level[] value='$row[reorder_level]'>";
echo $row['reorder_level'];
echo "</td><td>";
echo "<input type='hidden' name=supplier_name[] value='$row[supplier_name]'>";
echo $row['supplier_name'];
echo "</td><td>";
echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>";
echo "</td></tr>"; 
}
echo "</table>";
echo "<input type='submit' value='Submit'>";
echo "<input type='reset'>";
echo "</form>";
// ...
?>

 

And step 2:

<?php
if (isset($_POST['ticked'])){
$i = 0;
echo "chosen<br>";
echo "<table border='1'>";
echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th> <th>BRANCH</th><th>Reorder Level</th><th>Reorder Quantity</th><th>Total Stock</th><th>Status</th></tr>";
foreach ($_POST['ticked'] as $drug_id){

echo "<tr><td>"; 
echo $drug_id;
echo "</td><td>";
echo "$_POST[branch_id][$i]";
echo "</td><td>";
echo $_POST[drug_name][$i];
echo "</td><td>";
// I haven't changed anything past this point, because I don't understand it.
echo $_POST[$branch_details];
echo "</td><td>";
echo $_POST[$reorder_level];
echo "</td><td>";
echo $_POST[$reorder_quantity];
echo "</td><td>";
echo $_POST[$total_stock];
echo "</td><td>";
echo $_POST[$status][$i];
echo "</td></tr>";
$i++;
}
}


//$size_array=count($_POST[drug_id]);
//echo "$size_array";

if (isset($_POST['ticked'])){
foreach ($_POST['ticked'] as $drug_id){
$result =mysql_query("UPDATE branch_items b SET
status =ORDERED
WHERE drug_id=".$_POST [drug_id]."
AND branch_id=".$_POST [branch_id]."");
}}
?>

 

 

 

 

Yep, the data shows now thanks a lot., however, the result data shows the same for all the rows, apart from drug_id

 

DRUGID    ID      DRUG NAME  STOC    RE   SUPPLIER

5     2     sampledrug7   4     4   Unichem

7     2     sampledrug7   4     4   Unichem

 

In fact it shows the details for the second key for the first one.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.