douhao7889 2017-04-06 16:42
浏览 21
已采纳

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

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 :)

  • 写回答

1条回答 默认 最新

  • dongqia3502 2017-04-06 20:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解