0

I am trying to submit a table full of information at once, and cannot find the answer. It has no problem submitting one set of information to the DB, but i need to insult between 1 and 50 of the same value at once. Here is an example:

We are recording lap times, therefore we need to be able to fill in a table on a web app, with loads of names, and event times. Therefore, we will need to add loads of SQL entries at once.

I have tried just copy and pasting the form info, but this results in a blank set of data in phpmyadmin.

Here is the code:

<?php


$host = "localhost";
$databasename = "pe_results";
$databaseusername = "root";
$databasepassword = "";

$conn = mysql_connect("$host", "$databaseusername", "$databasepassword"); 
mysql_select_db("$databasename", $conn); 

        if (isset($_POST['Name'])) { 
        $Name = $_POST['Name'];
        }
        if (isset($_POST['Short'])) { 
        $Short = $_POST['Short'];
        }
        if (isset($_POST['Med'])) { 
        $Med = $_POST['Med'];
        }
        if (isset($_POST['Long'])) { 
        $Long = $_POST['Long'];
        }
        if (isset($_POST['VLong'])) { 
        $VLong = $_POST['VLong'];
        }
        if (isset($_POST['Extreme'])) { 
        $Extreme = $_POST['Extreme'];
        }
        if (isset($_POST['LJump'])) { 
        $LJump = $_POST['LJump'];
        }
        if (isset($_POST['HJump'])) { 
        $HJump = $_POST['HJump'];
        }
        if (isset($_POST['Shotputt'])) { 
        $Shotputt = $_POST['Shotputt'];
        }
        if (isset($_POST['Discuss'])) { 
        $Discuss = $_POST['Discuss'];
        }
        if (isset($_POST['Javelin'])) { 
        $Javelin = $_POST['Javelin'];
        }
        if (isset($_POST['Date'])) { 
        $Date = $_POST['Date'];
        }
        if (isset($_POST['Year'])) { 
        $Year = $_POST['Year'];
        }



           $i = count($Name);

for ($i=0;$i<10;$i++) {
$n = $Name[$i];
$s = $Short[$i];
$me = $Med[$i];
$lng = $Long[$i];
$slng = $VLong[$i];
$ext = $Extreme[$i];
$ljump = $LJump[$i];
$hjump = $HJump[$i];
$shot = $Shotputt[$i];
$disc = $Discuss[$i];
$jav = $Javelin[$i];
$date = $Date[$i];
$year = $Year[$i];

//and so on with more variable...

$sql="INSERT INTO results_main (`Name`, `Short`, `Med`, `Long`, `Vlong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`)
            VALUES ('$n', '$s', '$me', '$lng', '$slng', '$ext', '$ljump', '$hjump', '$shot', '$disc', '$jav', '$date', '$year')";

}


    $result = mysql_query($sql) or die(mysql_error ());;
        if($result){
echo"<br/>Everythings been saved";
echo "<BR>";
echo "<a href='index.php'>Back to the main page</a>";
}

else {

echo $result = mysql_query($sql,$conn) or die (mysql_error ());
}

// close connection 
mysql_close($conn);
?>

Also find HTML below

<?php
// Create connection
$con=mysqli_connect("127.0.0.1","root","","pe_results");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Uh oh, tell chris that " . mysqli_connect_error() . "NO DATA WILL BE SAVED";
  }
?>
    <a href="results.php"><div id="1" >Results</div></a>
    <a href="record.php"><div id="2">Record</div></a>
    <a href="overview.php"><div id="3">Overview</div></a>
    <div id="4">Blank</div>
   <form name="input_primary" action="process.php" method="post">

    <font color="#FFFFFF"><strong>Date:</strong></font><input name="Date" type="date" />

   <font color="#FFFFFF"><strong>Year:</strong></font><select name="Year">
        <option value="7">Year 7</option>
        <option value="8">Year 8</option>
        <option value="9">Year 9</option>
        <option value="10">Year 10</option>
        <option value="11">Year 11</option>
        <option value="12">Year 12</option>
        <option value="13">Year 13</option>
       </select>
       <input type="submit" value="Save results!" name="submit" />


<table width="200" border="1px solid black" id="maintab">
  <tr>
    <th scope="col">Name</th>
    <th scope="col">100m</th>
    <th scope="col">200m</th>
    <th scope="col">400m</th>
    <th scope="col">800m</th>
    <th scope="col">1500m</th>
    <th scope="col">Long Jump</th>
    <th scope="col">High Jump</th>
    <th scope="col">Shotputt</th>
    <th scope="col">Discus</th>
    <th scope="col">Javelin</th>
  </tr>
  <tr>
    <td>
        <input name="Name" type="text" />
    </td>
    <td> 
        <input name="Short" type="text" size="10px" />
    </td>
    <td>
         <input name="Med" type="text" size="10px" />
    </td>
    <td> 
        <input name="Long" type="text" size="10px" />
    </td>
    <td>
         <input name="VLong" type="text" size="10px" />
    </td>
    <td>
         <input name="Extreme" type="text" size="10px" />
    </td>
    <td>
         <input name="LJump" type="text" size="10px" />
    </td>
    <td>
         <input name="HJump" type="text" size="10px" />
    </td>
    <td>
         <input name="Shotputt" type="text" size="10px" />
    </td>
    <td>
         <input name="Discuss" type="text" size="10px" />
    </td>
    <td>
         <input name="Javelin" type="text" size="10px" />
    </td>
  </tr>
