duanluan2047 2016-10-03 18:20
浏览 70
已采纳

如何使用PHP在html中使用SELECT标记查询我的SQL数据库

Basically I'd like to use the choices which the user has selected from a different select tags, and in essence store these as variables which I can then use to query my database in SQL.

My HTML code is here:

<div id ="search_elements">

    <img  src="UniSelect.jpeg">

    <select>
        <option selected disabled>Select a university</option>
        <option value="ucl">UCL</option>
        <option value="kings">Kings College</option>
        <option value="imperial">Imperial College</option>
        <option value="lse">London School of Economics</option>
    </select>

    <img  src="PriceSelect.jpeg">

    <select>
        <option selected disabled>Select a weekly rent price</option>
        <option value="50">0-£50</option>
        <option value="100"> £100-£150</option>
        <option value="150">£150-200</option>
        <option value="200"> £200+</option>
    </select>
</div>

And the type of php i would be looking to use would be:

//$con=mysqli_connect("localhost","adam","YjM3ZTYwOTQ5OWRmYWZh","adam_...");
//if (mysqli_connect_errno())
//  {
//     echo "Failed to connect to MySQL: " . mysqli_connect_error();
//  }

// Perform queries 
//$sql=mysqli_query($con,"SELECT CONTENT FROM Blog WHERE ID = 01");
//$result=mysqli_fetch_assoc($sql);

//echo $result['CONTENT'];
//mysqli_close($con);

To make it clear once more, I want to use the different values which the user selects, upon clicking a search button, have these query results shown in a table.

  • 写回答

1条回答 默认 最新

  • dsxmwin86342 2016-10-03 18:28
    关注

    This solution a little differs from yours because you have no provided your form, submit button, etc. but in general, after a few changes, it should work too:

    <form method="post" action="">
        <img src="UniSelect.jpeg">
    
        <select name="university">
            <option selected disabled>Select a university</option>
            <option value="ucl">UCL</option>
            <option value="kings">Kings College</option>
            <option value="imperial">Imperial College</option>
            <option value="lse">London School of Economics</option>
        </select>
    
        <img src="PriceSelect.jpeg">
    
        <select name="rent_price">
            <option selected disabled>Select a weekly rent price</option>
            <option value="50">0-£50</option>
            <option value="100"> £100-£150</option>
            <option value="150">£150-200</option>
            <option value="200"> £200+</option>
        </select>
    
        <input type="submit" value="Submit form">
    </form>
    

    And now, to get values of these (something like this and I recommend to place it above your form):

    if (!empty($_POST)) {
        // Checking connection in here
    
        $university_id = mysqli_real_escape_string($con, $_POST['university']);
        $rent_price = mysqli_real_escape_string($con, $_POST['rent_price']);
    
        $sql = mysqli_query($con, "SELECT * FROM university WHERE name = '".$university_id."'");
        $result = mysqli_fetch_assoc($sql);
        // Same thing goes for rent price
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接