[RESOLVED] PHP randomly updates data!
Hi,
When I execute this code, it is quite random when PHP updates the table!
PHP Code:
$query = "UPDATE $username SET `dato` = '$ny_dato', `aktivitet` = '$ny_aktivitet', `varighed` = '$ny_varighed' WHERE `id` = $id";
mysql_query($query) or die(mysql_error());
Re: PHP randomly updates data!
What do you mean randomly updates? Does it give an error when it doesn't update?
Re: PHP randomly updates data!
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?
Re: PHP randomly updates data!
try using arrays ie,
Code:
<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>
PHP Code:
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 you will get 0 returned.
Re: PHP randomly updates data!
Ok, my code is now:
HTML Code:
<!-- 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 --!>
PHP Code:
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 :/
Re: PHP randomly updates data!
Have you tried using 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 returns nothing
Re: PHP randomly updates data!
I've tried alerting the values with Javascript, but they return blank
Re: PHP randomly updates data!
The values where? In the form? If they are blank you need to look at the code thats getting them from the browser
Re: PHP randomly updates data!
Yeah, the form data. But I'm confused, it's only on this page it won't work
Re: PHP randomly updates data!
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
Re: PHP randomly updates data!
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
Re: PHP randomly updates data!
try printing out the query that are being executed and make sure that they are alid, and have the correct information in them.
PHP Code:
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/>";
}