By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

How to insert 2 arrays into mysql table.

P: 1
Hello,

I have a problem with foreach loop. i have 2 arrays which are the part of a dynamic form ie; the fields will be increasing with click. i try to insert with for each loop. but i ont now how can i use for each for multiple array to insert in database. And its not work && or AND with my code. Please help me... Any body. Thank You...

I pasted the piece of code below.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <title>Create Order Form</title>
  6. <script type="text/javascript" src="js/jquery.js"></script>
  7. <link rel="stylesheet" type="text/css" href="css/css.css" />
  8. <script type="text/javascript">
  9. var count = 0;
  10. $(function(){
  11.     $('p#add_field').click(function(){
  12.         count += 4;
  13.         $('#container').append(
  14.                 '<input id="field_' + count + '" name="prd[]' + '" type="text"  style="float:left; margin:0px"/>' 
  15.                 + '<input id="field_' + count + '" name="qty[]' + '" type="text" style="float:right; margin:0px"  />'+'<div class="vspace"></div></br>');
  16.  
  17.     <!--<label for="name" style="float:left;">Product:</label>'+'<label for="name" style="float:right; margin-right:75px;">Quantity:</label>'+'<div class="vspace"></div></br>' + '-->
  18.  
  19.     });
  20. });
  21. </script>
  22.  
  23. </head>
  24. <body>
  25. <div id="wrapper">
  26. <div id="header">
  27. <h1 align="center"> Admin - Create Order </h1>
  28. <div id="menu">
  29. <ul>
  30. <li class="active"><a href="create_order.php">Create_order</a></li>
  31. <li><a href="#">Edit_order</a></li>
  32. <li><a href="#">Delete_order</a></li>
  33. <li><a href="confirm.php">Checkout</a></li>
  34. <li><a href="logout.php">Logout</a></li>
  35. </ul>
  36. </div>
  37. </div>
  38. <div id="page">
  39. <?php
  40.  
  41. //If form was submitted
  42. if (isset($_POST['btnSubmit'])) {
  43.  
  44.     //create instance of database class
  45.     $db = new mysqldb();
  46.     $db->select_db();
  47.  
  48.             $cname = $_POST['cname'];
  49.             $email = $_POST['email'];
  50.             $price = $_POST['price'];
  51.             $shpn = $_POST['shpn'];
  52.  
  53.             //Insert into cr_order
  54.  
  55.     $sql_order = sprintf("INSERT INTO cr_order (cname,email,price,shpn) VALUES ('%s','%s','%s','%s')",
  56.             mysql_real_escape_string($cname),mysql_real_escape_string($email),mysql_real_escape_string($price),mysql_real_escape_string($shpn) );  
  57.             $result_order = $db->query($sql_order);
  58.                     $inserted_order_id = $db->last_insert_id();
  59.                     $_SESSION['ref']=$inserted_order_id;
  60.  
  61. //if ($_POST['prd'] || $_POST['qty']) {
  62.  
  63.             $inserted_order_id = $db->last_insert_id();
  64.  
  65.         //Loop through added fields
  66.         for((list($pkey, $value) = each($_POST['prd'])) &&  (list($qkey, $qvalue) = each($_POST['qty'])) {        
  67.  
  68.             //Insert into productlist
  69.  
  70.             $sql_product = sprintf("INSERT INTO productlist (OrderID,product,quantity) VALUES ('%s','%s','%s')",
  71.                                  mysql_real_escape_string($inserted_order_id),mysql_real_escape_string($value),mysql_real_escape_string($qvalue) );  
  72.             $result_product = $db->query($sql_product);
  73.             $inserted_product_id = $db->last_insert_id();
  74.  
  75.         }
  76.     /*} else {
  77.  
  78.         //No additional fields added by user
  79.  
  80.     }*/
  81.     header("location:confirm.php");
  82.  
  83.     //disconnect mysql connection
  84.     $db->kill();
  85. }
  86. ?>
  87.  
  88. <?php if (!isset($_POST['btnSubmit'])) { ?>
  89. <div id="entry">
  90.     <form name="test" method="post" action="" id="form">
  91.         <label for="name">Name:</label>
  92.         <input type="text" name="cname" id="cname" />
  93.  
  94.         <div class="vspace"></div>
  95.  
  96.         <label for="name">Email:</label>
  97.         <input type="text" name="email" id="email" /> 
  98.  
  99.         <div class="vspace"></div><div class="vspace"></div>
  100.  
  101.         <div id="container">
  102.         <label for="name" style="float:left;">Product:</label>
  103.         <label for="name" style="float:right; margin-right:75px;">Quantity:</label>
  104.         <div class="vspace"></div></br>
  105.         <input id="field_1" name="prd[]" type="text"  style="float:left; margin:0px"/> 
  106.         <input id="field_1" name="qty[]" type="text" style="float:right; margin:0px"  />
  107.         <div class="vspace"></div></br>
  108.  
  109.         <input id="field_2" name="prd[]" type="text"  style="float:left; margin:0px"/> 
  110.         <input id="field_2" name="qty[]" type="text" style="float:right; margin:0px"  />
  111.         <div class="vspace"></div></br>
  112.  
  113.         <input id="field_3" name="prd[]" type="text"  style="float:left; margin:0px"/> 
  114.         <input id="field_3" name="qty[]" type="text" style="float:right; margin:0px"  />
  115.         <div class="vspace"></div></br>
  116.  
  117.             <p id="add_field"><a href="#"><span>&raquo; Add More Product Details.....</span></a></p>
  118.         </div>
  119.  
  120.         <div class="vspace"></div>
  121.         <label for="name">Price:</label>
  122.         <input type="text" name="price" id="price" />
  123.  
  124.         <div class="vspace"></div>
  125.  
  126.         <label for="name">Shipping:</label>
  127.         <input type="text" name="shpn" id="shpn" /> 
  128.  
  129.         <div class="vspace"></div>
  130.         <input id="go" name="btnSubmit" type="submit" value="Signup" class="btn" />
  131.     </form>
  132.     </div>
  133. <?php } ?>
  134. </div>
  135. </div>
  136. </body>
  137. </html>
Nov 11 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.