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

Link to comment
Share on other sites

<?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 />";
    }
  }
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 />";
    }
  }

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.