wkilc Posted August 20, 2011 Share Posted August 20, 2011 I'm usng this simple script to remove a row from a MySQL table: <?php $id = "123456789"; $pass = "bach"; $query = "DELETE FROM my_form WHERE member = ('$id') && password = ('$pass')"; $result = mysql_query($query); echo "The data has been deleted."; ?> Works as intended, if the password and ID match, then the row is deleted and it echos "The data has been deleted." I want to add an "else" statement... the the password and ID did not match, "the data has NOT been deleted". I know I can't use "else" if I don' begin with an "if"... but I'm not sure where to put it (after the query?) Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/245292-simple-else-statement/ Share on other sites More sharing options...
titan21 Posted August 20, 2011 Share Posted August 20, 2011 You can to use mysql_affected_rows() to check what was deleted - http://uk2.php.net/manual/en/function.mysql-affected-rows.php Quote Link to comment https://forums.phpfreaks.com/topic/245292-simple-else-statement/#findComment-1259823 Share on other sites More sharing options...
wkilc Posted August 20, 2011 Author Share Posted August 20, 2011 Thank you. Now I get: "Number of records deleted: 0" or "number of records deleted: 1" This is good, but I was hoping for a unique response for incorrect ID and password combinations. I'm a noob... so I'm starting simple. Once I got this working... I'm going to modify this code so that the user inputs his/her ID and password using a simple form... then grab the form value form that POST and either delete the row or have it return an error. <?php $id = "123456789"; $pass = "bach"; $query = "DELETE FROM my_form WHERE member = ('$id') && password = ('$pass')"; $result = mysql_query($query); printf ("Number of records deleted: %d\n", mysql_affected_rows()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/245292-simple-else-statement/#findComment-1259827 Share on other sites More sharing options...
titan21 Posted August 20, 2011 Share Posted August 20, 2011 Try <?php $id = "123456789"; $pass = "bach"; $query = "DELETE FROM my_form WHERE member = ('$id') && password = ('$pass')"; $result = mysql_query($query); $affected = mysql_affected_rows(); if($affected == 0) { //nothing was deleted } else { //something was deleted } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245292-simple-else-statement/#findComment-1259834 Share on other sites More sharing options...
wkilc Posted August 20, 2011 Author Share Posted August 20, 2011 PERFECT!!! Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/245292-simple-else-statement/#findComment-1259842 Share on other sites More sharing options...
titan21 Posted August 20, 2011 Share Posted August 20, 2011 No worries - could you mark this thread as solved as a favour? Quote Link to comment https://forums.phpfreaks.com/topic/245292-simple-else-statement/#findComment-1259843 Share on other sites More sharing options...
titan21 Posted August 20, 2011 Share Posted August 20, 2011 No worries - could you mark this thread as solved as a favour? Oh - u did! Quote Link to comment https://forums.phpfreaks.com/topic/245292-simple-else-statement/#findComment-1259844 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.