Jump to content

Recommended Posts

I do not understand how to write an array....maybe someone can help me.  I need to write this in a foreach loop:

if($_POST['pay'])
{

$product = mysql_real_escape_string($_POST['product']);
$price = mysql_real_escape_string($_POST['price']);
$qty = mysql_real_escape_string($_POST['qty']);
$id = mysql_real_escape_string($_POST['id']);
$total = mysql_real_escape_string($_POST['total']);
$priceunit = ($_POST['price'])* $qty;
$month = date("F Y"); 
$day = date("d"); 
$year = date("Y"); 
$date = date("Y-m-d"); 

$academy = mysql_real_escape_string($_session['academy']);

$sql = "INSERT INTO transactions (price, product, quantity, product_id, priceunit, month, day, year, academy, date) 





VALUES('$priceunit', '$product', '$qty', '$id', '$price', '$month', '$day', '$year', '$academy', '$date' )";
$rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error());
}

 

from this:

 

<input name="product[]" type="hidden" value="<?php echo $product; ?>" /><input name="price[]" type="hidden" value="<?php echo $price1; ?>" /><input name="id[]" type="hidden" value="<?php echo $id; ?>" />
<input name="qty[]" type="hidden" value="<?php echo $qty; ?>" /><input name="total[]" type="hidden" value="$total" />

.....values missing in the form I will add later.  can anyone please help????

Link to comment
https://forums.phpfreaks.com/topic/256366-help-with-array/
Share on other sites

ok.  I am not good at writing foreach loops.  I need help writing the first code in a foreach loop.  the second code is in a while loop, but I didn't show that.  I need it to be able to submit all the data in it that is present.....so like all products.....not just one.

Link to comment
https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314337
Share on other sites

that's what I'm attempting to tell you......but as you can see I don't know much about them.  but let me try again.  I will have multiple products on one page......they will all be using the form name "product".  I need all of these.....lets say 5......to submit to my database in a loop.  currently it only submits that last one.....I need all of them to submit.  I hope this makes a little more sense. :confused:

Link to comment
https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314339
Share on other sites

but it's not just $product that I need....it is all of those that I need.  I get this error when I try: Warning: Invalid argument supplied for foreach() in /home/tkdpostk/public_html/collect.php on line 7:

<?php
include_once("connect.php");
session_start();

if($_POST['pay'])
{
foreach ($_POST['pay'] as $pay) {
$product = mysql_real_escape_string($_POST['product']);
$price = mysql_real_escape_string($_POST['price']);
$qty = mysql_real_escape_string($_POST['qty']);
$id = mysql_real_escape_string($_POST['id']);
$total = mysql_real_escape_string($_POST['total']);
$priceunit = ($_POST['price'])* $qty;
$month = date("F Y"); 
$day = date("d"); 
$year = date("Y"); 
$date = date("Y-m-d"); 

$academy = mysql_real_escape_string($_session['academy']);}


$sql = "INSERT INTO transactions (price, product, quantity, product_id, priceunit, month, day, year, academy, date) 





VALUES('$priceunit', '$product', '$qty', '$id', '$price', '$month', '$day', '$year', '$academy', '$date' )";
$rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error());
}
?>

 

and here is code inside of the loop

 <form name="pay" class="pay" method="POST" action="collect.php"> <?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM cart where cart_id = ".$cid."");
while($row = mysql_fetch_array($sql)){
$product = $row["product123"];
$price1 = $row["price"];
$id = $row["id"];
$qty = $row["quantity"];




?>
<input name="product[]" type="hidden" value="<?php echo $product; ?>" /><input name="price[]" type="hidden" value="<?php echo $price1; ?>" /><input name="id[]" type="hidden" value="<?php echo $id; ?>" />
<input name="qty[]" type="hidden" value="<?php echo $qty; ?>" /><input name="total[]" type="hidden" value="$total" />
  
<hr />

    
<?php
}
?><input type="submit" name="pay" id="pay" value="update" /></form>

.  I seriously am trying to explain.....but it is very hard to express in letters.  I am sorry about it.  Hopefully this one makes some sense.....although I doubt it.

Link to comment
https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314343
Share on other sites

$_POST['pay'] is obviously not an array. Is that the name of your submit button or something?

 

Use a for loop.

 

for ($i = 0; $i <= count($_POST['product']); $i++) {
  $product = mysql_real_escape_string($_POST['product'][$i]);
  $price = mysql_real_escape_string($_POST['price'][$i]);
  $qty = mysql_real_escape_string($_POST['qty'][$i]);
  $id = mysql_real_escape_string($_POST['id'][$i]);
  $total = mysql_real_escape_string($_POST['total'][$i]);
}

Link to comment
https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314344
Share on other sites

ok, I don't know if this fixed or not.....but with this code:

<?php
include_once("connect.php");
session_start();


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

$month = date("F Y"); 
$day = date("d"); 
$year = date("Y"); 
$date = date("Y-m-d"); 

for ($i = 0; $i <= count($_POST['product']); $i++) {
  $product = ($_POST['product'][$i]);
  $price = ($_POST['price'][$i]);
  $qty = ($_POST['qty'][$i]);
  $id = ($_POST['id'][$i]);
  $total = ($_POST['total'][$i]);
  $priceunit = $priceunit * $qty[$i];
  $month = $month[$i];
  $day = $day[$i];
  $year = $year[$i];
  $date = $date[$i];
  

$academy = ($_session['academy'][$i]);
}

$sql = "INSERT INTO transactions (price, product, quantity, product_id, priceunit, month, day, year, academy, date) 





VALUES('$priceunit', '$product', '$qty', '$id', '$price', '$month', '$day', '$year', '$academy', '$date' )";
$rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error());

?>

I get results that are what the attachment is.....exactly what it is......how do I fix this.....and yes all my form fields do have values....

post-112513-13482403226627_thumb.png

Link to comment
https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314352
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.