I need to insert arrayed values into a database using PDO, I have this code working but it doesn't use arrays (this was prototype code before I got arrayed input)
This is a snippet from the form for input showing the arrays (date isn't arrayed):
echo "<tbody><tr><td><input type='text' name='tipster[]'></td>";
echo "<td><input type='text' name='date' value=''>";
echo "<td><input type='text' name='tip[]'></td>";
echo "<td><input type='text' name='outcome[]'></td>";
echo "<td><input type='text' name='odds[]'></td>";
This is some of the code to process the submitted form:
$tipster = $_POST['tipster'];
$date= $_POST['date'];
$tip= $_POST['tip'];
$outcome= $_POST['outcome'];
$odds= $_POST['odds'];
$sql = "INSERT INTO bets (tipster,date,tip,outcome,odds) VALUES (:tipster,:date,:tip,:outcome,:odds)";
$stmt = $conn->prepare($sql);
$stmt->bindValue(':tipster', $tipster, PDO::PARAM_STR);
$stmt->bindValue(':date', $date, PDO::PARAM_STR);
$stmt->bindValue(':tip', $tip, PDO::PARAM_STR);
$stmt->bindValue(':outcome', $outcome, PDO::PARAM_STR);
$stmt->bindValue(':odds', $odds, PDO::PARAM_STR);
$stmt->execute();
How would I change my code to cater for arrayed values being submitted?
I want a new row input for each row on the form, so if the user enters 3 rows of data I want 3 new rows in the database. The form uses javascript to allow the user to enter as many rows as they like.
Thanks.