Jump to content

Else statement not working


dessolator

Recommended Posts

Hi, I have a form on a page where a user enters their booking id which is then passed over to the next page so that their booking id can be deleted from the database where their user id = something. The script is deleting the booking id if it exists where userid= something but when their is no match in the database I want it to display a error message but for some reason I can't get it to show it and it is just displaying booking cancelled. I have tried fetch_array and num rows and still can't get it to work.

 

<?php
session_start();
$session_username = $_SESSION['myusername'];

$reason = substr($_POST['reason'], 0, 65); 
$booking_id = substr($_POST['bookingid'], 0, 65);

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="abc123"; // Mysql password
$db_name="colab_booking"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

//Database - Assigns the below statement into the variable query
$query = "SELECT * FROM members WHERE username = '$session_username'";
$result = mysql_query($query);

$count=mysql_num_rows($result);
if($count==1){
$user_id = $row[0];
  
  mysql_query("DELETE FROM `bookings` WHERE `booking_id` = '$booking_id' AND `member_id` = '$user_id'");

echo "Reservation sucessfully cancelled";
}
  
  else{
  echo "The booking id you entered is not vaid please check it and try again.";
  exit(0);
  }
?>

 

I would appreciate your help.

 

 

Thanks,

 

Ian

Link to comment
Share on other sites

Try this:

 

if($count==1){
$user_id = $row[0];
  
  mysql_query("DELETE FROM `bookings` WHERE `booking_id` = '$booking_id' AND `member_id` = '$user_id'");

echo "Reservation sucessfully cancelled";
}
  
elseif($count==0){
  echo "The booking id you entered is not vaid please check it and try again.";
  exit(0);
  }

Link to comment
Share on other sites

Thanks for your reply,

 

Basically i'm getting the username from the session then I want to lookup in the members table to get the member_id so that I can identify them in the bookings table as this only has booking id, member id and event id in. But I can't think of the way to do it as I thought the count would check for matching rows. Any pointers on how to do this?

 

Thanks,

 

Ian

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.