简体   繁体   中英

PHP update is not updating database with checkbox

to all i have a weird problem when i am trying to update a table of my database with checkboxes it takes only one value and all the rest just ingnores them here is my php code so far

foreach ($_POST['choice'] as $id){
    $price=$_POST['price'][$id];
    $availability=$_POST['availability'][$id];

    $result1=mysql_query("UPDATE store SET storeid='".$id."',availability='".$availability."', price='".$price."'  WHERE productid='".$par1."'");
}

Yes you are right and i am deeply sorry for the lack of information.Here is my html code as well

echo "<td><input type='text' id='availability[".$row->id ."]' name='availability[".$row->id ."]'  value='".$row->diathesimotita ."' size='20'/></td>";
echo "<td><input type='text' id='price[".$row->id ."]' name='price[".$row->id ."]'  value='".$row->price ."'  size='10'/></td>";
echo "<td><input type='checkbox' id='choice[".$row->id ."]' name='choice[".$row->id ."]'  value='".$row->id ."'   /></td>";
echo"</tr>";

So i am trying to take the textbox values which are in the same row with the check box but it gets only the first checked ckeckbox

Checkboxes only post a value when they are checked. You should validate them or set a value first before trying to use them in a query (for security if nothing else!). For example do this at the top of your processing code:

$checkbox_val = (!empty($_POST['choice']) ? 'Yes' : 'No');  //example only assuming non-array for 'choice'

This will set the value to 'No' if the POST value is empty or not set, guaranteeing that you always have a value. You can change the values to 0/1, true/false, etc.

Also, without seeing what is in your 3 $_POST arrays, it is impossible for us to tell you if you are getting the correct values for $price etc.

The PHP code looks fine; we need to see the HTML code, but anyway try this in SQL:

UPDATE store SET storeid='".$id."' , availability='".$availability."' , price='".$price."'     WHERE productid='".$par1."'" );

Spaces really matter in SQL. If this does not work please show us the HTML code.

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