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

dynamic rows insert into database using php

P: 9
i have some dynamic rows, when i submit a single row submit into my database. how can i insert my dynamic all rows into database by submit?








Expand|Select|Wrap|Line Numbers
  1. entry_history.php    (page)
  2.  
  3.  
  4.  
  5.  
  6.  
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  8. <html xmlns="http://www.w3.org/1999/xhtml">
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  11. <title>Untitled Document</title>
  12.  
  13. <?php include("sql_conn.php");?>
  14.  
  15. <style>
  16. body{background-color:#F4FDDB;}
  17. .table{
  18. width:100%;
  19.  
  20. border:groove;
  21. border-color:#FFFFFF;
  22.  
  23. background-color:#E8F0EE;
  24. }
  25. th{
  26. font-style:italic;
  27. color:#999999;
  28.  
  29. }
  30. </style>
  31.  
  32.  
  33.  
  34.  
  35.  
  36. <SCRIPT language="javascript">
  37.  
  38.                 function addRow(tableID) {
  39.                 var table = document.getElementById(tableID);
  40.  
  41.              var rowCount = table.rows.length;
  42.             var row = table.insertRow(rowCount);
  43.  
  44.             var colCount = table.rows[0].cells.length;
  45.  
  46.            for(var i=0; i<colCount; i++) {
  47.            var newcell = row.insertCell(i);
  48.            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
  49.            switch(newcell.childNodes[0].type) {
  50.                     case "text":
  51.                             newcell.childNodes[0].value = "";
  52.                             break;
  53.                     case "checkbox":
  54.                             newcell.childNodes[0].checked = false;
  55.                             break;
  56.                     case "select-one":
  57.                             newcell.childNodes[0].selectedIndex = 0;
  58.                             break;
  59.  
  60.          }}}
  61.  
  62.         function deleteRow(tableID) {
  63.           try {
  64.            var table = document.getElementById(tableID);
  65.             var rowCount = table.rows.length;
  66.             for(var i=0; i<rowCount; i++) {
  67.                var row = table.rows[i];
  68.                 var chkbox = row.cells[0].childNodes[0];
  69.                 if(null != chkbox && true == chkbox.checked) {
  70.                     if(rowCount <= 1) {
  71.                         alert("Cannot delete all the rows.");
  72.                         break;
  73.                     }
  74.                     table.deleteRow(i);
  75.                     rowCount--;
  76.                     i--;
  77.                 }}
  78.             }catch(e) {
  79.                 alert(e);
  80.             }}
  81.  
  82.  
  83.  
  84.  
  85.     </SCRIPT>
  86.      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. </HEAD>
  98. <BODY>
  99. <header><ul><tr><li><a href="https://bytes.com/submit/mediasourcebd_reza/home.php">Home</a></li></tr></ul></header>
  100. <br />
  101.  
  102.  
  103.  
  104.     <form action="action_entry_history.php" method="post" name="" id="">
  105.     <table class="table" >
  106.     <tr>
  107.  
  108.         <th  align="right">Title</th>
  109.         <th  align="right">Start Time</th>
  110.         <th align="right">End Time</th>
  111.         <th  align="right">Program</th>
  112.         <th  align="center">Peak/Off Peak</th>
  113.         <th align="left">Sub Brand</th>
  114.         <th align="left">Sponsor</th>
  115.         <th align="left">Product</th>
  116.     </tr>
  117.     </table>
  118.     <table id="dataTable" border="0" class="table">
  119.             <tr>
  120.             <td><INPUT type="checkbox" name="chk[]"/></td>
  121.  
  122.             <td><input type="text" name="title[]"/></td>
  123.             <td><input type="time" name="start_time[]"></td>
  124.             <td><input type="time" name="end_time[]"></td>
  125.  
  126.  
  127.             <td><?php 
  128.                         $sql = "SELECT program_name FROM program";
  129.                         $result = mysql_query($sql);
  130.                         echo "<select name='program_name[]' >";
  131.                         while ($row = mysql_fetch_array($result)) {
  132.                         echo "<option value='" . $row['program_name'] ."'>" . $row['program_name'] ."</option>";
  133.                         }
  134.                         echo "</select>";        
  135.                         ?></td>
  136.             <td><select name="peak_offpeak[]"><option>Peak</option><option>0ff_Peak</option></select></td>
  137.             <td><?php 
  138.                         $sql = "SELECT sub_brand_name FROM sub_brand";
  139.                         $result = mysql_query($sql);
  140.                         echo "<select name='sub_brand_name[]' >";
  141.                         while ($row = mysql_fetch_array($result)) {
  142.                         echo "<option value='" . $row['sub_brand_name'] ."'>" . $row['sub_brand_name'] ."</option>";
  143.                         }
  144.                         echo "</select>";        
  145.                         ?></td>
  146.         <td><input type="text" name="sponsor[]"/></td>
  147.         <td><?php 
  148.                         $sql = "SELECT product_name FROM product";
  149.                         $result = mysql_query($sql);
  150.                         echo "<select name='product_name[]' >";
  151.                         while ($row = mysql_fetch_array($result)) {
  152.                         echo "<option value='" . $row['product_name'] ."'>" . $row['product_name'] ."</option>";
  153.                         }
  154.                         echo "</select>";        
  155.                         ?></td>
  156.             </tr>
  157.  
  158.         </table>
  159.  
  160.  
  161.     <table class="table"><th colspan="13">End</th></table>
  162.     <table><th><INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
  163. <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
  164. <input type="submit" value="Save" />
  165.  
  166. </table>
  167.  
  168.  </form>
  169.  
  170.  
  171. ************************
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180. action_entry_history.php   (page)
  181.  
  182.  
  183.  
  184.  
  185. <?php include("sql_conn.php");
  186. $title = $_POST['title'][$i];
  187. for ($i = 0; $i < count($_POST['title']); $i++) {
  188.  
  189. $title = $_POST['title'][$i];
  190. $start_time = $_POST['start_time'][$i];
  191. $end_time = $_POST['end_time'][$i];
  192. $duration = $_POST['duration'][$i];
  193. $peak_offpeak = $_POST['peak_offpeak'][$i];
  194. $program_name = $_POST['program_name'][$i];
  195. $sub_brand_name = $_POST['sub_brand_name'][$i];
  196. $sponsor = $_POST['sponsor'][$i];
  197. $product_name = $_POST['product_name'][$i];
  198.  
  199. }
  200.  
  201. $que = "insert into entry_history(date,channel_name,title,start_time,end_time,duration,peak_offpeak,program_name,sub_brand_name,sponsor,product_name) values('$date','$channel_name','$title','$start_time','$end_time','$duration','$peak_offpeak','$program_name','$sub_brand_name','$sponsor','$product_name')";
  202.  
  203.  
  204.  
  205. if(mysql_query($que))
  206. {
  207.  header('Location:entry_history.php');
  208. }
  209.  echo "data has been inserted into database";
  210.  
  211. echo $que; exit;
  212. mysql_close($conn);
  213.  
  214. ?>
Apr 28 '16 #1
Share this Question
Share on Google+
2 Replies


P: 9
Expand|Select|Wrap|Line Numbers
  1. your submit page
  2.  
  3.  
  4.  
  5. <?php
  6. session_start();
  7. $date = $_SESSION['date'];             
  8. $channel_name = $_SESSION["channel_name"];?>
  9.  
  10. <?php include("sql_conn.php");
  11. $num = $_POST['reza'];
  12.  
  13. for($i>0;$i<=$num;$i++)
  14. {
  15. if(isset($_REQUEST["title$i"])){$title = $_REQUEST["title$i"];}
  16. if(isset($_REQUEST["start_time$i"])){$start_time = $_REQUEST["start_time$i"];}
  17. if(isset($_REQUEST["end_time$i"])){$end_time = $_REQUEST["end_time$i"];}
  18. if(isset($_REQUEST["duration$i"])){$duration = $_REQUEST["duration$i"];}
  19. if(isset($_REQUEST["sponsor$i"])){$sponsor = $_REQUEST["sponsor$i"];}
  20. if(isset($_REQUEST["peak_offpeak$i"])){$peak_offpeak = $_REQUEST["peak_offpeak$i"];}
  21. if(isset($_REQUEST["program_name$i"])){$program_name = $_REQUEST["program_name$i"];}
  22. if(isset($_REQUEST["sub_brand_name$i"])){$sub_brand_name = $_REQUEST["sub_brand_name$i"];}
  23. if(isset($_REQUEST["product_name$i"])){$product_name = $_REQUEST["product_name$i"];}
  24.  
  25. $sql = "Insert Into entry_history (date,channel_name,title,start_time,end_time,duration,sponsor,peak_offpeak,program_name,sub_brand_name,product_name) Values('$date','$channel_name','$title','$start_time','$end_time','$duration','$sponsor','$peak_offpeak','$program_name','$sub_brand_name','$product_name')"; 
  26. $result=mysql_query($sql) or die(mysql_error());
  27.  
  28. echo ($sql);
  29. }
  30. header('location:entry_history.php');
  31.  
  32.  
  33. ?>
  34.  
  35.  
  36.  
  37.  
Jun 18 '16 #2

P: 3
Step1 : Determining the correct SQL Insert command
When you have it write data to a database , if it's used sql statemant , specially the insert command.
i)Login into your cpanel and it's phpmyadmin icon.
ii)You choose then left menu bar , first to click database name and then click the table work.
iii)Top menu to insert data
iV)You've copy it sql statement to a temporay location , such as a text file on your own computer.
step 2 : Now to write php code that will excute mysql query.
Now We have modify data for smaple query we need it modify it run data for submitted comment.
step 3 : Now placing our php code on your pages.
Create a file named manage_comments.php
Paste in the sample code above
Update both page1.php and page2.php to include manage_comments.php by using
<? include("manage_comments.php"); ?>
at the top of the file


<? include("manage_comments.php"); ?>

<h1>This is page1.php</h1>

<div><a href='page2.php?id=2'>Click here</a> to go to page2.php</div>

<div style='margin:20px; width:100px; height:100px; background:blue;'></div>

<? include("formcode.php"); ?>
Jun 20 '16 #3

Post your reply

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