21
  • Do you mean you could have 50 identical values in 50 different rows? Commented Apr 14, 2013 at 23:00
  • See this. Commented Apr 14, 2013 at 23:01
  • nope, its a little hard to explain, the sites on a LAN otherwise id link you. Its basically like filling out an excel spreadsheet then submitting, so after an event, there would be 20 odd names, 20 odd 100m times, 20 odd 200m times etc etc. There are no errors upon submitting and it writes to the DB, however all fields are blank. Commented Apr 14, 2013 at 23:01
  • Fresh prince, mind dumming it down / posting an example relative to my DB? My knowledge is so poor at the mo after leaving the world of php / sql for a few years Commented Apr 14, 2013 at 23:02
  • @ChrisConorDavern - aha ok. I get it, it seems like the values of the variables arent set then? What is the value of $Name`, $short etc? Commented Apr 14, 2013 at 23:02

3 Answers 3

1

If I understood your Question right what you wanna do is name the fields with a [] So

<select name="Year">

Becomes

<select name="Year[]">

Then you'll be able to loop through the submitted values, example:

foreach($_POST['Year'] as $k=>$v){
 echo $v." - This is the value";
 echo $k." - This is the key";
}

EDIT:

(string)$insert;
if(is_array($_POST['Year'])){
    foreach($_POST['Year'] as $k=>$v){  
         $insert .= "(".$_POST['Name'][$k].", ".$_POST['Short'][$k].", ".$_POST['Med'][$k].", ".$_POST['Long'][$k].", ".$_POST['VLong'][$k].", ".$_POST['Extreme'][$k].", ".$_POST['LJump'][$k].", ".$_POST['HJump'][$k].", ".$_POST['Shotputt'][$k].", ".$_POST['Discuss'][$k].", ".$_POST['Javelin'][$k].", ".$_POST['Date'][$k].", ".$_POST['Year'][$k]."),"; 
    }
     $insert = substr_replace($insert ,0,-1);
}else{
    $insert .= "($_POST['Name'], $_POST['Short'], $_POST['Med'], $_POST['Long'], $_POST['VLong'], $_POST['Extreme'], $_POST['LJump'], $_POST['HJump'], $_POST['Shotputt'], $_POST['Discuss'], $_POST['Javelin'], $_POST['Date'], $_POST['Year'])"; 
}
$sql="INSERT INTO results_main 
(`Name`, `Short`, `Med`, `Long`, `VLong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`)
VALUES 
".$insert;

This should do the trick to get everything into only one query instead of doing multiple queries for each row

Sign up to request clarification or add additional context in comments.

6 Comments

no im afraid not, the year function works absolutely fine, the issue is writing more than one row at once
That's what im saying, with my solution you can have 10000000 select, then on the server side you loop through the results. I added a sample for you to see
ok, could you help me further as to where exactly i would add this into my php? and can i just substitute "Year" for the other values? Also what are values an keys? are keys the id's for the sql db and values what i want to insert?
@ChrisConorDavern - keys are like "identifier" for elements in an array. It could be like arr[0], arr[1], arr[2] - in this case keys are 0,1 and 2. It could also be like arr['tom'], arr['jerry'], arr['fool'] - in this case keys are tom, jerry and fool.
ok so could you give me a full on example of how to implement this? Im not sure how the echo would come into play?
|
0

Add brackets [] to ALL your names in your html-form (where it makes sense). The bracket [] tells that elements are treated as dynamic arrays.

In your html-form, do something like this:

<form name="input_primary" action="process.php" method="post">

    <font color="#FFFFFF"><strong>Date:</strong></font><input name="Date" type="date" />
   <font color="#FFFFFF"><strong>Year:</strong></font><select name="Year">
        <option value="7">Year 7</option>
        <option value="8">Year 8</option>
        <option value="9">Year 9</option>
        <option value="10">Year 10</option>
        <option value="11">Year 11</option>
        <option value="12">Year 12</option>
        <option value="13">Year 13</option>
       </select>
       <input type="submit" value="Save results!" name="submit" />


<table width="200" border="1px solid black" id="maintab">
  <tr>
    <th scope="col">Name</th>
    <th scope="col">100m</th>
    <th scope="col">200m</th>
    <th scope="col">400m</th>
    <th scope="col">800m</th>
    <th scope="col">1500m</th>
    <th scope="col">Long Jump</th>
    <th scope="col">High Jump</th>
    <th scope="col">Shotputt</th>
    <th scope="col">Discus</th>
    <th scope="col">Javelin</th>
  </tr>
<?php
//Repeat element inside loop 10 times
for($i=0;$i<10;$i++) {
?>
  <tr>
    <td>
        <input name="Name[]" type="text" />
    </td>
    <td> 
        <input name="Short[]" type="text" size="10px" />
    </td>
    <td>
         <input name="Med[]" type="text" size="10px" />
    </td>
    <td> 
        <input name="Long[]" type="text" size="10px" />
    </td>
    <td>
         <input name="VLong[]" type="text" size="10px" />
    </td>
    <td>
         <input name="Extreme[]" type="text" size="10px" />
    </td>
    <td>
         <input name="LJump[]" type="text" size="10px" />
    </td>
    <td>
         <input name="HJump[]" type="text" size="10px" />
    </td>
    <td>
         <input name="Shotputt[]" type="text" size="10px" />
    </td>
    <td>
         <input name="Discuss[]" type="text" size="10px" />
    </td>
    <td>
         <input name="Javelin[]" type="text" size="10px" />
    </td>
  </tr>
<?php
}
?>
</table>
</form>

And in your PHP code do something like this:

//Create initial query for sql-insert.
$sql="INSERT INTO results_main (`Name`, `Short`, `Med`, `Long`, `Vlong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`) VALUES ";


$date = $Date; //Do like this if date-element is only occur once (at top) (then no need for brackets)
$year = $Year; //Do like this if year-element is only occur once (at top) (then no need for brackets)

$cnt = count($Name);    
for ($i=0;$i<$cnt;$i++) {
    //Use mysql_escape_string to escape strings (when needed) BEFORE adding to the $sql.
    $n = $Name[$i];
    $s = $Short[$i];
    $me = $Med[$i];
    $lng = $Long[$i];
    $slng = $VLong[$i];
    $ext = $Extreme[$i];
    $ljump = $LJump[$i];
    $hjump = $HJump[$i];
    $shot = $Shotputt[$i];
    $disc = $Discuss[$i];
    $jav = $Javelin[$i];

    //Insert each row separated with a comma
    $sql .= "('$n', '$s', '$me', '$lng', '$slng', '$ext', '$ljump', '$hjump', '$shot', '$disc', '$jav', '$date', '$year'),";
}

$useQuery = substr($sql, 0, -1); //Delete last comma
$result = mysql_query($useQuery); //Do the actual insert

then of course, read up on PDO instead of using mysql_query etc. Do this soon because mysql_* functions like this are deprecated and will be removed in the future. PDO is a better alternative because it's oop, it's safer and more flexible. Start from here... http://www.php.net/manual/en/intro.pdo.php

7 Comments

thanks, its late where i am so i will try this tomorrow and let you know how i get on
You're welcome! its late here also. about 2 in the morning... :-)
name="[]" this amongst other thing wont work. and this thing with reapplying all post variable to another var is unnecessary in this case, its a waste of memory and processing, and running a query for every row goes without saying a stupid idea since it can all be run in one, and will save computer power
Hi thanks for the support. I keep getting the following error now - - -Notice: Uninitialized string offset: 1 in C:\xampp\htdocs\process.php on line 66 - This error repeats itself for lines 65 ,66 and 76. I have updated my question with the new code i have
@Breezer - sorry for the bracket-thing. I was tired yesterday, it's editiet now. Yes, I know it wasn't the best solution - I was just trying to demonstrate how you loop through an array and do something with it as stated in the answer.
|
0

Storing your data across multiple arrays is unsafe because its so easy to get out of order and cause errors. Instead store data into an associative array like this:

$people = array();
$people[] = array('Name' => $name, 'Short' => $short ....);

The above code creates and array of arrays and the inner arrays are associative arrays so you can use the following syntax to get the name of the first person:

$people[0]['Name'];

Everything stays together and if certain values arn't set for certain people it will not seep into the other peoples records.

I see you were using POST variables. The above associative array can be easily passed around with ajax (if thats what your using, just an option) by using json_encode and json_decode. This helps preserve data structures when your passing it through multiple languages.

Lastly to insert a large number of records I would suggest a PDO statement as one ofthe easiest and cleanest way to do this. Check out a great tutorial here http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

Hope this helps, good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.