-3

I am trying to post some table rows into a database. I have created a while loop and it keeps posting my first HTML table row to the database. The desired behaviour is that every HTML table row is posted to the DB.

<?PHP
$con=mysqli_connect("localhost","root","","freoplanner");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$count=1;
$shiftDate='dp'.$count;
$shiftTime='shiftTime'.$count;
$shiftAantal='shiftAantal'.$count;
while(isset($_POST[$shiftDate]) && !empty($_POST[$shiftDate])){
    $sql="INSERT INTO shifts (datum, tijd, aantal)
        VALUES
        ('$_POST[$shiftDate]','$_POST[$shiftTime]','$_POST[$shiftAantal]')";
    $count++;
    mysqli_query($con,$sql);
}
header("location:shiftstoevoegen.php");
?>
Oswald
  • 31,254
  • 3
  • 43
  • 68
NBISDB
  • 9
  • 2
  • While the word *while* can be used in the english language to effectively denote on of many alternatives at a single instance in time, this is not the case in programming languages. In programming languages, *while* has the effect of repeatedly checking whether a condition holds and repeatedly executing something until the condition no longer holds. – Oswald Nov 02 '13 at 12:34
  • [Your code is vulnerable to SQL injection attacks](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – PeeHaa Nov 02 '13 at 12:34

1 Answers1

1

You need to reinitialise the following inside the loop:

$shiftDate='dp'.$count;
$shiftTime='shiftTime'.$count;
$shiftAantal='shiftAantal'.$count;

currently, they will always be using $count = 1

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • So when the while loop is triggered again it won't update the variables outside of it? – NBISDB Nov 02 '13 at 12:35
  • No. You should re-initialize the variables again inside the loop body @NiekBakermans – hjpotter92 Nov 02 '13 at 12:37
  • 1
    Although you might think above did the trick don't take the irresponsible route and say "I have my answer" while you still have that huge security hole in your code. – PeeHaa Nov 02 '13 at 12:41
  • I am fixing that as we speak via the link you commented. – NBISDB Nov 02 '13 at 13:07