Note that there are some explanatory texts on larger screens.

plurals
  1. POupdate database by text box based on the checked box's
    primarykey
    data
    text
    <p>**Hi i have been working on this php code . </p> <p>I want to update the quantity that belongs to the checked box . but the exact problem with my code is :if I have for example 3 checkboxs and when I enter the quantity for them all in the same time the updates are done correctly and if I only update the first quantity with out the other 2 the update is done correctly also ,But when I update the second or the third alone not in the same time it takes the quantity of the old value of the first :""( so how can I change my code so I can update all the items or only the checked items.</p> <p>here is my code to display the checkbox's with the quantity.</p> <p>in the file manage_items.php :**</p> <pre><code>&lt;?php $DB_HOST ='localhost'; $DB_USER ='root'; $DB_PASSWORD=''; $DB_DATABASE='dks'; $con= mysql_connect($DB_HOST ,$DB_USER , $DB_PASSWORD); if(!$con){ die('Failed to connect to server :'.mysql_error()); } $db=mysql_select_db($DB_DATABASE); if(!$db){ die("unable to select database"); }?&gt; $qry="SELECT * FROM catalog"; $result= mysql_query($qry); if($result){ while($info = mysql_fetch_array($result)) { print "&lt;h3&gt;&lt;a href=\"\#\"&gt; cat:".$info['name']."&lt;/a&gt;&lt;/h3&gt;&lt;div&gt;"; $qryitem="SELECT * FROM item WHERE Id=". $info['Cid']; $resultitem=mysql_query($qryitem); if($resultitem){ ?&gt; &lt;form method="post" action="manage_item_action.php"&gt; &lt;?php while($info=mysql_fetch_array($resultitem)) { ?&gt; &lt;input type="checkbox" name="op[]" value="&lt;?php echo $info['Id'];?&gt;"/&gt;&lt;?php echo $info['name'];?&gt; &lt;label&gt; Quantity &lt;input type="text" name="Quantity[]" value="&lt;?php echo $info['Quantity'];?&gt;"/&gt;&lt;/label&gt; &lt;br/&gt; &lt;?php } } else echo "There are no items."; print "&lt;/div&gt;"; } } ?&gt; &lt;/div&gt; &lt;input type="submit" value="update" name="submit"/&gt; &lt;/form&gt; </code></pre> <p>and here is the excution of update in the file manage_item_action.php </p> <pre><code>&lt;?php $DB_HOST ='localhost'; $DB_USER ='root'; $DB_PASSWORD=''; $DB_DATABASE='dks'; $con= mysql_connect($DB_HOST ,$DB_USER , $DB_PASSWORD); if(!$con){ die('Failed to connect to server :'.mysql_error()); } $db=mysql_select_db($DB_DATABASE); if(!$db){ die("unable to select database"); } $options=$_POST['op']; $qun=$_POST['Quantity']; $size =count($options); for($i =0; $i&lt;$size; $i++) { //$qryop="UPDATE item SET Quantity =".$qun."WHERE Id =".$options[$i]."';"; $resultop=mysql_query("update item set Quantity='".$qun[i]."'where Id='".$options[$i]."'"); } if($resultop){ header("location: manage_items.php");} else echo "there was an error" ?&gt; </code></pre> <p>i dunno how to fix the problem ! but i think my main problem is that all the textbox's have the same name . so when i send it to the other file for ecution it just take it as it is the last one .</p> <p>help me please :$ </p> <p><strong>*<em>UPDATED</em>*</strong></p> <p><strong>*<em>The Solution</em>*</strong></p> <p>so I solved the problem of my code and its only needed the textbox to be disabled before checkin the checkbox so the array doesn't have any null index..only have the entered values without any nulls :)) </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload