Jump to content

[SOLVED] Updating Multiple rows with different values


timmah1

Recommended Posts

How do you update a MySQL database at one time with different values?

 

I have this but it's not working, actually, it's done anything!!

 

if($_POST['submit']) {

for($i=0;
$i < $count;
$i++ ){
$status= $_POST['status'][$i];
$username = $_POST['username'][$i];

$query="UPDATE profile SET status='$status' WHERE username='$username'";
mysql_query($query) or die ("Error in query: $query");
print "$status- $username<br /><em>Updated!</em><br /><br />";
}
}

 

Thanks in advance

<?php
  if($_POST['submit']) {
    for($i=0;$i < count($_POST['username']);$i++ ){ //Changed count here
      $status= mysql_real_escape_string($_POST['status'][$i]); //Added mysql_real_escape_string
      $username = mysql_real_escape_string($_POST['username'][$i]); //Added mysql_real_escape_string
      $query="UPDATE profile SET status='$status' WHERE username='$username'";
      mysql_query($query) or die ("Error in query: $query");
      print "$status- $username<br /><em>Updated!</em><br /><br />";
    }
  }
?>

if the post has an array of status'/username's then you could update them.

if for instance the form has inputs like so:

 

<input name="status[]" value="" />

<input name="username[]" value="" />

 

 

if($_POST['submit']) {

for($i=0;$i < $count($_POST['status']);$i++ ){

$status= $_POST['status'][$i];

$username = $_POST['username'][$i];

 

$query= sprintf("UPDATE profile SET status='%s' WHERE username='%s'",

mysql_real_escape_string($status),

mysql_real_escape_string($username)

);

mysql_query($query) or die ("Error in query: $query");

echo $status."-".$username."<br /><em>Updated!</em><br /><br />";

}

}

 

You code needs a way to set up the iterator ($i) to the number of usernames and statuses.

 

Cheers

 

Joseph Melnick

ok, fixed that, but it's not updating anything.

it says it has updated, but it don't

 

Here is the form

<form>
$count = 1;
<input type="text" size="10" name="status" value="<?php echo $count; ?>">
<input type="text" size="40" name="username" value="<?php echo $row['username']; ?>" />
++$count;
<input type="submit" name="submit" id="submit" value="Update Status">

 

Then the process

  if($_POST['submit']) {
    for($i=0;$i < count($_POST['username']);$i++ ){ //Changed count here
      $status= mysql_real_escape_string($_POST['status'][$i]); //Added mysql_real_escape_string
      $username = mysql_real_escape_string($_POST['username'][$i]); //Added mysql_real_escape_string
      $query="UPDATE profile SET status='$status' WHERE username='$username'";
      mysql_query($query)
      or die("Sorry, there was a problem updating the profiles status ".mysql_error());
      print "$status- $username<br /><em>Updated!</em><br /><br />";
    }
  }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.