Jump to content

script not adding all data to db


runnerjp

Recommended Posts

I have added a function so i can add more textboxes to the form if needed

<script language="javascript1.2" type="text/javascript"> 
function addRow() { 

          var tbl = document.getElementById('thetable');  
          var lastRow = tbl.rows.length;  
          var row = tbl.insertRow(lastRow);  
            
          var cell1 = row.insertCell(0);  
                   
                  var str1 = document.createTextNode("Distance: "); 
                  cell1.appendChild(str1); 
                  var input = document.createElement('input'); 
          input.name = 'distance[]' + lastRow;  
          input.id = 'distance' + lastRow;  
                  input.style.width = "187px" 
          cell1.appendChild(input);  
                   
                  var theBR = document.createElement('br'); 
                  cell1.appendChild(theBR); 
            
     var str1 = document.createTextNode("Time: "); 
                  cell1.appendChild(str1);
                  var input2 = document.createElement('input'); 
input2.value = '00'; 
          input2.name = 'hours[]' + lastRow;
  
          input2.id = 'hours' + lastRow;  
                  input2.style.width = "25px" 
                  input2.setAttribute('maxlength', 1); 
          cell1.appendChild(input2);  
                  var str2 = document.createTextNode(" Hours"); 
                  cell1.appendChild(str2); 

          var input3 = document.createElement('input');  
          input3.value = '00';  
input3.name = 'min[]' + lastRow; 

          input3.id = 'min' + lastRow;  
                  input3.style.width = "25px" 
          cell1.appendChild(input3); 
                  var str3 = document.createTextNode(" Minutes"); 
                  cell1.appendChild(str3); 
                   
          var input4 = document.createElement('input');  
        input4.value = '00';
  input4.name = 'seconds[]' + lastRow;
   
          input4.id = 'seconds' + lastRow;  
                  input4.style.width = "25px" 
          cell1.appendChild(input4);  
                  var str4 = document.createTextNode(" Seconds"); 
                  cell1.appendChild(str4); 
}  
                   
function delRow() { 
          var tbl = document.getElementById('thetable');  
          var lastRow = tbl.rows.length;  
          if (lastRow > 2) tbl.deleteRow(lastRow - 1);  
}  

</script> 

 

The whole form is below

 

<link rel = "stylesheet" href = "http://www.runningprofiles.com/dad/Form.Send/demo.css" type = "text/css"/>

<script type = "text/javascript" src = "http://www.runningprofiles.com/dad/mootools.js"></script>

<script type = "text/javascript" src = "http://www.runningprofiles.com/dad/Form.Send/demo.js"></script>
<?php

