Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Displaying content from mysql table when selected from a drop down

    Hi all,

    sorry if this is easy, as I'm new to PHP and MYSQL.

    i have some hotel in a table called clients and there is a column call clientsStatus, there are 4 statuses meaning different things. what i want to achieve is to create a drop down menu with the 4 statuses and when selected and submitted the page then displays all the hotels with that status.

    i have been trying and the code i have so far is below. Thanks for your help.

    <?php
    // Make a MySQL Connection
    mysql_connect("localhost", "root", "root") or die(mysql_error());
    mysql_select_db("gsw_cms") or die(mysql_error());
    ?>

    <form method=“post”>
    <select name=“clientStatus">
    <option value=“1”>option 1</option>
    <option value=“2”><option 2</option>
    <option value=“3”><option 2</option>
    <option value=“4”><option 2</option>
    </select>

    <input type=“submit” value=“submit” name=“submit”>
    </form>

    <?php
    // Check if POST is set from form
    if(isset($_POST)) {

    $sql = "SELECT * FROM `clients` WHERE clientStatus = ‘$_POST[stateside]’); //
    Query needed for db
    $result = mysql_query($sql, $dbconnection); // mysql query


    while ($row = mysql_fetch_assoc($result)) {
    echo $row[‘clientName']; // loop through each row and output info
    }
    }
    ?>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,303
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Your query is wrong. Don't use quotes on the table name. Your mixing up your quotes with ticks.
    Should be this:
    PHP Code:
    <?php
    if(isset($_POST)) {
        
    $sql "SELECT * FROM clients WHERE clientStatus = ".$_POST['stateside'];
        echo 
    $sql;
        
    $result mysql_query($sql);
        while (
    $row mysql_fetch_assoc($result)) {
            echo 
    $row['clientName'];
        }
    }
    ?>
    To put this into your <select> use something like this.
    PHP Code:
    <?php
    // Make a MySQL Connection
    mysql_connect("localhost""root""root") or die(mysql_error());
    mysql_select_db("gsw_cms") or die(mysql_error());
    $_POST["stateside"] = "stateside";  // You need code to get this passed value
    $i 1;
    ?>


    <select name="clientStatus">
    <?php
    if(isset($_POST)) {
        
    $sql "SELECT * FROM clients WHERE clientStatus = ".$_POST['stateside'];
        echo 
    $sql;
        
    $result mysql_query($sql);
        while (
    $row mysql_fetch_assoc($result)) {
            echo 
    "<option value='$i'>".$row['clientName']."</option>";
            
    $i++;
        }
    }
    ?>
    </select>

    <input type="submit" value="submit" name="submit">
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •