I am having real trouble trying to figure out how I can add multiple rows into a MySQL database at one time.
I have a form where the person selects a number from the drop down options. then I need to add that amount of new rows into a table as well as some other info with each row.
What I have so far is: But I assume it's completely wrong, as it's not working.
$id = NULL; $team = $_POST['team']; $round = $_POST['rounds']; // THIS IS THE value of the drop down, could be 5 could be 40. $time = '0'; $result = 'Undecided'; $i = 1; $i <= $round; $i++; /*** connect to db ***/ $conn = dbConnect('admin'); /*** set the error mode ***/ $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO round (id, team, round, time, result) VALUES (?,?,?,?,?)"; /*** our sql query ***/ $done = $conn->prepare($sql); /*** bind the params ***/ $done->bindParam(1, $id); $done->bindParam(2, $team); $done->bindParam(3, $i); $done->bindParam(4, $time); $done->bindParam(5, $result); /*** execute the query ***/ $done->execute();
I have also tried this:
$sql = str_repeat("INSERT INTO fixtures_results (id, team, round, time, result) VALUES (?,?,?,?,?)", $round);
Please help, Thanks heaps.