PDA

Click to See Complete Forum and Search --> : [RESOLVED] PHP randomly updates data!


sveegaard
Apr 21st, 2006, 02:32 AM
Hi,
When I execute this code, it is quite random when PHP updates the table!
$query = "UPDATE $username SET `dato` = '$ny_dato', `aktivitet` = '$ny_aktivitet', `varighed` = '$ny_varighed' WHERE `id` = $id";
mysql_query($query) or die(mysql_error());

john tindell
Apr 21st, 2006, 02:50 AM
What do you mean randomly updates? Does it give an error when it doesn't update?

sveegaard
Apr 21st, 2006, 03:08 AM
Well, I think I found the real prob.
I'm trying to update several textfields from a page. It seems like I can't post all the fields' own id's to this page.
How do I post several id's and then php can update the correct rows in mysql?

john tindell
Apr 21st, 2006, 03:18 AM
try using arrays ie,


<form method="post">
<!--First Item -->
<input type="text" name="name[]"/>
<input type="text" name="age[]"/>
<input type="hidden" name="id[]" value="1"/>

<!-- Second Item-->
<input type="text" name="name[]"/>
<input type="text" name="age[]"/>
<input type="hidden" name="id[]" value="2"/>

<!-- Third Item -->
<input type="text" name="name[]"/>
<input type="text" name="age[]"/>
<input type="hidden" name="id[]" value="3"/>

<input type="submit"/>
</form>



for($i = 0; $i < count($_POST['name']); $i++)
{
$query = "UPDATE table SET( name = '" . $_POST['name'][$i] . "', age = " . $_POST['age'][$i] .") WHERE id = " . $_POST['id'][$i];
mysql_query($query);
}


[EDIT]
If you nothing has changed in the row then it won't get updated, so if you are checking to see if an update has worked with mysql_affected_rows (http://uk2.php.net/mysql_affected_rows) you will get 0 returned.

sveegaard
Apr 21st, 2006, 03:26 AM
Ok, my code is now:

<!-- Blah bla --!>
echo "
<tr>
<tr>
<td align='center'><input name='nydato[]' type='text' value='$dato' size='25'></td>
<td align='center'><input name='nyaktivitet[]' type='text' value='$aktivitet' size='25'></td>
<td align='center'><input name='nyvarighed[]' type='text' value='$varighed' size='25'></td>
<input type='hidden' name='id[]' value='$id'>
</tr>
";
<!-- Blah blah --!>



for($i = 0; $i < count($_POST['nydato']); $i++)
{
$query = "UPDATE table SET( dato = '" . $_POST['nydato'][$i] . "', aktivitet = " . $_POST['nyaktivitet'][$i] ."', varighed = " . $_POST['nyvarighed'][$i] .") WHERE id = " . $_POST['id'][$i];
mysql_query($query);
}

but nothing works :/

john tindell
Apr 21st, 2006, 03:34 AM
Have you tried using mysql_affected_rows() (http://uk2.php.net/mysql_affected_rows) to see if its updated or not, it return -1 if the update failed and 0 if no changes have taken place, assuming that mysql_error (http://uk2.php.net/mysql_error) returns nothing

sveegaard
Apr 21st, 2006, 04:49 AM
I've tried alerting the values with Javascript, but they return blank

john tindell
Apr 21st, 2006, 05:29 AM
The values where? In the form? If they are blank you need to look at the code thats getting them from the browser

sveegaard
Apr 21st, 2006, 05:35 AM
Yeah, the form data. But I'm confused, it's only on this page it won't work

john tindell
Apr 21st, 2006, 05:48 AM
Well if there are no values on the form then there will be nothing to update? Also instead of echoing out huge chunks of html from php you can use <?=$id?> to embed php variables into PHP

sveegaard
Apr 21st, 2006, 06:05 AM
The form is filled, that's why it confuses me.
I'm quite new to php, so I don't know other methods then echoing these chunks of html. But I'll try

john tindell
Apr 21st, 2006, 06:30 AM
try printing out the query that are being executed and make sure that they are alid, and have the correct information in them.

for($i = 0; $i < count($_POST['nydato']); $i++)
{
$query = "UPDATE table SET( dato = '" . $_POST['nydato'][$i] . "', aktivitet = " . $_POST['nyaktivitet'][$i] ."', varighed = " . $_POST['nyvarighed'][$i] .") WHERE id = " . $_POST['id'][$i];
print $query . "<br/>";
}