I have the below code. I have a form setup that has 10 input fields set up, 5 for column1 and 5 for column2.
try
{
$count = count($_POST['column1']);
$sql = 'INSERT INTO tablename (column1, column2) VALUES ';
for ($i=0; $i< $count; $i++)
{
$sql2 = '(' .$_POST['column1'][$i] . ', ' . $_POST['column2'][$i] . ')';
if ($i <$count - 1) {
$sql2 .= ',';
}
echo "$sql2 'indside'<br>";
}
echo "$sql2 'outside'<br>";
$sql3 = "$sql $sql2";
$pdo->exec($sql3);
}
catch (PDOException $e)
{
$output = 'Error: ' . $e->getMessage();
exit();
}
If I input 1,2,3,4,5,6,7,8,9,10 into my 10 input fields and hit submit I get
(1, 6), 'indside'
(2, 7), 'indside'
(3, 8), 'indside'
(4, 9), 'indside'
(5, 10) 'indside'
(5, 10) 'outside'
And 5 is inserted into column 1 and 10 is inserted into column 2 of my database.
What I really want is 5 seperate rows inserted so 1,6 in row 1, 2,7 in row 2 etc. But I can't work out how to take the value of $sql2 when it is in the for loop. Is this possible? Or is their another approach I can take to acheive this?