Sarahpengie Posted February 12, 2013 Share Posted February 12, 2013 Hi, I create a webpage where the admin can delete members. I am using the same code to delete bookings, however it seems it is not reading the IF statement and keeps returning "Nothing deleted". This code I have used is as follows: <?php include('../../config.php'); $LessonDate = ""; if (isset ($_GET['LessonDate']) ) {$LessonDate = $_GET['LessonDate'];} mysql_query("DELETE FROM `booking` WHERE `LessonDate` = '$LessonDate' ") ; echo (mysql_affected_rows()) ? "Row deleted.<br /> " : "Nothing deleted.<br /> "; ?> I did have to make some changes from the code I used when deleting members so this may be a problem. The original code is as follows: <?php include('../../config.php'); $userid = (int) $_GET['userid']; mysql_query("DELETE FROM `student` WHERE `userid` = '$userid' ") ; echo (mysql_affected_rows()) ? "Row deleted.<br /> " : "Nothing deleted.<br /> "; ?> Does anyone know what the problem could be? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/274399-skipping-an-if-statement/ Share on other sites More sharing options...
KevinM1 Posted February 12, 2013 Share Posted February 12, 2013 You need to put all of your delete code in the if-conditional: include('../../config.php'); $LessonDate = ""; if (isset ($_GET['LessonDate']) ) { $LessonDate = $_GET['LessonDate']; mysql_query("DELETE FROM `booking` WHERE `LessonDate` = '$LessonDate' ") ; echo (mysql_affected_rows()) ? "Row deleted.<br /> " : "Nothing deleted.<br /> "; } The brackets {} denote a block of code. In this case, it means that everything within them will only be executed if the query string value LessonDate is set. With your original code, your placement of the brackets meant that $LessonDate was being assigned if the query string was set, but the actual MySQL query and subsequent echo were happening regardless. Now, all of it depends on whether or not the query string is set. If it's not, no deletion will happen. For more info, read the manual: http://php.net/manual/en/control-structures.if.php Quote Link to comment https://forums.phpfreaks.com/topic/274399-skipping-an-if-statement/#findComment-1411998 Share on other sites More sharing options...
Sarahpengie Posted February 12, 2013 Author Share Posted February 12, 2013 Hi Kevin, Thanks for your help. I had previously tried this way too, but it still doesn't delete it from the database table? Thanks, Sarah. Quote Link to comment https://forums.phpfreaks.com/topic/274399-skipping-an-if-statement/#findComment-1412000 Share on other sites More sharing options...
Sarahpengie Posted February 12, 2013 Author Share Posted February 12, 2013 If I took this line here from the original code : $userid = (int) $_GET['userid']; and replaced 'userid' with 'LessonDate' what would I put in the brackets instead of (int)? Quote Link to comment https://forums.phpfreaks.com/topic/274399-skipping-an-if-statement/#findComment-1412001 Share on other sites More sharing options...
KevinM1 Posted February 12, 2013 Share Posted February 12, 2013 Are you certain that a listing with the lesson date in question exists in your database? Or that the lesson date passed in is in the right format? Have you tried running the query in phpMyAdmin? Quote Link to comment https://forums.phpfreaks.com/topic/274399-skipping-an-if-statement/#findComment-1412002 Share on other sites More sharing options...
KevinM1 Posted February 12, 2013 Share Posted February 12, 2013 This line of code: $userid = (int) $_GET['userid']; Does the following: 1. It assumes a query string like www.example.com/index.php?userid=something 2. It grabs the 'userid' portion with $_GET['userid']. At this point, if the query string exists with userid, $_GET['userid'] will contain something 3. The (int) portion casts (transforms) the something contained in $_GET['userid'] into an integer 4. That new integer version of something is then assigned to (stored in) a variable named $userid So, asking me whether casting LessonDate as an integer misses the point. Are the values in your LessonDate database column stored as integers? Do you want the incoming data to be turned into an integer? Quote Link to comment https://forums.phpfreaks.com/topic/274399-skipping-an-if-statement/#findComment-1412005 Share on other sites More sharing options...
Sarahpengie Posted February 12, 2013 Author Share Posted February 12, 2013 Ok, I changed it from LessonDate and now am using BookingID instead. So this is the code now: <?php include('../../config.php'); $BookingID = (int) $_GET['BookingID']; mysql_query("DELETE FROM `student` WHERE `BookingID` = '$BookingID' ") ; echo (mysql_affected_rows()) ? "Row deleted.<br /> " : "Nothing deleted.<br /> "; ?> It says row deleted when I click delete, but still doesn't actually delete anything. Sorry if I'm confusing. I really appreciate your help thanks. Quote Link to comment https://forums.phpfreaks.com/topic/274399-skipping-an-if-statement/#findComment-1412006 Share on other sites More sharing options...
KevinM1 Posted February 12, 2013 Share Posted February 12, 2013 You need to explain as clearly and concisely as you can what you're trying to do and how you're trying to do it. That includes: 1. What you expect an incoming URL to look like 2. Your current DB structure Right now, you're blindly throwing code at the problem without any sort of understanding behind it. Programming doesn't work like that. Quote Link to comment https://forums.phpfreaks.com/topic/274399-skipping-an-if-statement/#findComment-1412009 Share on other sites More sharing options...
Sarahpengie Posted February 12, 2013 Author Share Posted February 12, 2013 I got it working!! Thanks for your help :) Quote Link to comment https://forums.phpfreaks.com/topic/274399-skipping-an-if-statement/#findComment-1412010 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.