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

how to insert multiple array into database using PHP

P: 10
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function validate(frm)
  3.     {
  4.         var x=document.forms["form2"]["fname1"].value;
  5. if (x==null || x=="")
  6.     {
  7.     alert("First name must be filled out");
  8.     return false;
  9.     }
  10.     var y=document.forms["form2"]["lname1"].value;
  11. if (y==null || y=="")
  12.     {
  13.     alert("last name must be filled out");
  14.     return false;
  15.     }
  16.  
  17.         var w=document.forms["form2"]["std1"].value;
  18.         if(w == "")
  19.     {
  20.         alert("Please Enter Std-Code.");
  21.         return false;
  22.     }
  23.     if(isNaN(w))
  24.     {
  25.         alert("Please Enter Valid Std-Code.");
  26.         return false;
  27.     }
  28.     if(w.length < 3)
  29.     {
  30.         alert("Std-Code should be minimum 3 digits");
  31.         return false;
  32.     }
  33.  
  34.      var p=document.forms["form2"]["phone1"].value;
  35.     if(p == "")
  36.     {
  37.         alert("Please Enter Phone Number.");
  38.         return false;
  39.     }
  40.     if(isNaN(p))
  41.     {
  42.         alert("Please Enter Valid Phone Number.");
  43.          return false;
  44.     }
  45.     if(p.length < 7)
  46.     {
  47.         alert("Contact Number should of minimum 7 digits");
  48.         return false;
  49.     }
  50.  
  51.      var b=document.forms["form2"]["mobile1"].value;
  52.     if(b == "")
  53.     {
  54.         alert("Please Enter Mobile Number.");
  55.         return false;
  56.     }
  57.     if(b!=""){
  58.         if(isNaN(b))
  59.         {
  60.             alert("Please Enter Valid Mobile Number.");
  61.             document.form1.b.value='';
  62.              return false;
  63.         }
  64.         if(b.length < 10)
  65.         {
  66.             alert("Mobile Number should be minimum 10 digits");
  67.             return false;
  68.         }
  69.     }
  70.  
  71.     } 
  72.  
  73.  
  74.  
  75.     function add_feed()
  76.     {
  77.         var div1 = document.createElement('div');
  78.  
  79.         // Get template data
  80.         div1.innerHTML = document.getElementById('newlinktpl').innerHTML;
  81.  
  82.         // append to our form, so that template data
  83.         //become part of form
  84.         document.getElementById('newlink').appendChild(div1);
  85.  
  86.     }
  87.     </script>
  88.  
  89. <?php
  90.     if(isset($_POST['submit1']))
  91.     {
  92.      $sql="INSERT INTO `jos_callbackentry` (`fname1`, `lname1`, `std1`, `phone1`, `mobile1`) VALUES ( '$_POST[fname1]', '$_POST[lname1]', '$_POST[std1]', '$_POST[phone1]', '$_POST[mobile1]')";
  93. mysql_query($sql);
  94.  
  95.     }
  96. ?>
  97.  
  98.  
  99.  
  100. <!--here we insert html code -->
  101. <form method="post" action="" name="form2" id="form2"  onsubmit="return validate(this)">
  102.  
  103.     <table>
  104.     <tr>
  105.         <td valign=top>Submit Callback Entry</td>
  106.         <td valign=top>
  107.             <div id="newlink">
  108.                 <div class="feed">
  109.                  <!-- <input type="text" name="feedurl[]" value="http://feeds.feedburner.com/satya-weblog/scripting" size="50"> <br > -->
  110.                  <table width="100%" border="0"align="center" cellpadding="0" cellspacing="0" class="offwhite">
  111.          <tr>
  112.          <td> <table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" class="white">
  113.          <tr class="sidemenuebg">
  114.  
  115.             <td width="12%" align="right" nowrap><font color="#FF0000">*</font>First Name</td>
  116.             <td width="39%"><input name="fname1" id="fname1" type="text" size="25" maxlength="100" ></td>
  117.             <td width="20%" align="right"><font color="#FF0000">*</font> Last Name</td>
  118.             <td width="31%"><input name="lname1" id="lname1" type="text" size="25" maxlength="100" ></td>
  119.          </tr>
  120.          <tr class="simplegraytable">
  121.             <td colspan="2" align="right" nowrap>Phone</td>
  122.             <td align="left"><input name="std1" id="std1"type="text" size="5" maxlength="5" >
  123.              <input name="phone1" id="phone1" type="text" size="20" maxlength="10" >
  124.              <br >
  125.              <span class="style5">STD Code - Number</span></td>
  126.             <td align="right"><font color="#FF0000">*</font>Mobile</td>
  127.             <td align="left"><input name="mobile1" id="mobile1" type="text" size="25" maxlength="10" onkeyup="checkNumber(this);"></td>
  128.          </tr>
  129.         </table> </td></tr></table>
  130.                     </div>
  131.             </div>
  132.         </td>
  133.     </tr>
  134.     </table>
  135.  
  136.         <p>
  137.             <br>
  138.             <input type="submit" name="submit1">
  139.             <input type="reset" name="reset1">
  140.         </p>
  141.  
  142.     <p id="addnew">
  143.         <a href="javascript:add_feed()">Add New </a>
  144.     </p>
  145.  
  146.     </form>
  147.  
  148.     <!-- Template. This whole data will be added directly to working form above -->
  149.     <div id="newlinktpl" style="display:none">
  150.         <div class="feed">
  151.  
  152.          <table width="100%" border="0"align="center" cellpadding="0" cellspacing="0" class="offwhite">
  153.          <tr>
  154.          <td> <table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" class="white">
  155.          <tr class="sidemenuebg">
  156.             <td width="5%" align="right" nowrap class="simplegraytable">
  157.              </td>
  158.             <td width="12%" align="right" nowrap><font color="#FF0000">*</font>First Name</td>
  159.             <td width="39%"><input name="fname1" id="fname1" type="text" size="25" maxlength="100" ></td>
  160.             <td width="13%" align="right"><font color="#FF0000">*</font>Last Name</td>
  161.             <td width="31%"><input name="lname1" id="lname1" type="text" size="25" maxlength="100" ></td>
  162.          </tr>
  163.          <tr class="simplegraytable">
  164.             <td colspan="2" width="12%" align="right" nowrap>Phone</td>
  165.             <td align="left"><input name="std1" id="std1"type="text" size="5" maxlength="5" >
  166.              <input name="phone1" id="phone1" type="text" size="20" maxlength="10" >
  167.              <br >
  168.              <span class="style5">STD Code - Number</span></td>
  169.             <td align="right"><font color="#FF0000">*</font>Mobile</td>
  170.             <td align="left"><input name="mobile1" id="mobile1" type="text" size="25" maxlength="10" onkeyup="checkNumber(this);"></td>
  171.          </tr>
  172.         </table></td></tr></table>
  173.  
  174.         </div>
  175.     </div>

when i fill information in filed without click "add new"
then information store in table
but when i click on "add new" suppose there in 2 form filed then the second last entry information is store in table and first form entry not store in table .
Plz........ guide me how i can store all information in table when i click on "add new" without redundancy of information.
Dec 23 '11 #1
Share this Question
Share on Google+
5 Replies


Dormilich
Expert Mod 5K+
P: 8,639
with your current setup, you canít. reason being that the template form elements overwrite each other while submitting. you would have to add [] to each name to get an array of values in PHP.

passing data to the DB can be achieved easily:
Expand|Select|Wrap|Line Numbers
  1. // example code
  2. try
  3. {
  4.   $pdo = new PDO($dsn, $login, $password);
  5.   $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  6.   // prepare the query
  7.   $ins = $pdo->prepare("INSERT INTO mytable (field1) VALUES (?)");
  8.   // bind parameter(s), assuming string type here
  9.   $ins->bindParam(1, $field1, PDO::PARAM_STR);
  10.   // loop
  11.   // for multiple values, check out the list() construct
  12.   foreach ($_POST['field1'] as $field1)
  13.   {
  14.     $ins->execute();
  15.   }
  16. }
  17. catch (PDOException $e) 
  18. {
  19.   echo $e->getMessage();
  20. }
this way youíre even immune against SQL Injection attacks.
Dec 23 '11 #2

P: 10
Hi Dormilich

u r right but one prob with i am not getting a idea that how is that possible can u give me some php code that i used for that form .
becoz i am new in this filed so plz give me some code that i apply for that ..............
Dec 23 '11 #3

Dormilich
Expert Mod 5K+
P: 8,639
how that is possible, I have shown in the above code. anyways, here is help on the list() function.
Dec 23 '11 #4

P: 10
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function validate(frm)
  3.     {
  4.         var x=document.forms["form2"]["fname1"].value;
  5. if (x==null || x=="")
  6.     {
  7.     alert("First name must be filled out");
  8.     return false;
  9.     }
  10.     var y=document.forms["form2"]["lname1"].value;
  11. if (y==null || y=="")
  12.     {
  13.     alert("last name must be filled out");
  14.     return false;
  15.     }
  16.  
  17.         var w=document.forms["form2"]["std1"].value;
  18.         if(w == "")
  19.     {
  20.         alert("Please Enter Std-Code.");
  21.         return false;
  22.     }
  23.     if(isNaN(w))
  24.     {
  25.         alert("Please Enter Valid Std-Code.");
  26.         return false;
  27.     }
  28.     if(w.length < 3)
  29.     {
  30.         alert("Std-Code should be minimum 3 digits");
  31.         return false;
  32.     }
  33.  
  34.      var p=document.forms["form2"]["phone1"].value;
  35.     if(p == "")
  36.     {
  37.         alert("Please Enter Phone Number.");
  38.         return false;
  39.     }
  40.     if(isNaN(p))
  41.     {
  42.         alert("Please Enter Valid Phone Number.");
  43.          return false;
  44.     }
  45.     if(p.length < 7)
  46.     {
  47.         alert("Contact Number should of minimum 7 digits");
  48.         return false;
  49.     }
  50.  
  51.      var b=document.forms["form2"]["mobile1"].value;
  52.     if(b == "")
  53.     {
  54.         alert("Please Enter Mobile Number.");
  55.         return false;
  56.     }
  57.     if(b!=""){
  58.         if(isNaN(b))
  59.         {
  60.             alert("Please Enter Valid Mobile Number.");
  61.             document.form1.b.value='';
  62.              return false;
  63.         }
  64.         if(b.length < 10)
  65.         {
  66.             alert("Mobile Number should be minimum 10 digits");
  67.             return false;
  68.         }
  69.     }
  70.  
  71.     }
  72.     var x = 1;
  73.     function add_feed()
  74.     {
  75.         var div1 = document.createElement('div');
  76.         div1.innerHTML = document.getElementById('newlinktpl').innerHTML;
  77.         document.getElementById('newlink').appendChild(div1);
  78.     }
  79.     function count()
  80.     {
  81.  
  82.         x += 1;
  83.         document.getElementById( "counting" ).value = x;
  84.     }
  85. </script>
  86. <?php
  87.     if(isset($_POST['submit1']))
  88.     {
  89.             for($i=0;$i<$_POST['txtCount'];$i++)
  90.         {
  91.             $sql="INSERT INTO `tablename` (`fname1`, `lname1`, `std1`, `phone1`, `mobile1`) VALUES ( '".$_POST['fname1'][$i]."', '".$_POST['lname1'][$i]."', '".$_POST['std1'][$i]."', '".$_POST['phone1'][$i]."', '".$_POST['mobile1'][$i]."')";
  92.             mysql_query($sql);
  93.         }
  94.     }
  95. ?>
  96. <!--here we insert html code -->
  97.  
  98. <div valign=top class="titleheading">Submit Callback Entry</div>
  99. <form method="post" action="" name="form2" id="form2" onsubmit="return validate(this)">
  100.     <table>
  101.     <tr>
  102.           <td valign=top class="tablebody"><div id="newlink">
  103.          <div class="feed">
  104.             <!-- <input type="text" name="feedurl[]" value="http://feeds.feedburner.com/satya-weblog/scripting" size="50"> <br > -->
  105.             <table width="100%" border="0"align="center" cellpadding="0" cellspacing="0" class="offwhite">
  106.              <tr>
  107.                 <td><table width="102%" border="0" align="center" cellpadding="5" cellspacing="1" class="white">
  108.                     <tr class="sidemenuebg">
  109.                      <td width="5%" align="right" nowrap class="simplegraytable"></td>
  110.                      <td width="12%" align="right" nowrap><font color="#FF0000">*</font>First Name</td>
  111.                      <td width="39%"><input name="fname1[]" id="fname1" type="text" size="25" maxlength="100" ></td>
  112.                      <td width="27%" align="right"><font color="#FF0000">*</font>Last Name</td>
  113.                      <td width="31%"><input name="lname1[]" id="lname1" type="text" size="25" maxlength="100" ></td>
  114.                     </tr>
  115.                     <tr class="simplegraytable">
  116.                      <td colspan="2" align="right" nowrap >Phone</td>
  117.                      <td align="left" width="45%"><input name="std1[]" id="std1"type="text" size="5" maxlength="5" >&nbsp;
  118.                         <input name="phone1[]" id="phone1" type="text" size="15" maxlength="10" >
  119.                         <br >
  120.                         <span class="style5">STD Code - Number</span></td>
  121.                      <td align="right"><font color="#FF0000">*</font>Mobile</td>
  122.                      <td align="left"><input name="mobile1[]" id="mobile1" type="text" size="25" maxlength="10" ></td>
  123.                     </tr>
  124.                  </table></td>
  125.              </tr>
  126.             </table>
  127.          </div>
  128.         </div></td>
  129.     </tr>
  130.     </table>
  131.     <p> <br>
  132.     <input class="submitbutton" type="submit" name="submit1" value="Submit">
  133.  
  134. <input class="resetbutton" type="reset" name="reset1">
  135.     </p>
  136.     <p id="addnew"><a href="javascript:add_feed();count();"><input class="addnewbutton" type="button" name="submit" value="Add New Entry"></a>&nbsp;&nbsp;<input size="1" name="txtCount" id = "counting" type = "text" value="1" readonly="readonly" /><strong>&nbsp;</strong>
  137. </p>
  138. </form>
  139. <!-- Template. This whole data will be added directly to working form above -->
  140. <div id="newlinktpl" style="display:none">
  141.     <div class="feed">
  142.     <table width="100%" border="0"align="center" cellpadding="0" cellspacing="0" class="offwhite">
  143.      <tr>
  144.         <td><table width="102%" border="0" align="center" cellpadding="5" cellspacing="1" class="white">
  145.             <tr class="sidemenuebg">
  146.              <td width="5%" align="right" nowrap class="simplegraytable"></td>
  147.              <td width="12%" align="right" nowrap><font color="#FF0000">*</font>First Name</td>
  148.              <td width="39%"><input name="fname1[]" id="fname1" type="text" size="25" maxlength="100" ></td>
  149.              <td width="27%" align="right"><font color="#FF0000">*</font>Last Name</td>
  150.              <td width="31%"><input name="lname1[]" id="lname1" type="text" size="25" maxlength="100" ></td>
  151.             </tr>
  152.             <tr class="simplegraytable">
  153.              <td colspan="2" align="right" nowrap>Phone</td>
  154.              <td align="left" width="45%"><input name="std1[]" id="std1"type="text" size="5" maxlength="5" >&nbsp;
  155.                 <input name="phone1[]" id="phone1" type="text" size="15" maxlength="10" >
  156.                 <br >
  157.                 <span class="style5">STD Code - Number</span></td>
  158.              <td align="right"><font color="#FF0000">*</font>Mobile</td>
  159.              <td align="left"><input name="mobile1[]" id="mobile1" type="text" size="25" maxlength="10" ></td>
  160.             </tr>
  161.          </table></td>
  162.      </tr>
  163.     </table>
  164.     </div>
  165. </div>
Dec 28 '11 #5

Dormilich
Expert Mod 5K+
P: 8,639
you are aware that this code is highly susceptible to SQL injection attacks?
Dec 28 '11 #6

Post your reply

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