How would I go by inserting a assoc array into a MySQL database.
When I print_r($_POST), I get this:
Array
(
[first_name] => Array
(
[0] => James
[1] => Will
[2] => Jackie
)
[last_name] => Array
(
[0] => Bond
[1] => Smith
[2] => Chan
)
)
What I have in mind is something like this.
INSERT INTO actors(first_name, last_name) VALUES ('val1','val2'), ('val1','val2'), ('val1', ....
or something like this.
$sql="INSERT INTO actors(first_name, last_name)VALUES(:first_name,:last_name)";
$stmt=$db->prepare($sql);
$stmt->bindValue(':first_name', $_POST['first_name']);//Here I get a "Array to string conversion" error
$stmt->bindValue(':last_name', $_POST['last_name']);// Here I get a "Array to string conversion" error
foreach($_POST as $row){
$stmt->execute($row);//Here I get a "PDOStatement::execute() expects parameter 1 to be array, string given" error
}
I know there are many questions similar to this, I have viewed them all and tried the ones I could understand.
Here is the html form I'm trying to use to post to the database:
<form method="post">
<input type="text" name="first_name[]" placeholder="First Name"/>
<input type="text" name="last_name[]" placeholder="Last Name"/>
<input type="text" name="first_name[]" placeholder="First Name"/>
<input type="text" name="last_name[]" placeholder="Last Name"/>
<input type="text" name="first_name[]" placeholder="First Name"/>
<input type="text" name="last_name[]" placeholder="Last Name"/>
<input type="submit" name="add_actor" value="Submit"/>
</form>
$stmt->bindValue(':first_name', $_POST['first_name']);$stmt->bindValue(':last_name', $_POST['last_name']);.. Doesn't it work ?foreach($_POST['first_name'] as $key=>$value){$stmt->bindValue(':first_name', $value);$stmt->bindValue(':last_name', $_POST['last_name'][$key]);}:first_nameand not the value