In php:
$query ="SELECT rent FROM mydatabase WHERE rent= '$rent'";
$rs=mysqli_query($connect,$query);
if(!$rs)
{echo "<script type ='text/javascript'>alert('Cannot connect to database')</script>";
}
else
{ if(mysqli_num_rows($rs) != 0)
{die("<script type ='text/javascript'>alert('The selected room are not available')</script>");}
}
$sql = "INSERT INTO mydatabase(title,first_name,last_name,dob,email,id,password,rent) VALUES('$title','$first_name','$last_name','$dob','$email','$id','$password','$rent')";
mysqli_query($connect,$sql);
$_SESSION['first_name'] = $first_name;
$_SESSION['rent'] = $rent;
header("location: database.php");
In html:
<select name = "rent">
<option></option>
<option name= "rent" value="A1">A1</option>>
<option name= "rent" value="A10">A10</option>
<option name= "rent" value="A3">A3</option>
<option name= "rent" value="A4">A4</option>
<option name= "rent" value="A5">A5</option>
<option name= "rent" value="A15">A15</option>
<option name= "rent" value="A20">A20</option>
<option name= "rent" value="A8">A8</option>
<option name= "rent" value="A6">A6</option>
<option name= "rent" value="A12">A12</option>
</select>
Here,I want to set unique for rent value. For example, if customer had selected rent value 'A1' again he cannot select the same value. The problem is I also want to set the empty option for customer which does not want to select any option and store their data in database. But the empty option () cannot be entered two time. So what the thing can I give to overcome this. Ps. I am totally amateur and my English is not so good. Thank You :)
I think the best solution for your problem is you need to create your selection dynamically from your database, I mean get what rent didn't take or has been taken by users from your database.
i.e after user loaded the page that selection fills data from database not put it like static. for this, you can create one more table, or just simply add one more field to determines the rent, I called "IsTaken" it returns 1 for Yes, or 0 for not taken.
after user took then update "IsTaken" from 0 to 1 it means it's taken by the user, or after removed you can update from 1 to 0.
for each new record that you or your users insert, add a query to update that field. also for removing each record.
for the update:
update table t1 set rname = 1 where 'your condition to update only this record'
for the delete:
delete from t1 where 'your condition to delete only this record'
please see my solution and let me know if your problem is solved or not. the following code is just shown selection query.
See this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stackoverflow";
$conn = new mysqli($servername, $username, $password, $dbname);
//SELECT those record that not taken, be default i set that field to 0
$sql = "SELECT rname FROM t1 where istaken = 0";
$result = mysqli_query($conn,$sql);
?>
<form action= "" method= "post">
<select name = "rent">
<?php
if ($result->num_rows > 0) {
//fill options with data returns from our query..
while($row = $result->fetch_assoc()) {
echo "<option name=".$row['rname'].">" . $row['rname'] . "</option>";
}
} else {
//if there is no record...
echo "<option name='None'>None</option>";
}
?>
</select>
</form>
<?php
$conn->close();
?>
.
.
.
I am sorry if I have any grammar or spelling error, hope you understand what I am did here.