|
-
Apr 21st, 2006, 02:32 AM
#1
Thread Starter
Addicted Member
[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());
-
Apr 21st, 2006, 02:50 AM
#2
<?="Moderator"?>
Re: PHP randomly updates data!
What do you mean randomly updates? Does it give an error when it doesn't update?
-
Apr 21st, 2006, 03:08 AM
#3
Thread Starter
Addicted Member
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?
-
Apr 21st, 2006, 03:18 AM
#4
<?="Moderator"?>
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.
Last edited by john tindell; Apr 21st, 2006 at 03:21 AM.
-
Apr 21st, 2006, 03:26 AM
#5
Thread Starter
Addicted Member
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 :/
-
Apr 21st, 2006, 03:34 AM
#6
<?="Moderator"?>
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
-
Apr 21st, 2006, 04:49 AM
#7
Thread Starter
Addicted Member
Re: PHP randomly updates data!
I've tried alerting the values with Javascript, but they return blank
-
Apr 21st, 2006, 05:29 AM
#8
<?="Moderator"?>
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
-
Apr 21st, 2006, 05:35 AM
#9
Thread Starter
Addicted Member
Re: PHP randomly updates data!
Yeah, the form data. But I'm confused, it's only on this page it won't work
-
Apr 21st, 2006, 05:48 AM
#10
<?="Moderator"?>
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
-
Apr 21st, 2006, 06:05 AM
#11
Thread Starter
Addicted Member
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
-
Apr 21st, 2006, 06:30 AM
#12
<?="Moderator"?>
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/>";
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|