$getthreads = 
                                "Select * from dad where id='".$_GET["id"]."' ORDER BY date ASC"; 

                            $getthreads2=mysql_query($getthreads) or die("Could not get threads"); 

                            while ($getthreads3=mysql_fetch_array($getthreads2)) 
                                { 
?>
<center>
<form id = "myForm" action = "include/addlog.php" method = "post">   <table style="BORDER-COLLAPSE: collapse">
     <tbody>
       <tr>
         <td>
           <p align="right">Date:</p></td>
         <td>
           <p align="left"><?php echo $getthreads3['date'];?></p>
<input type="hidden" name="date" value="<?php echo $getthreads3['date'];?>" >


</td>
      </tr>
       <tr>
         <td>
<br />
           <p align="right">Am Run:</p></td>
         <td>
           <p align="left"><br />
            <input style="WIDTH: 186px;" value="<?php echo $getthreads3['am'];?>" name="am" /></p></td>
      </tr>
       <tr>
         <td>
           <p align="right">Duration:</p></td>
         <td>
           <p align="left">
            <input maxlength="2" style="WIDTH: 20px;" name="hoursam" /> Hours<input maxlength="2" style="WIDTH: 20px;" name="minam" /> Minutes<input maxlength="2" style="WIDTH: 25px; " name="secam" />Seconds</p></td>
      </tr>
       <tr>
         <td><br />
           <p align="right">Pm Run:</p></td>
         <td>
           <p align="left"><br />
            <input style="WIDTH: 186px;" value="<?php echo $getthreads3['pm'];?>" name="pm" /></p></td>
      </tr>
       <tr>
         <td>
           <p align="right">Duration:</p></td>
         <td>
           <p align="left">
            <input maxlength="2" style="WIDTH: 25px; " name="hourspm" /> Hours<input maxlength="2" style="WIDTH: 25px;" name="minpm" /> Minutes<input maxlength="2" style="WIDTH: 25px;" name="secpm" /> Seconds</p></td>
      </tr>
             <tr>
         <td><br />
           <p align="right">Weather:</p></td>
         <td>
           <p align="left"><br />
            <input style="WIDTH: 187px;" name="weather" /></p></td>
      </tr>
       <tr>
         <td><br />
           <p align="right">Comments:</p></td>
         <td>
           <p align="left"><br />
            <input style="WIDTH: 187px;" name="comments" /></p></td>
      </tr>
       <tr>
         <td>
     </td>
         <td>
           <p align="center"> </p></td>
      </tr>
    </tbody>

  </table>
<table id="thetable" align="center">
       <tr> 
<td>
</td> 
</tr> 
</table> 
<script language="javascript1.2" type="text/javascript"> 
function addRow() { 

          var tbl = document.getElementById('thetable');  
          var lastRow = tbl.rows.length;  
          var row = tbl.insertRow(lastRow);  
            
          var cell1 = row.insertCell(0);  
                   
                  var str1 = document.createTextNode("Distance: "); 
                  cell1.appendChild(str1); 
                  var input = document.createElement('input'); 
          input.name = 'distance[]' + lastRow;  
          input.id = 'distance' + lastRow;  
                  input.style.width = "187px" 
          cell1.appendChild(input);  
                   
                  var theBR = document.createElement('br'); 
                  cell1.appendChild(theBR); 
            
     var str1 = document.createTextNode("Time: "); 
                  cell1.appendChild(str1);
                  var input2 = document.createElement('input'); 
input2.value = '00'; 
          input2.name = 'hours[]' + lastRow;
  
          input2.id = 'hours' + lastRow;  
                  input2.style.width = "25px" 
                  input2.setAttribute('maxlength', 1); 
          cell1.appendChild(input2);  
                  var str2 = document.createTextNode(" Hours"); 
                  cell1.appendChild(str2); 

          var input3 = document.createElement('input');  
          input3.value = '00';  
input3.name = 'min[]' + lastRow; 

          input3.id = 'min' + lastRow;  
                  input3.style.width = "25px" 
          cell1.appendChild(input3); 
                  var str3 = document.createTextNode(" Minutes"); 
                  cell1.appendChild(str3); 
                   
          var input4 = document.createElement('input');  
        input4.value = '00';
  input4.name = 'seconds[]' + lastRow;
   
          input4.id = 'seconds' + lastRow;  
                  input4.style.width = "25px" 
          cell1.appendChild(input4);  
                  var str4 = document.createTextNode(" Seconds"); 
                  cell1.appendChild(str4); 
}  
                   
function delRow() { 
          var tbl = document.getElementById('thetable');  
          var lastRow = tbl.rows.length;  
          if (lastRow > 2) tbl.deleteRow(lastRow - 1);  
}  

</script> 

<table style="WIDTH: 100%; BORDER-COLLAPSE: collapse"> 
     <tr> 
       <td> 
       <p align="center"> 
          <input type="button" value="Add Interval" onclick="addRow()" /> 
          <input type="button" value="Subtract Interval" onclick="delRow()" /> 
                </p> 
       </td> 
    </tr> 
</table> 

    <center>
        <input type = "submit" name = "button" id = "submitter"/></center>

    <span class = "clr"> <!-- spanner --></span>

    </div>
</form>

<?php } ?>
<center>
    <div id = "log">
        <center><h3>Added diary</h3>

            <div id = "log_res"> <!-- spanner --></center>
    </div>

    </div>
</center>

 

Now when i come to try add the data to the db it only adds the 1st result from the loop...why??

 

<?php 
include '../../settings.php';

$mydate=$_POST['date'];
   $am=mysql_real_escape_string($_POST['am']);
      $minam=mysql_real_escape_string($_POST['minam']);
$hoursam=mysql_real_escape_string($_POST['hoursam']);
$secam=mysql_real_escape_string($_POST['secam']);
   $pm=mysql_real_escape_string($_POST['pm']);
      $minpm=mysql_real_escape_string($_POST['minpm']);
$hourspm=mysql_real_escape_string($_POST['hourspm']);
$secpm=mysql_real_escape_string($_POST['secpm']);
$weather=mysql_real_escape_string($_POST['weather']);
$comments=mysql_real_escape_string($_POST['comments']);

if($am != ""){$amtime = $hoursam . ":" . $minam. ".". $secam;}
if($pm != ""){$pmtime = $hourspm . ":" . $minpm. ".". $secpm;}
mysql_query("INSERT INTO daddiary (`date`,`am`,`pmtime`,`pm`,`amtime`,`weather`,`comment`) VALUES ('$mydate','$am' , '$amtime','$pm','$pmtime' , '$weather','$comments') ") or die(mysql_error());



$i=-1;
$limit=count($_POST['date']);
while(++$i<$limit)
{
if ($_POST['distance'] != "") {

      $mydate=$_POST['date'];
   $distance=mysql_real_escape_string($_POST['distance'][$i]);
      $min=mysql_real_escape_string($_POST['min'][$i]);
$hours=mysql_real_escape_string($_POST['hours'][$i]);
$sec=mysql_real_escape_string($_POST['seconds'][$i]);
  
$time = $hours . ":" . $min. ".". $sec;
echo $time;

   mysql_query("INSERT INTO dadreps (`date`,`distance`,`time`) VALUES ('$mydate','$distance' , '$time') ") or die(mysql_error());

}
}

  echo 'Your diary entry has been added';

?>

Link to comment
https://forums.phpfreaks.com/topic/195547-script-not-adding-all-data-to-db/
Share on other sites

but im only wanting these to go through the array

 

   var cell1 = row.insertCell(0);  
                   
                  var str1 = document.createTextNode("Distance: "); 
                  cell1.appendChild(str1); 
                  var input = document.createElement('input'); 
          input.name = 'distance[]' + lastRow;  
          input.id = 'distance' + lastRow;  
                  input.style.width = "187px" 
          cell1.appendChild(input);  
                   
                  var theBR = document.createElement('br'); 
                  cell1.appendChild(theBR); 
            
     var str1 = document.createTextNode("Time: "); 
                  cell1.appendChild(str1);
                  var input2 = document.createElement('input'); 
input2.value = '00'; 
          input2.name = 'hours[]' + lastRow;
  
          input2.id = 'hours' + lastRow;  
                  input2.style.width = "25px" 
                  input2.setAttribute('maxlength', 1); 
          cell1.appendChild(input2);  
                  var str2 = document.createTextNode(" Hours"); 
                  cell1.appendChild(str2); 

          var input3 = document.createElement('input');  
          input3.value = '00';  
input3.name = 'min[]' + lastRow; 

          input3.id = 'min' + lastRow;  
                  input3.style.width = "25px" 
          cell1.appendChild(input3); 
                  var str3 = document.createTextNode(" Minutes"); 
                  cell1.appendChild(str3); 
                   
          var input4 = document.createElement('input');  
        input4.value = '00';
  input4.name = 'seconds[]' + lastRow;
   
          input4.id = 'seconds' + lastRow;  
                  input4.style.width = "25px" 
          cell1.appendChild(input4);  
                  var str4 = document.createTextNode(" Seconds"); 
                  cell1.appendChild(str4); 

 

and as you can see they are allready in an array ver.

 

Or do i need them ALL to be like this to have just these work?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.