Reputation: 1006
I have a form to upload data to multiple MySQL tables and part of it requires four identical sections to upload video location data to one table. All four video sections won't always be filled out sometimes they might all be left blank, then any number up to all four having data. Below is part of the form showing one video location upload, this needs to appear four times on the form:
<tr>
<td>Video 1:</td>
<td></td>
</tr>
<tr>
<td>Location</td>
<td><input type="text" name="location"></td>
</tr>
<tr>
<td>Park Video</td>
<td><input type="checkbox" name="park_id_video"
value="<?=$park_id?>">Park Video</td>
</tr>
<tr>
<td>Ride</td>
<td>
<select name="ride_id_video">
<option value="">Select Ride</option>
<?php echo $options ?>
</select>
</td>
</tr>
Below shows the typical insert query I use and will use for this. I have never had to insert multiple records like this though and am not sure about how to do it so all four sets of data can be uploaded in one query. And also so that if any of the four sets of data are blank a blank record won't get inserted.
$news_id = $pdo->lastInsertId() ;
$location = $_POST['headline'];
$park_id_video = $_POST['park_id_video'];
$ride_id_video = $_POST['ride_id_video'];
$query4 = $pdo->prepare('INSERT INTO tpf_videos
(location, park_id, ride_id, news_id)
VALUES (?, ?, ?, ?)');
$query4->execute(array($location, $park_id, $ride_id, $news_id));
From what I have read It needs an array but I don't know how to do this. Can somebody help me modify the form and query to make this work?
Thanks.
Upvotes: 2
Views: 476
Reputation: 13544
Just rename your form's field to like filed_name[]
and then using a loop you are able to perform multiple insert.
for ($i = 0; $i < count($_POST['location']); $i++){
//Do your insert code.
}
Upvotes: 3
Reputation: 122
<tr>
<td>Video 1:</td><td> </td>
</tr>
<tr>
<td>Location</td><td><input type="text" name="videos[1][location]"></td>
</tr>
<tr>
<td>Park Video</td><td><input type="checkbox" name="videos[1][park_id_video]" value="<?=$park_id?>">Park Video</td>
</tr>
<tr>
<td>Ride</td>
<td>
<select name="videos[1][ride_id_video]">
<option value="">Select Ride</option>
<?php echo $options ?>
</select>
</td>
</tr>
<tr>
<td>Video 2:</td><td> </td>
</tr>
<tr>
<td>Location</td><td><input type="text" name="videos[2][location]"></td>
</tr>
<tr>
<td>Park Video</td><td><input type="checkbox" name="videos[2][park_id_video]" value="<?=$park_id?>">Park Video</td>
</tr>
<tr>
<td>Ride</td>
<td>
<select name="videos[2][ride_id_video]">
<option value="">Select Ride</option>
<?php echo $options ?>
</select>
</td>
</tr>
$news_id = $pdo->lastInsertId() ;
$videos = $_POST['videos'];
$inserts = array();
$args = array();
foreach ($videos as $video) {
$vid_args = array(
$video['location'],
$video['park_id_video'],
$video['ride_id_video'],
$news_id
);
$inserts[] = "(?,?,?,?)";
$args = array_merge($args, $vid_args);
}
$sth = $pdo->prepare('INSERT INTO tpf_videos (location, park_id, ride_id, news_id) VALUES ' . implode(',', $inserts));
$sth->execute($args);
There may be a syntax error or two in this. I did not run it through PHP. But I believe this is what you were wanting to accomplish.
Upvotes: 1
Reputation: 30
<tr>
<td>Video 1:</td>
<td></td>
</tr>
<tr>
<td>Location</td>
<td><input type="text" name="location[1]"></td>
</tr>
<tr>
<td>Park Video</td>
<td><input type="checkbox" name="park_id_video[1]"
value="<?=$park_id?>">Park Video</td>
</tr>
<tr>
<td>Ride</td>
<td>
<select name="ride_id_video[1]">
<option value="">Select Ride</option>
<?php echo $options ?>
</select>
</td>
</tr>
for ($i = 0; $i < count($_POST['location']); $i++{
//Do your insert code.
}
Upvotes: 1