...

View Full Version : error unsure



mivec
05-05-2004, 07:04 PM
hi all,
i am building something where searches the database for all related records and then if user wants any particular record shown, the user checks it with a radio button or checkbox. then it updates the members records. here is the code i have done:



$sql = "SELECT id_music, music_dir, artist_name, song_title FROM tblmusic ORDER BY id_music ASC";
$rs = mysql_query($sql);
$numRows = mysql_num_rows($rs);

if ($numRows > 0) {
$i=0;
while ($row = mysql_fetch_object($rs)){
echo("<tr><td class=\"text\" align=center>$row->id_music</td>");
echo("<td class=\"text\" align=center>$row->artist_name</td>");
echo("<td class=\"text\" align=center>$row->song_title</td>");
echo("<td class=\"text\" align=center><u><a href=\"$row->music_dir\">Test Song</a></u></td>");
echo("<td class=\"text\" align=center><input name=\"radSong\" type=\"radio\" value=\"$row->music_dir\"></td>");
echo("</tr>");

}
}
else {
echo("<tr><td colspan=\"2\" class=\"text\">There Are No Backround Music In The Database.</td></tr>");
}


in the next page:



include("dbconnect.php");

$query = "UPDATE tblmembers SET bgmusic= '".$_POST["radSong"]."' WHERE username = '".$_POST["txtUsername"]."'";
$rs = mysql_query($query);
$affRows = mysql_affected_rows($dbc);

if ($affRows > 0){
$message = "SUCCESS: Your Background Music Has Been Successfully Updated.";
}
else{
$message = "ERROR: A problem has occured when you tried to update your background music. " .
"The possible reasons might be your username does not match or " .
"due to the unchecked radio button. Contact the administrators of Foreverus.com for assistence";
}


problem:
it doesn't work.instead it goes to this part:



ERROR: A problem has occured when you tried to update your background music. The possible reasons might be your username does not match or due to the unchecked radio button. Contact the administrators of Foreverus.com for assistence.


what's the problem??i have looked at it and i dunno what's the prob. pls advice.thank you

sad69
05-05-2004, 07:21 PM
Just a quick glance through, this may help you in the future when debugging:



$query = "UPDATE tblmembers SET bgmusic= '".$_POST["radSong"]."' WHERE username = '".$_POST["txtUsername"]."'";
echo $query;


Now you'll see what query is actually being executed. Then you can go into MySQL and actually run the same query and see if it works, if it should work, how you should change it, etc...

Double check the column names and all that kind of stuff. If you're still lost, post back and let us know what you've tried and if you've narrowed down the problem at all.

Good luck,
Sadiq.

raf
05-05-2004, 08:10 PM
You don't post the form or formprocessing so there not muc to go on.

Anyway, if you update a record, but the values are the same as the current ones inside therecord, then the number of affected rows will be 0, so
- you need to make sure that this value for bgmusic is realy different from the one i the db
- it's a bad idea to check the number of affected rows on an update, because 0 doesn't mean there is an error

mivec
05-05-2004, 08:14 PM
thank you!problem solved!..thank you

sad69
05-05-2004, 09:16 PM
- it's a bad idea to check the number of affected rows on an update, because 0 doesn't mean there is an error
Just out of curiousity, what would be a good check then?

Thanks,
Sadiq.

raf
05-05-2004, 09:42 PM
Just out of curiousity, what would be a good check then?

Thanks,
Sadiq.
mysql_query() will return False if the query was not valid or could not be executed. So this


$rs = mysql_query($query)
if ($rs){
$message = 'SUCCESS: Your Background Music Has Been Successfully Updated.';
} else { // will be ran (so $rs!=True) if the syntax is correct, but the query itself is not valid (wrong columnname, tablename etc)
$message = 'ERROR: A problem has occured when you tried to update your background music.
The possible reasons might be your username does not match or
due to the unchecked radio button. Contact the administrators of Foreverus.com for assistence';
}

Or you can force the record to be updated by also updating a timestamp or datetimefield to Now(). Like


$query = "UPDATE tblmembers SET updatedatetime=Now(), bgmusic= '".$_POST["radSong"]."' WHERE username = '".$_POST["txtUsername"]."'";

and then there is always 1 affected row

sad69
05-05-2004, 09:47 PM
Cool. I think I will have the timestamp thing as I want all records to have a username and timestamp associated with it so we know who did what and when.

But I think even in that case I would use the first test you show.

Thanks for that info raf!
Sadiq.

raf
05-05-2004, 10:26 PM
Cool. I think I will have the timestamp thing as I want all records to have a username and timestamp associated with it so we know who did what and when.
Yes, a common and good practice. (But it of course only shows the last editer and editing datetime and only at a rowlevel)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum