timmah1 Posted June 19, 2008 Share Posted June 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 19, 2008 Share Posted June 19, 2008 <?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 />"; } } ?> Quote Link to comment Share on other sites More sharing options...
timmah1 Posted June 19, 2008 Author Share Posted June 19, 2008 thanks rhodesa, at least the form does something now, but it don't work here is my error Error in query: UPDATE profile SET status='2' WHERE username='c' Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 19, 2008 Share Posted June 19, 2008 Remember what you said in your sig? <.< Make sure you have a table profile and columns status & username Quote Link to comment Share on other sites More sharing options...
jmelnick Posted June 19, 2008 Share Posted June 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
timmah1 Posted June 19, 2008 Author Share Posted June 19, 2008 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 />"; } } Quote Link to comment Share on other sites More sharing options...
timmah1 Posted June 19, 2008 Author Share Posted June 19, 2008 Thank Joseph, I get the error Fatal error: Call to undefined function: () on line 18 which is for($i=0;$i < $count($_POST['status']);$i++ ){ Quote Link to comment Share on other sites More sharing options...
timmah1 Posted June 19, 2008 Author Share Posted June 19, 2008 I got it to work by changin for($i=0;$i < $count($_POST['status']);$i++ ){ to this for($i=0;$i < count($_POST['username']);$i++ ){ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.