在 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");
在 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>
在这里,我想为租金值设置唯一。例如,如果客户再次选择租金值“A1”,则他无法选择相同的值。问题是我还想为不想选择任何选项并将其数据存储在数据库中的客户设置空选项。但空选项()不能输入两次。那么我可以付出什么来克服这个问题呢?诗。我完全是业余爱好者,而且我的英语也不是很好。谢谢:)
最佳答案
我认为解决您问题的最佳解决方案是您需要从数据库中动态创建选择,我的意思是从数据库中获取用户未收取或已收取的租金。
即,在用户加载页面后,选择从数据库填充数据,而不是像静态那样放置它。为此,您可以再创建一个表,或者只是再添加一个字段来确定租金,我称之为“IsTaken”,它返回 1 表示是,或 0 表示未占用。
用户获取后,将“IsTaken”从 0 更新为 1,这意味着它已被用户获取,或者删除后,您可以从 1 更新为 0。
对于您或您的用户插入的每条新记录,添加一个查询来更新该字段。也用于删除每条记录。
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'
请查看我的解决方案,并告诉我您的问题是否解决。 以下代码仅显示选择查询。
看这个:
<?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();
?>
.
.
.
如果我有任何语法或拼写错误,我很抱歉,希望您理解我在这里所做的事情。
关于php - 如何从 mysql 隐藏 HTML 中的某些选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43261159/