Need Help with PHP Please

I need some help with the below code. Lets start with the HTML code first…There is a 2 field input form that pulls numbers from MySQL database which is displayed correctly. I can successfully update the QTY (products quantity) field but i cannot the FLAGGED (products isbn) field. On the php code how can i use both qty and flagged and them update the database? Pleaseeeeeee help meeeee


  */
  } else if(isset($_POST['confirmed']) && $_POST['confirmed'] ==1) // show "done" page
  {

	
    foreach($_POST['update'] as $pid=>$qty)
      {
		
      // get the old quantity for logging purposes
        $selectql = $db->Execute("SELECT products_quantity, products_isbn FROM ".DB_PREFIX."products WHERE products_id = " . (int)$pid);
        $updateql = $db->Execute("UPDATE ".DB_PREFIX."products SET products_quantity = " . (int)$qty . " WHERE products_id = " . (int)$pid);



<form action="ez_update.php" method="post">
    <br /><br /><br />The following stock/inventory will be updated: <br /><br />
    <?php
    foreach($updated as $pid=>$info){
      ?>
      <input type="hidden" name="update[<?php echo $pid; ?>]" value="<?php echo $info['qty'];?>" />
       <input type="hidden" name="update[<?php echo $pid; ?>]" value="<?php echo $info['flagged'];?>" />
</form>
<BR>
<form method="post" action="myfile.php">
 <input type="text" name="flagged[<?php echo $pid; ?>]" value="<?php echo $info['products_isbn']; ?>"/>&nbsp;&nbsp;<i style="font-size:11px">(Product Flagged)</i><BR>
 <input type="text" name="qty[<?php echo $pid; ?>]" value="<?php echo $info['products_quantity']; ?>" />
 <input type="submit" name="update[<?php echo $pid; ?>]" value="Update" />
</form>

All your form fields are using the product ID as the key so use the KEY in the foreach loop to grab the values.

<?php
foreach($_POST['update'] as $pid=>$a)  
{   
	$qty = $_POST['qty'][$pid]; 	    
	$flagged = $_POST['flagged'][$pid];
	// get the old quantity for logging purposes
	$selectql = $db->Execute("SELECT products_quantity, products_isbn FROM ".DB_PREFIX."products WHERE products_id = " . (int)$pid);
	$updateql = $db->Execute("UPDATE ".DB_PREFIX."products SET products_quantity = " . (int)$qty . " WHERE products_id = " . (int)$pid);
}  
?>

And you can get rid of that first hidden form.

ok thanks, my other question is about the $updateql line, would i need to tweak that line also to add a second SET like i did below?


<?php 
foreach($_POST['update'] as $pid=>$a)   
{    
    $qty = $_POST['qty'][$pid];          
    $flagged = $_POST['flagged'][$pid]; 
    // get the old quantity for logging purposes 
    $selectql = $db->Execute("SELECT products_quantity, products_isbn FROM ".DB_PREFIX."products WHERE products_id = " . (int)$pid); 
    $updateql = $db->Execute("UPDATE ".DB_PREFIX."products SET products_quantity = " . (int)$qty . " AND SET products_ISBN = " . (int)$flagged . " WHERE products_id = " . (int)$pid); 
}   
?>

No just separate those with a comma.

$updateql = $db->Execute("UPDATE ".DB_PREFIX."products SET products_quantity = " . (int)$qty . ", products_ISBN = " . (int)$flagged . " WHERE products_id = " . (int)$pid);

ok that works by updating my flagged count, but it sets my database QTY to zero.

I can do a screen share if you want

How can i insert a second echo into the HTML form?

the below code works but my qty changes to 0


<input type="hidden" name="update[<?php echo $pid; ?>]" value="<?php echo $info['qty'];?>" />

the below code changes everything to 0


<input type="hidden" name="update[<?php echo $pid; ?>]" value="<?php echo $info['qty'];?>" />
<input type="hidden" name="update[<?php echo $pid; ?>]" value="<?php echo $info['flagged'];?>" />

should i change the $pid to the key $a ?

Looks like you didn’t get rid of that first hidden form like I said.

Sometimes you need to echo values to see what’s going on.

<?php
foreach($_POST['update'] as $pid=>$a)  
{   
	$qty = $_POST['qty'][$pid]; 	    
	$flagged = $_POST['flagged'][$pid];
	echo $qty . "<br />";
	// get the old quantity for logging purposes
	//$selectql = $db->Execute("SELECT products_quantity, products_isbn FROM ".DB_PREFIX."products WHERE products_id = " . (int)$pid);
	//$updateql = $db->Execute("UPDATE ".DB_PREFIX."products SET products_quantity = " . (int)$qty . ", products_ISBN = " . (int)$flagged . " WHERE products_id = " . (int)$pid); 
}  
?>

Adding this during testing will give you a better understanding of what’s going on.

<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>

Also having these with the same name and key are overriding each other. Remove first form like I said.

<input type=“hidden” name=“update[<?php echo $pid; ?>]” value=“<?php echo $info[‘qty’];?>” />
<input type=“hidden” name=“update[<?php echo $pid; ?>]” value=“<?php echo $info[‘flagged’];?>” />

You can’t really submit two forms at the same time anyway but maybe those values are interfering in some way, though I doubt it, not being in the same form.

I’m a newbie at php and i still need your help. I will reward you for your help. Do you have skype or oovoo?

If you were to run this little test page, you will see that keys correspond on all three post names, flagged, qty and update, no matter which button you push.

<html>
<body>
<?php
if(isset($_POST['update'])){
	foreach($_POST['update'] as $pid=>$a)  
	{   
		$qty = $_POST['qty'][$pid]; 	    
		$flagged = $_POST['flagged'][$pid];
		echo "<br />Quatity: " . $qty;
		echo "<br />Flagged: " . $flagged;
		// get the old quantity for logging purposes
		//$selectql = $db->Execute("SELECT products_quantity, products_isbn FROM ".DB_PREFIX."products WHERE products_id = " . (int)$pid);
		//$updateql = $db->Execute("UPDATE ".DB_PREFIX."products SET products_quantity = " . (int)$qty . ", products_ISBN = " . (int)$flagged . " WHERE products_id = " . (int)$pid); 
	}
	
echo "<pre>";
print_r($_POST);
echo "</pre>"; 
}  
?>
<?php
// TEST
foreach(range(0,10) as $pid => $v){
$info['products_isbn'] = "isbn".$v;
$info['products_quantity'] = $v;
?>
<form method="post" action="">
 <input type="text" name="flagged[<?php echo $pid; ?>]" value="<?php echo $info['products_isbn']; ?>"/>&nbsp;&nbsp;<i style="font-size:11px">(Product Flagged)</i><BR>
 <input type="text" name="qty[<?php echo $pid; ?>]" value="<?php echo $info['products_quantity']; ?>" />
 <input type="submit" name="update[<?php echo $pid; ?>]" value="Update" />
</form>
<?php } ?> 
</body>
</html>

For example all keys are 5 here.

Quatity: 5
Flagged: isbn5

Array
(
    [flagged] => Array
        (
            [5] => isbn5
        )

    [qty] => Array
        (
            [5] => 5
        )

    [update] => Array
        (
            [5] => Update
        )

)

Just as another example of how the key/value works in a rough order form.

<html>
<body>
<?php
if(isset($_POST['update'])):
	foreach($_POST['qty'] as $pid=>$qty):
		if(!empty($qty)):
			echo "<br />Product ID: " . $pid . " Quatity: " . $qty;
		endif;
	endforeach;
		
	echo "<pre>";
	print_r($_POST);
	echo "</pre>";
endif;
?>	
<form method="post" action="">
<?php
// TEST
foreach(range(0,150, 15) as $pid => $v){
$info['products_isbn'] = "Product".$v;
$info['products_quantity'] = $v;
?>
 <?php echo $info['products_isbn']; ?>
 <input type="text" name="qty[<?php echo $pid; ?>]" value="" /><br />
<?php } ?>
 <input type="submit" name="update" value="Update" />
</form>
</body>
</html>

Would return


Product ID: 2 Quatity: 4
Product ID: 4 Quatity: 5
Product ID: 6 Quatity: 2
Product ID: 8 Quatity: 6

Array
(
    [qty] => Array
        (
            [0] =>
            [1] =>
            [2] => 4
            [3] =>
            [4] => 5
            [5] =>
            [6] => 2
            [7] =>
            [8] => 6
            [9] =>
            [10] =>
        )

    [update] => Array
        (
            [10] => Update
        )

)