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

Recommended Answers

All 3 Replies

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

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.

Thanks,

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

Thanks a lot for your help.

Cheers,

QWaz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.