PHP & MySQL help please.

Discussion in 'Web Design and Development' started by Stocktrader, Mar 8, 2014.

  1. Stocktrader macrumors member

    Joined:
    Oct 28, 2007
    #1


    Thanks for any help.
    I have a date combo box which chooses from today and next 6 days.
    The output is currently set to show only CURRENTDATE data from MySQL
    My goal is to show data not from CURRENT DATE but from date selected fro ComboBox.

    Thanks again.






    <html>
    <head>
    <title>Connecting MySQL Server</title>

    <style>
    table,td,th
    {
    border:1px solid orange;
    }
    table
    {
    width:100%;
    }
    th
    {
    height:50px;
    }
    </style>


    </head>
    <body>
    <select name="select" id="select">
    <?php
    $date = time();
    $num_days = 7;
    for($i=0; $i<=$num_days; ++$i)
    {
    $date = mktime(0, 0, 0, date("m") , date("d")+$i, date("Y"));
    $date = date('D j M', $date);
    echo " <option value='{$date}'>{$date}</option>\n";
    }
    ?>
    </select>



    <?php
    $con=mysqli_connect("localhost","XXXXXXX","XXXXXXX","XXXXXXX");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }


    $result = mysqli_query($con,"
    SELECT *
    FROM movies2
    WHERE date = CURDATE()



    ");



    echo "<table border='2'>
    <tr>
    <th>Movie</th>
    <th>Rating</th>
    <th>Time</th>
    <th>Date</th>
    <th>Code</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td nowrap>" . $row['movie'] . "</td>";
    echo "<td nowrap>" . $row['rating'] . "</td>";
    echo "<td nowrap>" . $row['time'] . "</td>";
    echo "<td nowrap>" . $row['date'] . "</td>";
    echo "<td nowrap>" . $row['code'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";

    mysqli_close($con);
    ?>


    </body>
    </html>

     
  2. theluggage macrumors 68030

    Joined:
    Jul 29, 2011
    #2
    Remember that PHP runs on the web server and outputs a HTML file that is sent to the browser. You can't interact with the PHP while it is running - your mysql query doesn't know what value is selected in the combo box, because the user hasn't seen it yet! So anything like this needs to be done in 2 passes.

    First, you need to put the combo box inside a <form method="GET"> tag, and add a submit button.

    Then, your PHP script needs to check if $_GET['select'] is set - if not, you're sending the page to this user for the first time so you choose a default date. If it is set (the user has seen the page, chosen the date and clicked submit) then it gives you the date to insert in the SQL query after you have carefully checked that it is in the correct format. You don't want a visit from Little Bobby Tables! (Hint - go Google "mysqli prepared statements" for a safer way of inserting variables into SQL queries).

    If you don't want the user to have to click 'submit' you can add a bit of simple JavaScript to the web page to submit the form whenever the combo box is changed.

    Without wanting to sound mean, I think you need to go read a book or online tutorial about PHP and MySQL. Not only is this forum not the best place for such basic, non-Mac-specific queries, there are a lot of potential security blunders and bad habits you can get into with PHP forms and databases.
     

Share This Page