Updating multiple columns in MySQL DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
A7x
Forum Newbie
Posts: 10
Joined: Thu Mar 03, 2011 6:07 am

Updating multiple columns in MySQL DB

Post by A7x »

Hi, the topic says it all here's my code:

Code: Select all

if (isset($_POST['go']))
{
	foreach ($_POST['uniqueID'] as $id)
	$update = mysql_query("UPDATE mytable SET status='".$_POST['select']."' WHERE id='".$id."'");
}
This code is not working at all.
$_POST['select'] is a dropdown list contains 3 values. I want to take one of the selected values and put this value into a column in mysql when submit button is pressed. FYI: I am updating multiple columns in my DB.

$_POST['uniqueID'] is a hidden field with value $row['id'] pulled from database.

I have also tried to use this code:

Code: Select all

if (isset($_POST['go']))
{
	$update = mysql_query("UPDATE mytable SET status='".$_POST['select']."' WHERE id='".$_POST['uniqueID']."'");
	
}
This code updated just last record in my DB :\

Sorry I'm new to php and I'm stuck, I need to know what am I doing wrong. thanx
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Updating multiple columns in MySQL DB

Post by social_experiment »

Is $_POST['uniqueID'] an array? Paste your form (html) code aswell.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
A7x
Forum Newbie
Posts: 10
Joined: Thu Mar 03, 2011 6:07 am

Re: Updating multiple columns in MySQL DB

Post by A7x »

Here's the code;

Code: Select all

<form action="" method="post" name="form1">
<select name="select">
  <option value="Value1">Value1</option>
  <option value="Value2">Value2</option>
</select>
<input type="hidden" name="uniqueID" value="<?php echo $row['id']; ?>"   />
<input type='submit' name="go" value="Go"  />
</form>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Updating multiple columns in MySQL DB

Post by social_experiment »

Code: Select all

<input type="hidden" name="uniqueID" value="<?php echo $row['id']; ?>"   />
$_POST['uniqueID'] isn't an array that is why only 1 record is being updated.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply