Jump to content

[SOLVED] Inserting Multiple Values into MySQL


cheechm

Recommended Posts

I have three select fields. Say someone chooses the number "1" on one of them, "2" on the other and "3" on the other. Each of those values will also be paired with a field id.

EG:

ID----Select Value
01----1
02----2
03----3

the user might change it to something like this:

ID----Select Value
01----2
02----3
03----1

 

I need to know how to post that data (I know to use hidden field with the id) but I don't know how to insert multiple things.

 

EG:

 'UPDATE site SET select_value = '. $select_value .' WHERE id = ' .$id;

 

But how do I do that for all the options at once.

 

 

Thanks

 

 

Link to comment
Share on other sites

It is this code:

 

<form id="acp_menu" method="post" action="./index.php" name="menu">

<input type="hidden" name="action" value="save" />
	<input type="hidden" name="title" value="Home" />
	<input type="hidden" name="link" value="index.php?id=1" />
	<input type="hidden" name="id" value="1" />

	<input type="hidden" name="action" value="save" />
	<input type="hidden" name="title" value="Test" />
	<input type="hidden" name="link" value="Test" />
	<input type="hidden" name="id" value="9" />

	<input type="hidden" name="action" value="save" />
	<input type="hidden" name="title" value="Test" />
	<input type="hidden" name="link" value="Tsss" />
	<input type="hidden" name="id" value="11" />	</form>	

 

How do I update the data from that into one table all at once?

 

Thanks

Link to comment
Share on other sites

If each of your sets represents a row, you don´t. And you aren't inserting, you are updating.

 

Why do you wish to do it as a single statement? If you are concerned about data integrity while updating each row, wrap it with a transaction statement, which will make it atomic. (i.e. done as a unit, not as separate transactions)

 

I would assume that your ID is the primary key for each row.

 

Hope this helps.

Link to comment
Share on other sites

------

id (Primary Key)

---

title

---

link

---

order

------

 

Literally on the submit of this form

<input type="hidden" name="title" value="Home" />
<input type="hidden" name="link" value="index.php?id=1" />	
<input type="hidden" name="id" value="1" />


<input type="hidden" name="title" value="Test" />
<input type="hidden" name="link" value="Test" />
<input type="hidden" name="id" value="9" />


<input type="hidden" name="title" value="Test" />
<input type="hidden" name="link" value="Tsss" />
<input type="hidden" name="id" value="11" />

<input type="hidden" name="action" value="save" />

every single on of those values needs to be inputted where the id is whatever is posted.

So literally this@

------
[row]id (Primary Key)      | 1                      | 9         | 11
---
[row]title                      | Home                | Test     | Test
---
[row]link                       | index.php?id=1    | Test     | Tsss
---	
[row]order                     | NULL                 | NULL    | NULL
------

 

So how do I insert multiple values at the same time?

 

Thanks

 

Link to comment
Share on other sites

!!!!!!!!!!!!!!!!!!!!!

<?php

mysql_query("UPDATE `table` SET `field1` = '{$_POST[value1]}', `field2` = '{$_POST[value2]}', `field3` = '{$_POST[value3]}' WHERE `id` = '$id'");

?>

 

^^^^^^^^^^^^^^^^^^^^^^

^^^^^^^^^^^^^^^^^^^^^^

^^^^^^^^^^^^^^^^^^^^^^

::)

 

 

Link to comment
Share on other sites

Try this out

<?php
$count = count($_POST['action']);
for($i=0; $i<$count; $i++){
$sql = "UPDATE `table` SET `title` = {$_POST['title'][$i]}, `link` = {$_POST['link'][$i]} WHERE `id` = {$_POST['id'][$i]}";
echo $sql."<br />";
}
?>

 

Ray

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.