retrieve data from mysql db based on select option

时间:2016-11-26 18:41:05

标签: php html mysql post html-select

I am new to php and trying to retrieve data from mysql db based on select option (dropdown). I need user to choose one option (boat_class_name) and on submit button display name, venue, start and end dates of that boat class event in the table on the same page. When I run my code I get "Undefined index: event-choice" error, but not sure where is the problem and how can I fix it. Thanks in advance.

<?php
//include auth.php file on all secure pages
include("auth.php");
include("navbar.php");

?>


<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"        >
<p><select  name="event-choice">
<option disabled>Choose boat class</option>
<option value="c1" selected>A Class</option>
<option value="c2">MarbleHead</option>
<option value="c3">Ten Rater</option>
<option value="c4">One Metre</option>
</select></p>
<p><input type="submit" value="submit" name="submit"></p>
</form>         

<?php

require('connectDB.php');

$eventClass=$_POST['event-choice'];
$query= "SELECT event_name, venue, start_date, end_date
            FROM WC_event
            WHERE boat_class_name='$eventClass' AND start_date > CURRENT_DATE()
            ORDER BY start_date ASC;";
if($db->query($query) == TRUE){
        echo "<h1> Last A Class Results  </h1>";
        echo "<table>";
            echo "<tr>";
                echo "<th>Name</th>
                    <th>Venue</th>
                    <th>start_date</th>
                    <th>end_date</th>";
            echo "</tr>";

    $sql= mysqli_query($db, $query) or die (mysql_error());
    while ($row = mysqli_fetch_assoc($sql)){

        echo "<tr>";
        echo "<td> {$row['event_name']}</td> 
                <td>{$row['venue']} </td>
                <td>{$row['start_date']}</td> 
                <td>{$row['end_date']}</td>";

        echo "</tr>";
    }
    echo "</table>";
    }
    else{
        echo "Error: " . $query . $db->error;
    }
    mysqli_close($db);

?>

And this is my code for connection db:

 <?php
$hostname='xxx.xx.xx.xx';
$username='myusername';
$password='1122';
$schema='db_xx';

//create database object and connect to it
global $db;
$db = new mysqli($hostname, $username, $password, $schema);
//if not connected, display error
if($db->connect_errno) {
   die("Failed to connect to MySQL");
}


?> 

If something is not clear, please let me know and I will try to make it clear.

0 个答案:

没有答案