Jump to content

how to insert insert array


lukep11a
Go to solution Solved by AyKay47,

Recommended Posts

I am currently going through the process of converting all mysql code to mysqli and having some trouble with inserting an array from a prepared statement. This is how it used to work:

 

$apointsAry = array();
$apointsAry[] = "($id, $aid, $at_points)";

$query = "INSERT INTO team_points (fixture_id, team_id, points)
VALUES ".implode(', ', $apointsAry);
$result = mysql_query($query) or die("Query: {$query}<br>Error: ".mysql_error());

 

And this is what I have got so far after converting it:

 

$stmt = $mysqli->prepare("INSERT INTO team_points (fixture_id, team_id, points)
VALUES (?, ?, ?)");
			
$stmt->bind_param("iii", $id, $aid, $at_points);
$stmt->execute();
$stmt->close();

 

 

Obviously the above code only enters one row. I have searched around but can't find how to incorporate an array with bind_param or is it not possible? Any help would be greatly appreciated.

Link to comment
Share on other sites

  • Solution

Fill in the missing pieces of your logic before an appropriate answer can be formulated. An example array would help.

Storing multiple values in one array element is not how arrays are meant to be implemented. Something like:

 

 

$arr = array();
$arr[] = array($id, $aid, $at_points);

 

would be a better solution.

Edited by AyKay47
Link to comment
Share on other sites

Thanks to you both for your help, got it working with the code below so thanks for pointing me in the right direction:

 

$apointsAry = array();
$apointsAry[] = array($id, $aid, $at_points);

$stmt = $mysqli->prepare("INSERT INTO team_points (fixture_id, team_id, points)
VALUES (?, ?, ?)");
				
foreach ($apointsAry as $values) {
$stmt->bind_param("iii", $id, $aid, $at_points);
$id = $values[0];
$aid = $values[1];
$at_points = $values[2];
$stmt->execute();
}
$stmt->close();
Link to comment
Share on other sites

Oh yeah, I couldn't get it to work originally so I was trying it out inside the loop, but you obviously only need to specify that once which I didn't realise at first! Thanks for pointing it out, I've moved it back now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.