Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: error unsure

  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy error unsure

    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:

    PHP Code:
    $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:

    PHP Code:
    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
    Warm Regards,
    Mivec

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just a quick glance through, this may help you in the future when debugging:

    PHP Code:
    $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.

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up solved

    thank you!problem solved!..thank you
    Warm Regards,
    Mivec

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by raf
    - 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.

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sad69
    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
    PHP Code:
    $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
    PHP Code:
     $query "UPDATE tblmembers SET updatedatetime=Now(), bgmusic= '".$_POST["radSong"]."' WHERE username = '".$_POST["txtUsername"]."'"
    and then there is always 1 affected row
    Last edited by raf; 05-05-2004 at 11:23 PM. Reason: typo
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sad69
    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)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •