簡體   English   中英

如何將選擇表值存儲為php以在sql查詢中使用

[英]How to store select table values as php to use in sql query

我正在使用我想要存儲的表單中的選擇值,在提交表單時,會顯示一個表格,其中包含我的SQL查詢結果,但使用選擇值來查詢數據庫。 我目前的html在這里:

<body>
    <div id="top-menu">
        <ul>
            <li><a class="active" href="">Profile</a></li>
            <li><a href="">Favourites</a></li>
            <li><a href="">Messages</a></li>

            <li id="mainheading" style="align left"><a      href="Home.html"><strong>BookSmart</strong></a></li>

    </div>

    <div id ="search_elements">

        <form method="post" action="test.php">    
            <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 height="250px", width="600px" src="PriceSelect.jpeg">

                <select name="rent">
                    <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>
        <img height="250px", width="600px" src="RoomSelect.jpeg">

                <select name="roomtype">
                    <option selected disabled>Room Type</option>
                    <option value="50">University Halls</option>
                    <option value="100"> Studio</option>
                    <option value="150">Flat</option>
                    <option value="200"> Shared Accomodation</option>

                </select>


        <img height="250px", width="600px" src="DistanceSelect.jpeg">

                <select name="distance">
                    <option selected disabled> Selecet a distance from         the university</option>
                    <option value="1">0-1 miles</option>
                    <option value="3"> 1-3 miles</option>
                    <option value="5">3-5 miles</option>
                    <option value="6"> 5+ miles</option>

                </select>
            <input type="submit" name="submit" value="Search">
        </form>
    </div>



</body>

我目前用於查詢數據庫並在表中顯示結果的代碼是:

<p style ="text-align: center" id="php_style">
<?php
    error_reporting(E_ERROR | E_PARSE);
    session_start();
    $counter_name = "counter.txt";
    // Check if a text file exists. If not create one and initialize it to zero.
    if (!file_exists($counter_name)) {
      $f = fopen($counter_name, "w");
      fwrite($f,"0");
      fclose($f);
    }
    // Read the current value of our counter file
    $f = fopen($counter_name,"r");
    $counterVal = fread($f, filesize($counter_name));
    fclose($f);
    // Has visitor been counted in this session?
    // If not, increase counter value by one
    if(!isset($_SESSION['hasVisited'])){
      $_SESSION['hasVisited']="yes";
      $counterVal++;
      $f = fopen($counter_name, "w");
      fwrite($f, $counterVal);
      fclose($f); 
    }
    echo  nl2br ("You are visitor number ".$counterVal. " to this site     \n Today's date is ". date("d/m/Y"));

        $con=mysqli_connect("localhost:8889","root","root","booksmart_properties");
// Check connection
if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
else
{
    echo "we connected";
}

if(isset($_POST['submit']
    {
    $university = $_POST['university'];
    $rent = $_POST['rent'];
    $roomtype = $_POST['roomtype'];
    $distance = $_POST['distance'];


    //Now you can do anything with variables , you can put them inside     query like this : //
    // $result=mysqli_query($con,"SELECT * FROM Table WHERE Field = '$universit' "); // 
    // Note : this is dangrous way to do a query in PHP you should prevent something called SQL Injection , search for it  //
    // Perform queries  

    $result=mysqli_query($con,"SELECT * FROM ListedProperties 
    WHERE PropertyType = '.$roomtype' ");
    echo "<table border='1'>
        <tr>
            <th>PropertyType</th>
            <th>Description</th>
            <th>Rent Price</th>
            <th>Location</th>
        </tr>";
    while($row = mysqli_fetch_array($result))
        {
            echo "<tr>";
            echo "<td>" . $row['PropertyType'] . "</td>";
            echo "<td>" . $row['Description'] . "</td>";
            echo "<td>" . $row['RentPrice'] . "</td>";
            echo "<td>" . $row['Location'] . "</td>";
            echo "</tr>";
        }
    echo "</table>";

    }))

mysqli_close($con);
?>

我想知道如何將select表中的值存儲為php變量並插入到查詢中以便在按下搜索按鈕時使用。

您可以使用此鏈接http://www.w3schools.com/tags/att_form_method.asp之類的表單方法

並使用此鏈接將我的SQL插入數據庫http://www.w3schools.com/php/php_mysql_insert.asp

好吧,首先你必須給每個Select標簽一個這樣的名字:我會為一個人做,你為其他人做這個:

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

給每個SELECT標簽后,一個名稱現在給你的輸入一個如下名字:

    <input type="submit" name="submit" value="Search">

現在是時候從表單接收值並將它們保存在PHP變量中。

    $con=mysqli_connect("localhost:8889","root","root","booksmart_properties");
    // Check connection
    if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    else
    {
        echo "we connected";
    }

    if(isset($_POST['submit')
        {
        $university = $_POST['university'];
        $rent = $_POST['rent']; // i assumed that you name the second Select tag RENT and the Third ROOMTYPE and the last DISTANCE  
        $roomtype = $_POST['roomtype'];
        $distance = $_POST['distance'];

        //Now you can do anything with variables , you can put them inside query like this : //
        // $result=mysqli_query($con,"SELECT * FROM Table WHERE Field = '$universit' "); // 
        // Note : this is dangrous way to do a query in PHP you should prevent something called SQL Injection , search for it  //
        // Perform queries  

        $result=mysqli_query($con,"SELECT * FROM ListedProperties");
        echo "<table border='1'>
            <tr>
                <th>PropertyType</th>
                <th>Description</th>
                <th>Rent Price</th>
                <th>Location</th>
            </tr>";
        while($row = mysqli_fetch_array($result))
            {
                echo "<tr>";
                echo "<td>" . $row['PropertyType'] . "</td>";
                echo "<td>" . $row['Description'] . "</td>";
                echo "<td>" . $row['RentPrice'] . "</td>";
                echo "<td>" . $row['Location'] . "</td>";
                echo "</tr>";
            }
        echo "</table>";

        }

mysqli_close($con);
?>

將來有助於您的重要鏈接和主題:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM