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
https://forums.phpfreaks.com/topic/89221-else-statement-not-working/
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);
  }

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.