Jump to content

Skipping an IF statement


Sarahpengie

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.