0

I have 15 rows of the same data to be inserted repeatedly, is there a way of handling it rather than repeating 15 individual queries?

My current code for 1 row looks like this

$FirstName1=mysqli_real_escape_string($con,$_POST['FirstName1']);
$Surname1=mysqli_real_escape_string($con,$_POST['Surname1']);
$Position1=mysqli_real_escape_string($con,$_POST['Position1']);
$Value1=mysqli_real_escape_string($con,$_POST['Value1']);

mysqli_query($link,"INSERT INTO Players (FirstName, Surname, Position, Value) VALUES ('$FirstName1', '$Surname1', '$Position1', '$Value1')") or die(mysqli_error($link));

Is there a way of running a foreach to first check the data exists, then loop through and run the insert? The numbers for each field, so "FirstName1" for example will run from 1-15.

BN83
  • 902
  • 3
  • 9
  • 29
  • 1
    Use [batch insert](http://stackoverflow.com/questions/5526917/how-to-do-a-batch-insert-in-mysql) – M Khalid Junaid Apr 20 '15 at 12:51
  • can be done in myriad of ways, either set inputs to a grouping array ` – Kevin Apr 20 '15 at 12:52
  • @MKhalidJunaid would I still need to real escape each field manually there? – BN83 Apr 20 '15 at 12:54
  • @Ghost Thanks for that but I don't completely understand. I'm competent enough bringing in individual values and inserting them but i've never really done a large insert and I don't really understand how to achieve what you suggest. If I was to group for example FirstName1, Surname1 etc in to name="Row1[]" how would I then insert all of the data in a foreach? Apologies if that sounds dumb! – BN83 Apr 20 '15 at 12:57
  • @BN83 many answers will also depend on how the form was constructed. by the way, if really want to insert 15 duplicated rows? if you don't mind looping the same query 15 times, then just use a for or while loop. if you're inserting 15 different values, you need another approach – Kevin Apr 20 '15 at 13:02
  • @Ghost It's the same query but different values. – BN83 Apr 20 '15 at 13:06
  • @BN83 then you need to rephrase your question, its a little bit misleading. you should add that _15 different set of values_ i thought you wanted to insert 15 duplicated rows. – Kevin Apr 20 '15 at 13:16

2 Answers2

2

You may use as follows.

    $message = "";    
    for($i=1;$i<=15;$i++){

            $nameElemenet = "FirstName".$i;
            $FirstName = mysqli_real_escape_string($con,$_POST[$nameElemenet]);

            $surNameElemenet = "Surname".$i;
            $Surname = mysqli_real_escape_string($con,$_POST[$surNameElemenet]);

            $positionElemenet = "Position".$i;
            $Position = mysqli_real_escape_string($con,$_POST[$positionElemenet]);

            $valueElemenet = "Value".$i;
            $Value = mysqli_real_escape_string($con,$_POST[$valueElemenet]);

            if($nameElemenet !="" && $Surname !="" && $positionElemenet !="" && $Value !="" ){
                mysqli_query($link,"INSERT INTO Players (FirstName, Surname, Position, Value) VALUES ('$FirstName', '$Surname', '$Position', '$Value')") or die(mysqli_error($link));
            }else{
                $message .= "All filed values are required.";
            }
        }
0

Using while loop :

<?php
$x = 1;

while($x <= 15) {
    mysqli_query($link,"INSERT INTO Players (FirstName, Surname, Position, Value) VALUES ('$FirstName1', '$Surname1', '$Position1', '$Value1')") or die(mysqli_error($link));
    $x++;
}
?>
mysqlrockstar
  • 2,536
  • 1
  • 19
  • 36