dynamic rows insert into database using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • helalreza
    New Member
    • Apr 2016
    • 9

    dynamic rows insert into database using php

    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?








    Code:
    entry_history.php    (page)
    
    
    
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    
    <?php include("sql_conn.php");?>
    
    <style>
    body{background-color:#F4FDDB;}
    .table{
    width:100%;
    
    border:groove;
    border-color:#FFFFFF;
    
    background-color:#E8F0EE;
    }
    th{
    font-style:italic;
    color:#999999;
    
    }
    </style>
    
    
    
    
    
    <SCRIPT language="javascript">
    
    		        function addRow(tableID) {
    		        var table = document.getElementById(tableID);
    				
                 var rowCount = table.rows.length;
                var row = table.insertRow(rowCount);
    			
                var colCount = table.rows[0].cells.length;
    			
               for(var i=0; i<colCount; i++) {
               var newcell = row.insertCell(i);
               newcell.innerHTML = table.rows[0].cells[i].innerHTML;
               switch(newcell.childNodes[0].type) {
                        case "text":
                                newcell.childNodes[0].value = "";
                                break;
                        case "checkbox":
                                newcell.childNodes[0].checked = false;
                                break;
                        case "select-one":
                                newcell.childNodes[0].selectedIndex = 0;
                                break;
                       				  			          
         	}}}
     
            function deleteRow(tableID) {
              try {
               var table = document.getElementById(tableID);
                var rowCount = table.rows.length;
                for(var i=0; i<rowCount; i++) {
                   var row = table.rows[i];
                    var chkbox = row.cells[0].childNodes[0];
                    if(null != chkbox && true == chkbox.checked) {
                        if(rowCount <= 1) {
                            alert("Cannot delete all the rows.");
                            break;
                        }
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }}
                }catch(e) {
                    alert(e);
                }}
    			
    			
    			
    		
        </SCRIPT>
    	 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    </HEAD>
    <BODY>
    <header><ul><tr><li><a href="https://bytes.com/submit/mediasourcebd_reza/home.php">Home</a></li></tr></ul></header>
    <br />
    
    
    
    	<form action="action_entry_history.php" method="post" name="" id="">
    	<table class="table" >
    	<tr>
    		
    		<th  align="right">Title</th>
    		<th  align="right">Start Time</th>
    		<th align="right">End Time</th>
    		<th  align="right">Program</th>
    		<th  align="center">Peak/Off Peak</th>
    		<th align="left">Sub Brand</th>
    		<th align="left">Sponsor</th>
    		<th align="left">Product</th>
    	</tr>
    	</table>
        <table id="dataTable" border="0" class="table">
    	        <tr>
                <td><INPUT type="checkbox" name="chk[]"/></td>
    
                <td><input type="text" name="title[]"/></td>
    			<td><input type="time" name="start_time[]"></td>
    			<td><input type="time" name="end_time[]"></td>
    			
    							
    			<td><?php 
    						$sql = "SELECT program_name FROM program";
    						$result = mysql_query($sql);
    						echo "<select name='program_name[]' >";
    						while ($row = mysql_fetch_array($result)) {
        					echo "<option value='" . $row['program_name'] ."'>" . $row['program_name'] ."</option>";
    						}
    						echo "</select>";		
    						?></td>
    			<td><select name="peak_offpeak[]"><option>Peak</option><option>0ff_Peak</option></select></td>
    			<td><?php 
    						$sql = "SELECT sub_brand_name FROM sub_brand";
    						$result = mysql_query($sql);
    						echo "<select name='sub_brand_name[]' >";
    						while ($row = mysql_fetch_array($result)) {
        					echo "<option value='" . $row['sub_brand_name'] ."'>" . $row['sub_brand_name'] ."</option>";
    						}
    						echo "</select>";		
    						?></td>
    		<td><input type="text" name="sponsor[]"/></td>
    		<td><?php 
    						$sql = "SELECT product_name FROM product";
    						$result = mysql_query($sql);
    						echo "<select name='product_name[]' >";
    						while ($row = mysql_fetch_array($result)) {
        					echo "<option value='" . $row['product_name'] ."'>" . $row['product_name'] ."</option>";
    						}
    						echo "</select>";		
    						?></td>
    			</tr>
    		
    		</table>
    		
    			
    	<table class="table"><th colspan="13">End</th></table>
    	<table><th><INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
    <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
    <input type="submit" value="Save" />
    
    </table>
    
     </form>
    
    
    ************************
    
    
    
    
    
    
    
    
    action_entry_history.php   (page)
    
    
    
    
    <?php include("sql_conn.php");
    $title = $_POST['title'][$i];
    for ($i = 0; $i < count($_POST['title']); $i++) {
       
    $title = $_POST['title'][$i];
    $start_time = $_POST['start_time'][$i];
    $end_time = $_POST['end_time'][$i];
    $duration = $_POST['duration'][$i];
    $peak_offpeak = $_POST['peak_offpeak'][$i];
    $program_name = $_POST['program_name'][$i];
    $sub_brand_name = $_POST['sub_brand_name'][$i];
    $sponsor = $_POST['sponsor'][$i];
    $product_name = $_POST['product_name'][$i];
    
    }
    
    $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')";
    
    
    
    if(mysql_query($que))
    {
     header('Location:entry_history.php');
    }
     echo "data has been inserted into database";
    
    echo $que; exit;
    mysql_close($conn);
    
    ?>
    Last edited by Rabbit; Apr 29 '16, 10:23 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • helalreza
    New Member
    • Apr 2016
    • 9

    #2
    Code:
    your submit page
    
    
    
    <?php
    session_start();
    $date = $_SESSION['date'];             
    $channel_name = $_SESSION["channel_name"];?>
    
    <?php include("sql_conn.php");
    $num = $_POST['reza'];
    
    for($i>0;$i<=$num;$i++)
    {
    if(isset($_REQUEST["title$i"])){$title = $_REQUEST["title$i"];}
    if(isset($_REQUEST["start_time$i"])){$start_time = $_REQUEST["start_time$i"];}
    if(isset($_REQUEST["end_time$i"])){$end_time = $_REQUEST["end_time$i"];}
    if(isset($_REQUEST["duration$i"])){$duration = $_REQUEST["duration$i"];}
    if(isset($_REQUEST["sponsor$i"])){$sponsor = $_REQUEST["sponsor$i"];}
    if(isset($_REQUEST["peak_offpeak$i"])){$peak_offpeak = $_REQUEST["peak_offpeak$i"];}
    if(isset($_REQUEST["program_name$i"])){$program_name = $_REQUEST["program_name$i"];}
    if(isset($_REQUEST["sub_brand_name$i"])){$sub_brand_name = $_REQUEST["sub_brand_name$i"];}
    if(isset($_REQUEST["product_name$i"])){$product_name = $_REQUEST["product_name$i"];}
    
    $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')"; 
    $result=mysql_query($sql) or die(mysql_error());
    
    echo ($sql);
    }
    header('location:entry_history.php');
    
    
    ?>

    Comment

    • paulnevinthomas
      New Member
      • Feb 2016
      • 3

      #3
      dynamic rows insert into database using php

      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:2 0px; width:100px; height:100px; background:blue ;'></div>

      <? include("formco de.php"); ?>

      Comment

      Working...