naima69 Posted April 18, 2007 Share Posted April 18, 2007 This is my order.php file. Now I try to make a new php-file for processing the ordered items on this page, into another table "ordered_products" in the database. I want to INPUT following values into the table "ordered_products": 'order_num', 'order_item' and 'order_ant' I know how to insert rows in a table using the INSERT query, but I don't know how to do it while looping the arrays. I have to fetch the arrays from this page, and insert them row by row in the "ordered_products" table. Can you help me with some code, please? <form action="postorder.php" method="post"> <? $user="*****"; $password="*****"; $database="*****"; $host="*****"; $kund_id=$_POST['kund_id']; $prod_id=$_POST['prod_id']; $prod_nr_ant=$_POST['prod_nr_ant']; $prod_namn=$_POST['prod_namn']; $prod_pris=$_POST['prod_pris']; mysql_connect($host,$user,$password); @mysql_select_db($database) or die("Unable to select database"); $query= "SELECT * FROM table WHERE kund_id='$kund_id' ORDER BY order_num DESC LIMIT 1"; $result=mysql_query($query); $num=mysql_num_rows($result); mysql_close(); $i=0; while ($i < $num) { $order_num=mysql_result($result,$i,"order_num"); $kund_id=mysql_result($result,$i,"kund_id"); echo "<input type=\"text\" size=\"2\" name=\"order_num\" value=\"$order_num\">"; echo "<input type=\"text\" name=\"kund_id\" size=\"3\" value=\"$kund_id\">"; echo "<br>"; echo "</br><br>"; $i++; } ?> <?php foreach ($_POST['prod_namn'] as $key => $val) { if ($_POST['prod_nr_ant'][$key] != 0) { echo '<input type="text" value="' . $key . '" name="order_item" size="5">'; echo '<input type="text" value="' . $val . '" size="50">'; echo '<input type="text" value="' . $_POST['prod_nr_ant'][$key] . '" name="order_ant" size="5"><br>'; } } ?> <?php foreach ($_POST['kat_namn'] as $key2 => $val2) { if ($_POST['kat_ant'][$key2] != 0) { echo '<input type="text" value="' . $key2 . '" name="order_item" size="5">'; echo '<input type="text" value="' . $val2 . '" size="50">'; echo '<input type="text" value="' . $_POST['kat_ant'][$key2] . '" name="order_ant" size="5"><br>'; } } ?> <input type="submit" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/ Share on other sites More sharing options...
mpharo Posted April 18, 2007 Share Posted April 18, 2007 Are you inserting them in the table as an array or into each of their seperate fields? Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/#findComment-232168 Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 i have no idea what's going on with your code. what is the purpose of this page? can you give any more insight on the variable names and/or values? Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/#findComment-232171 Share on other sites More sharing options...
per1os Posted April 18, 2007 Share Posted April 18, 2007 foreach ($insertCols as $col => $val) { $cols .= "`" . $col . "`"; $vals .= "'" . $val . "',"; } $cols = substr($cols, , -1); $vals = substr($vals, , -1); $sql = "INSERT INTO table_name (" . $cols . ") VALUES(" . $vals . ");"; Something like that might help. Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/#findComment-232211 Share on other sites More sharing options...
naima69 Posted April 19, 2007 Author Share Posted April 19, 2007 Hi. Sorry that I didn't explain well enough. This is the query I try to run on the postorder.php page: (I only get this Warning: Invalid argument supplied for foreach() in /home/virtual/etralius.se/public_html/order/postorder.php on line 13) ??? <? $user="*****"; $password="*****"; $database="*****"; $host="*****"; foreach ($_POST['order_item'] as $key => $val) { if ($_POST['order_ant'][$key] != 0) { echo 'Order num: ' . $key . ', Order Item: ' . $val . ', Quantity: ' . $_POST['order_ant'][$key] . '<br>'; // mysql_connect($host,$user,$password); @mysql_select_db($database) or die("Unable to connect to database"); $query= "INSERT INTO ordered_products VALUES ('$order_num','$order_item','$order_ant')"; mysql_query($query); mysql_close(); // } } ?> I have a problem looping the query through the ordered items, inserting them row by row in the database table "ordered_items". Every row will have the same "order_num" wich is the number of the order. "Order_item" is the product number for the product ordered. "Order_ant" is the quantity ordered for the product. Example: A customer has ordered 5 rolls of Thermal paper (product#009), and 10 rolls of printerpaper (product#018). At the order.php page, the customer will be able to see what he has ordered, and then confirm his order by pushig the submit-button. The data is then transmitted to the postorder.php page, where my script is supposed to INSERT the order into the "ordered_products" table: order_numorder_itemorder_ant 3280095 32801810 I have to use arrays doing this, since I have about 150 different items in the store. Can you give me some ideas on what to change in the script to make it work? Thank you, and lots of kisses from a "dumb blond" scandinavian Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/#findComment-233326 Share on other sites More sharing options...
boo_lolly Posted April 19, 2007 Share Posted April 19, 2007 at the top of your page, put this line of code in: echo "<pre>". print_r($_POST, true) ."</pre>\n"; this will print everything that was posted by your form. you will use this information to construct a valid foreach loop and insert query. once you do that, post what the results of your $_POST array are in this thread and we'll go from there. Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/#findComment-233408 Share on other sites More sharing options...
Barand Posted April 19, 2007 Share Posted April 19, 2007 Something like <?php if (isset($_POST['order_num'])) { $order_num = $_POST['order_num']; foreach ($order_prod as $k => $prod) { $qty = $_POST['order_ant'][$key]; if ($qty > 0) { $sql = "INSERT INTO orderitems (order_num, order_prod, order_ant) VALUES ('$order_num', '$prod', $qty)"; mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); } } } ?> <form method='post'> Order Number <input type='text' name='order_num' size='6'><br> Product <input type='text' name='order_prod[]' size='6'> Qty <input type='text' name='order_ant[]' size='6'> <br> Product <input type='text' name='order_prod[]' size='6'> Qty <input type='text' name='order_ant[]' size='6'> <br> Product <input type='text' name='order_prod[]' size='6'> Qty <input type='text' name='order_ant[]' size='6'> <br> Product <input type='text' name='order_prod[]' size='6'> Qty <input type='text' name='order_ant[]' size='6'> <br> Product <input type='text' name='order_prod[]' size='6'> Qty <input type='text' name='order_ant[]' size='6'> <br> <input type='submit' name='action' value='Submit'> <form> Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/#findComment-233477 Share on other sites More sharing options...
naima69 Posted May 4, 2007 Author Share Posted May 4, 2007 Hi, Barand. Thank you. I think it's almost complete. It's just that the script you gave me give this Warning: "Invalid argument supplied for foreach() in /home/virtual///postorder.php on line 24" Line 24 is this line: foreach ($order_item as $k => $prod) { Do you know what types of problems that can cause this? I use PHP version 4.4.4, so I don't think the version is too old. Naima Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/#findComment-245482 Share on other sites More sharing options...
naima69 Posted May 4, 2007 Author Share Posted May 4, 2007 Hi. I have struggled a lot with this issue. Now it seems that what I get from the ordr.php page, is several strings, not arrays. So, can anyone help me looping strings to insert them into my MySql database table? Thank you This is my order.php file. Now I try to make a new php-file for processing the ordered items on this page, into another table "ordered_products" in the database. I want to INPUT following values into the table "ordered_products": 'order_num', 'order_item' and 'order_ant' I know how to insert rows in a table using the INSERT query, but I don't know how to do it while looping the arrays. I have to fetch the arrays from this page, and insert them row by row in the "ordered_products" table. Can you help me with some code, please? <form action="postorder.php" method="post"> <? $user="*****"; $password="*****"; $database="*****"; $host="*****"; $kund_id=$_POST['kund_id']; $prod_id=$_POST['prod_id']; $prod_nr_ant=$_POST['prod_nr_ant']; $prod_namn=$_POST['prod_namn']; $prod_pris=$_POST['prod_pris']; mysql_connect($host,$user,$password); @mysql_select_db($database) or die("Unable to select database"); $query= "SELECT * FROM table WHERE kund_id='$kund_id' ORDER BY order_num DESC LIMIT 1"; $result=mysql_query($query); $num=mysql_num_rows($result); mysql_close(); $i=0; while ($i < $num) { $order_num=mysql_result($result,$i,"order_num"); $kund_id=mysql_result($result,$i,"kund_id"); echo "<input type=\"text\" size=\"2\" name=\"order_num\" value=\"$order_num\">"; echo "<input type=\"text\" name=\"kund_id\" size=\"3\" value=\"$kund_id\">"; echo "<br>"; echo "</br><br>"; $i++; } ?> <?php foreach ($_POST['prod_namn'] as $key => $val) { if ($_POST['prod_nr_ant'][$key] != 0) { echo '<input type="text" value="' . $key . '" name="order_item" size="5">'; echo '<input type="text" value="' . $val . '" size="50">'; echo '<input type="text" value="' . $_POST['prod_nr_ant'][$key] . '" name="order_ant" size="5"><br>'; } } ?> <?php foreach ($_POST['kat_namn'] as $key2 => $val2) { if ($_POST['kat_ant'][$key2] != 0) { echo '<input type="text" value="' . $key2 . '" name="order_item" size="5">'; echo '<input type="text" value="' . $val2 . '" size="50">'; echo '<input type="text" value="' . $_POST['kat_ant'][$key2] . '" name="order_ant" size="5"><br>'; } } ?> <input type="submit" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/#findComment-245556 Share on other sites More sharing options...
Barand Posted May 4, 2007 Share Posted May 4, 2007 If you look at the code I posted, you'll see that the text input fields in the form have names ending with "[]". This causes them to be posted as an arrays of values which are then processed by the foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/#findComment-245644 Share on other sites More sharing options...
naima69 Posted May 5, 2007 Author Share Posted May 5, 2007 Thanks, Barand. I finally got it right !!!! Quote Link to comment https://forums.phpfreaks.com/topic/47554-solved-how-to-insert-arrays-into-mysql-database/#findComment-246276 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.