如何在mysql中隐藏HTML中的某些选项

时间:2017-04-06 16:42:59

标签: php html mysql

在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”,则他无法选择相同的值。问题是我还想为客户设置一个空选项,它不想选择任何选项并将其数据存储在数据库中。但是空选项()不能两次输入。那么我可以给予什么来克服这一点。 PS。我完全是业余爱好者,我的英语不太好。谢谢你:)

1 个答案:

答案 0 :(得分:0)

我认为您的问题的最佳解决方案是您需要从数据库动态创建选择,我的意思是获取用户从您的数据库中未采取或未采取的租金。

即在用户加载页面后,选择从数据库填充数据并不像静态那样。为此,您可以再创建一个表,或者只是添加一个字段来确定租金,我称之为“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();
?>
.
.
.

如果我有任何语法或拼写错误,我很抱歉,希望你能理解我在这里做了什么。