1

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.

5
  • 1
    Hmm... you want values from university and rent price? Commented Oct 3, 2016 at 18:22
  • 1
    Yeah I want to store and use one value from each select element. So one for university and one for rent price Commented Oct 3, 2016 at 18:23
  • 1
    Are these selections in <form>? Commented Oct 3, 2016 at 18:24
  • 1
    Add name attribute for each select tag Commented Oct 3, 2016 at 18:25
  • 1
    In addition to the comments from Edvin about properly escaping data before use for SQL, you may wish to take a look at PDO - it's just as easy to use as mysqli - but offers the chance to connect to a plethora of different database engines. In one instance, I have precisely the same code running under MySQL, SQLite and SQLServer- the only thing that is different in the 3 installations is the connection string - a single line of code. Commented Oct 3, 2016 at 19:14

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

7 Comments

would I not be able to use a format whereby I follow the syntax of a standard query, so " SELECT university FROM TABLENAME WHERE...."etc
Sorry, I don't quite understand your comment. Can you explain more?
Do you mean this?: SELECT university FROM tableName WHERE university = $_POST["university"] (etc.)? But don't forget to escape variables.
Yes! Something like that, but how would it work in practice with correct syntax?
This is actually a correct syntax. Just to for escaping write before this query: $_POST["university"] = $con->real_escape_string($_POST["university"]); (actually, it's better to assign to a new variable, not overwrite, but for now let's leave as it is to avoid confusion. Better solution would be: $university = $con->real_escape_string($_POST["university"]); and then use $university in my written query).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.