searls03 Posted February 4, 2012 Share Posted February 4, 2012 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???? Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/ Share on other sites More sharing options...
trq Posted February 4, 2012 Share Posted February 4, 2012 Sorry but your question makes little sense. Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314335 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314337 Share on other sites More sharing options...
trq Posted February 4, 2012 Share Posted February 4, 2012 *What* do you want displayed in a foreach loop? And *what* array do you wish to loop through? Seriously, your question makes little sense. Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314338 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314339 Share on other sites More sharing options...
trq Posted February 4, 2012 Share Posted February 4, 2012 that's what I'm attempting to tell you..... Obviously not very well. foreach is a simple construct. foreach ($_POST['products'] as $product) { // do something with $product } Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314341 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314343 Share on other sites More sharing options...
trq Posted February 4, 2012 Share Posted February 4, 2012 $_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]); } Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314344 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 one last question: how would I write this sentence: $priceunit = ($_POST['price']) * $qty; if this is an array: input name="qty[]" type="hidden" value="<?php echo $qty; ?>" /> without getting a fatal error of unsupported operand types? Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314349 Share on other sites More sharing options...
trq Posted February 4, 2012 Share Posted February 4, 2012 You would access the value by it's index. There are examples of this in my previous reply. Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314351 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 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.... Quote Link to comment https://forums.phpfreaks.com/topic/256366-help-with-array/#findComment-1314352 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.