0

Hi,

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);

No luck,

Please help, Thanks heaps.

Cheers,

Qwaz

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by QWaz
0

I think this is what you are looking for:

for($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();
}
0

Try this.

$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';
	
			/*** 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);
				
			for ($i = 1; $i <= $round; $i++)
			{

			     /*** execute the query ***/
			     $done->execute();
			}

Also, since the only thing changing in your insert is $i, you can bind just that and insert the rest right into your sql.

Edited by LethargicCoder: n/a

0

Thanks,

I tried both of these ways and they both worked great.

Thanks a lot for your help.

Cheers,

QWaz

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.