Drop down box problem using mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dodaaxx
    New Member
    • Apr 2010
    • 1

    Drop down box problem using mysql

    Hi all i'm newbie in this php and mysql coding
    so can you plz help me

    i'm using wamp server

    and when i try to run the code there is an error pop up which say

    PHP Code:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1


    here is my code

    dropdown.php
    Code:
    <?php
    $localhost="localhost";
    $username="root";
    $password="";
    $database="e_cinema";
    
    $linkid=mysql_connect($localhost,$username,$password);
    mysql_select_db($database) or die( "Unable to select database");
    
    $query="SELECT MovieName,MovieID FROM movie";
    $result = mysql_query ($query);
    
    echo "<form name=MovieName method=post action='data.php'> <select MovieName=MovieName>Movie Name";
    while($nt=mysql_fetch_array($result))
    {
        echo "<option value=$nt[MovieID]>$nt[MovieName]</option>";
    } echo
    
    "</select>";
    echo " <input type=submit value=Submit name=button> </form>"; 
    ?>

    data.php
    Code:
    <?php
    $localhost="localhost";
    $username="root";
    $password="";
    $database="e_cinema";
    
    $linkid=mysql_connect($localhost,$username,$password);
    mysql_select_db($database) or die( "Unable to select database");
    
    if(isset($_POST['button']))
    {
        $MovieName= isset($_POST['MovieName']);
        $query = "SELECT * FROM movie WHERE MovieName= $MovieName";
        $result = mysql_query($query) or die(mysql_error());
    
        echo "<table border='1'>";
        echo "<tr> <th>MovieID</th> <th>MovieName</th> <th>MovieRating</th> <th>Category</th> </tr>";
        while($row = mysql_fetch_array($result))
        {
            echo "<tr><td>";
            echo $row['MovieID'];
            echo "</td><td>";
            echo $row['MovieName'];
            echo "</td><td>";
            echo $row['MovieRating'];
            echo "</td><td>";
            echo $row['Category'];
            echo "</td></tr>";
        } echo
        "</table>";
    }
    else
    {
    
    }
    
    ?>


    i dont know where is the wrong so can you plz help
    Last edited by Atli; Apr 16 '10, 11:22 PM. Reason: Fixed the missing new-lines and missing indentation in the code.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    and when i try to run the code there is an error pop
    Which code produces the error, dropdown.php or the data.php?

    A few things you should take a look at:
    • The "MovieName" select box in your dropdown code uses the ID of the movie as it's value, but in your data code you try to match it against the MovieName column in your database table. You should be matching it against the MovieID column.
    • The isset function is used to check whether a variable/element exists. Your usage of it on line #12 in your data.php code is incorrect. You should replace it with either mysql_real_esca pe_string (in cast it is text), or cast it to the appropriate type (int, for IDs).
    • It's not a good idea to verify that a form has been posted by checking a submit button. There are situations where a form can be submitted without it being used. Instead, verify that the POST data you are expecting exists; MovieName, in your case. (On line #10 in data.php)

    Comment

    Working...