0

I have a car rental system I am working on. When a user rents a car, the system should first check if the number of available cars is greater than 0, if yes, then make the adjustment "AVAILABLE = AVAILABLE+1" (in the MySQL table which keeps track of cars), which means, rent the car to the user. Also, I am trying to record which car went to which user. So I have another database table called rentalrecords which takes in the values of the Username of the logged in user, and ID of the car rented. Now, the problem is, my 'IF-ELSE' part is not executing as desired.

<div id="stylized" class="myform">
<form id="form" name="form" method="POST" action="renting.php" >
<h1>Rent a Car</h1>
<label>Car ID
<span class="small">eg. Enter 1 for Mer1</span>
</label>
<input type="text" name="ID" id="ID" />
<input type="submit" style="margin:30px 100px;" name="submit" value="Check-Out">
<div class="spacer"></div>

</form>
</div>

Now,the action of this form, which is renting.php, is as follows:

<?php
session_start();
if(!session_is_registered(theUSERNAME)){
header("location:customer_login.php");
}

mysql_CONNECT("xx", "xx", "xx") OR DIE("Unable to connect");
mysql_SELECT_DB("xx") OR DIE("Unable to select database");
$ID = $_POST['ID'];
$result = mysql_query("SELECT AVAILABLE FROM car WHERE ID='$ID'");

if(mysql_fetch_array($result)>0)
{
$query="UPDATE car SET AVAILABLE=AVAILABLE-1 WHERE ID='$ID'";
mysql_query($query);
$query = "insert into rentalrecords (USERNAME,ID,RENTED_ON) values ('$_SESSION[theUSERNAME]','$_POST[ID]',CURDATE())";
$result = mysql_query($query);
header("location: list_Clogged.php");
}
else
{
echo "<script>alert('The car you chose is currently unavailable!'); location.href='rent.php';</script>"; 
}
?>

Even though I have available=0, it still is NOT executing the else part and no matter what, it always executes the IF part. The ID and AVAILABLE are the attributes of my MySQL table called 'car' and the in rental records table i just want to insert these values. I am aware that the script is vulnerable to injection at the moment, but first I want to get things working! Any immediate help would be much appreciated.

2 Answers 2

1

You're trying to count a resource...

if(mysql_fetch_array($result)>0)

You need to obtain the results and then count an item within those results:

$res = mysql_fetch_assoc($result);

if($res[0]['AVAILABLE'] > 0)

Note $res[0] means first row of the results. You can also use mysql_fetch_row to obtain a single result.

Keep in mind, mysql_ functions shouldn't be used at all. Look into switching to mysqli or PDO. Also, you need to sanitize input. You're just blindly accepting $_POST['ID']

Sign up to request clarification or add additional context in comments.

2 Comments

only ELSE part is being executed now even though available > 0
First thing would be to run the query manually in phpmyadmin to make sure you should be expecting anything. If that passes, try var_dump($res) just before the IF conditional. That should let you know if you have anything in the resource. You can use mysql_error() to see if there is a mysql error.
0

The mysql_fetch_array function doesn't do what you think it does; it returns an array, not a single value.

3 Comments

ah ok so to return a single value I would require?
Assign its return value to a variable (e.g. $result) and then use $result[0].
only the ELSE part being executed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.