Jump to content

Multiple inserts from a variable that has comma seperated values


Recommended Posts

This is a step up for me as I have only ever done single inserts to MySQL. I now require to do multiple inserts from a form I am building. I didn't realize how hard this was going to be!

 

What I am trying to do

Add a single or multiple database entry that holds a numeric ID and other info in each record. The ID's are selected from a dynamic list using tick boxes that then POST to a page that has the INSERT function.

 

Here is the form code

<form id="message" name="message" method="post" action="messages-send.php">
              <?php do { ?>
                <table width="451" border="0" cellpadding="3">
                  <tr>
                    <td width="36">
                      <input name="group[]" type="checkbox" value="<?php echo $row_groups['group_id']; ?>" />
                   </td>
                    <td width="397"><?php echo $row_groups['group_name']; ?></td>
                  </tr>
                </table>
<?php } while ($row_groups = mysql_fetch_assoc($groups)); ?>
<input name="button" type="submit" class="style1" id="button" value="Select & continue" /> 
</form>

 

What I can do

On the receiving page, 'messages-send.php', I can use the POST data to query the db to get the correct record information using the chosen ID's ready to insert into a 'messages' table. The messages can then be recalled using the ID's by the users.

 

Implode the posted data into a coma separated array

$selected = "-0"; 
if (isset($_POST['group'])) {
$selected = $_POST['group'];
}
if (is_array($selected)){   
$selected = implode(",", $selected);
} else {
$selected = $selected; // no array -> print single value
}

 

Then query the db to get more information for the relevant the ID's

 

"SELECT group_name, groups.group_id FROM groups WHERE groups.group_id IN ($selected)"

 

 

.... :-[ and this is where I am stuck!. I have Googled for 3 hours and can't find the solution to do multiple insert after a query. I know that the statement looks like the following and should be used with a loop function but I can't get my head around the code. Can anyone offer advice?

 

INSERT INTO x (a,b)

VALUES

('1', 'one'),

('2', 'two'),

('3', 'three')

 

Why do you need to query the db for information regarding the selected IDs? You should only need to save records with the selected IDs since they will point back to the relevant records anyway.

 

However, to answer your questin directly (even though it is not the right approach in this situation), you don't need to so a select query and then multiple inserts. You could simply do an insert using a select as the values

 

Example:

INSERT INTO table1
(field1, field2)
VALUES (SELECT fieldA, fieldB FROM table2 WHERE something='$value')

Thanks for your reply.

 

I now realize I don't need to do a query, I got a bit lost with it all through tiredness last night. I just need to tackle the multiple record insert using a loop I think.

 

To simply it all....  I just need to insert 3 records if the user selects 3 text boxes etc...

 

That's why I think I need a loop function on the insert. Just don't know how...

After some more Googling and perseverance I have it working  :D

 

I just needed to understand how to post an Array and than do a multiple row insert.

 

Heres the code that I am using.

 

<?php  // Do multiple record insert loop into db from Array post values
$field1 = $_POST['group_id'];
$field2 = $_POST['group_name'];

$values="";
for ($i=0;$i<count($field1);$i++)
{
$values.="('$field1[$i]','$field2[$i]'),";
}
$values=substr($values,0,-1); // to remove last comma
$query="INSERT INTO Groups (group_id, group_name) VALUES $values".";";
$result=mysql_query($query); ?>

 

 

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.