Jump to content

For Each statement with 2 $_POSTs


Crystal-Pixel

Recommended Posts

I have been going in circles for 3 days now trying to figure out how to pass 2 variable values through a foreach statement that will let me run them through mysql. Its basically to update a quantities in a shoppingcart. The two values are, #1 the quantity #2 the product id. The problem I'm having is I am only getting ONE of the values to go through when I need both of them together so I can input the data correctly into the database. If I run 2 Foreach statements I end up with different qtys and the same productid.. I have no idea what else to do.. please help?? :confused:

 

Here is a snippet..

 

foreach (array_merge($_POST['qty'], $_POST['favids']) as $key => $value) {

 

echo '<br><span class="error"><b>Going to Script Number Two - QTY: '.$value.'  FAV ID: '.$favids.'</b></span>';

 

$query = "UPDATE favorites set".

  "  qty='".$key."'".

"  WHERE favid ='".$_POST['favids']."'";

 

echo 'FAVID: '.$key.'<br>';

echo 'QTY:'. $val.'<br>';

 

$result = mysql_query($query) or die ("Error in query: $query");

 

if ($result) {

echo '<br><span class="error"><b>Product Successfully Updated!</b></span>';

}

Link to comment
https://forums.phpfreaks.com/topic/178433-for-each-statement-with-2-_posts/
Share on other sites

Okay I tried this to change it to array_combine, but now the script isn't even running

 

Any ideas? - I need both values in order ot update the database properly, I need the Qty and the ProductId number.... this is comin from a shoppingcart where there are multiple qty text input fields that allow the user to modify there qty and press Update.

 

Snippet of What i have so far:

$qty = array($_POST['qty']);

$favids = array($_POST['favids']);

 

foreach (array_combine($qty, $favids) as $key => $value) {

 

echo '<br><span class="error"><b>Going to Script - QTY: '.$qty.'  FAV ID: '.$favids.'</b></span>';

 

$query = "UPDATE favorites set".

  "  qty='".$qty."'".

"  WHERE favid ='".$favids."'";

 

echo 'FAVID: '.$favids.'<br>';

echo 'QTY:'. $qty.'<br>';

 

$result = mysql_query($query) or die ("Error in query: $query");

 

if ($result) {

echo '<br><span class="error"><b>Product Successfully Updated!</b></span>';

}

 

Here is part of the form. It is within a "While" because it pulls multiple product ids out of the database.

 

while ($row = mysql_fetch_array($result)) {

 

$getproduct = "SELECT * FROM Products WHERE id = '".$row['productid']."'";

$getprod = mysql_query($getproduct) or die("Query Failed: ".mysql_errno()." - ".mysql_error()."<BR>\n$Query<BR>\n");

$pd = mysql_fetch_array($getprod);

 

//Get SizeName

$getsizes = "SELECT sizesname FROM Sizes WHERE id = '".$pd['Size']."'";

$getsize = mysql_query($getsizes) or die("Query Failed: ".mysql_errno()." - ".mysql_error()."<BR>\n$Query<BR>\n");

$finalsize = mysql_fetch_array($getsize);

// $favid = $row['favid'];

 

echo '  <tr>

            <td width="4%" bgcolor="#F7FAFD" class="blacktext">

                  <div align="center">'.$row['favid'].'

                  <input name="favid" id="favid" type="radio" value="'.$row['favid'].'">

                  </div>

</td>

            <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['PartNum'].'</td>

            <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['Name'].'</td>

            <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$finalsize['sizesname'].'</td>

            <td width="26%" bgcolor="#F7FAFD" class="blacktext"><input name="qty[]" type="text" class="tinyform" value ="'.$row['qty'].'" /><input name="favids[]" type="hidden" value="'.$row['favid'].'" /></td>

<td width="4%" bgcolor="#F7FAFD" class="blacktext">

                  <div align="center">

                  <input name="checked[]" id="checked" type="checkbox" value="'.$row['favid'].'">

                  </div>

</td>

          </tr> '; }

}

change form to

echo '   <tr>
             <td width="4%" bgcolor="#F7FAFD" class="blacktext">
                  <div align="center">'.$row['favid'].'
                   <input name="favid" id="favid" type="radio" value="'.$row['favid'].'">
                  </div>
          </td>
             <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['PartNum'].'</td>
             <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['Name'].'</td>
             <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$finalsize['sizesname'].'</td>
             <td width="26%" bgcolor="#F7FAFD" class="blacktext"><input name="qty['.$row['favid'].']" type="text" class="tinyform" value ="'.$row['qty'].'" />'.$row['qty'].'</td>
          <td width="4%" bgcolor="#F7FAFD" class="blacktext">
                  <div align="center">
                   <input name="checked[]" id="checked" type="checkbox" value="'.$row['favid'].'">
                  </div>
          </td>

and on next page do

foreach ($_POST['qty'] as $favid => $qty){ ...

change form to

echo '   <tr>
             <td width="4%" bgcolor="#F7FAFD" class="blacktext">
                  <div align="center">'.$row['favid'].'
                   <input name="favid" id="favid" type="radio" value="'.$row['favid'].'">
                  </div>
          </td>
             <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['PartNum'].'</td>
             <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['Name'].'</td>
             <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$finalsize['sizesname'].'</td>
             <td width="26%" bgcolor="#F7FAFD" class="blacktext"><input name="qty['.$row['favid'].']" type="text" class="tinyform" value ="'.$row['qty'].'" />'.$row['qty'].'</td>
          <td width="4%" bgcolor="#F7FAFD" class="blacktext">
                  <div align="center">
                   <input name="checked[]" id="checked" type="checkbox" value="'.$row['favid'].'">
                  </div>
          </td>

and on next page do

foreach ($_POST['qty'] as $favid => $qty){ ...

 

 

 

 

 

 

That Did it!! Thank you SO MUCH!!!!  :-*

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.