By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,473 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,473 IT Pros & Developers. It's quick & easy.

Drop down box problem using mysql

P: 1
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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $localhost="localhost";
  3. $username="root";
  4. $password="";
  5. $database="e_cinema";
  6.  
  7. $linkid=mysql_connect($localhost,$username,$password);
  8. mysql_select_db($database) or die( "Unable to select database");
  9.  
  10. $query="SELECT MovieName,MovieID FROM movie";
  11. $result = mysql_query ($query);
  12.  
  13. echo "<form name=MovieName method=post action='data.php'> <select MovieName=MovieName>Movie Name";
  14. while($nt=mysql_fetch_array($result))
  15. {
  16.     echo "<option value=$nt[MovieID]>$nt[MovieName]</option>";
  17. } echo
  18.  
  19. "</select>";
  20. echo " <input type=submit value=Submit name=button> </form>"; 
  21. ?>

data.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $localhost="localhost";
  3. $username="root";
  4. $password="";
  5. $database="e_cinema";
  6.  
  7. $linkid=mysql_connect($localhost,$username,$password);
  8. mysql_select_db($database) or die( "Unable to select database");
  9.  
  10. if(isset($_POST['button']))
  11. {
  12.     $MovieName= isset($_POST['MovieName']);
  13.     $query = "SELECT * FROM movie WHERE MovieName= $MovieName";
  14.     $result = mysql_query($query) or die(mysql_error());
  15.  
  16.     echo "<table border='1'>";
  17.     echo "<tr> <th>MovieID</th> <th>MovieName</th> <th>MovieRating</th> <th>Category</th> </tr>";
  18.     while($row = mysql_fetch_array($result))
  19.     {
  20.         echo "<tr><td>";
  21.         echo $row['MovieID'];
  22.         echo "</td><td>";
  23.         echo $row['MovieName'];
  24.         echo "</td><td>";
  25.         echo $row['MovieRating'];
  26.         echo "</td><td>";
  27.         echo $row['Category'];
  28.         echo "</td></tr>";
  29.     } echo
  30.     "</table>";
  31. }
  32. else
  33. {
  34.  
  35. }
  36.  
  37. ?>


i dont know where is the wrong so can you plz help
Apr 16 '10 #1
Share this Question
Share on Google+
1 Reply


Atli
Expert 5K+
P: 5,058
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_escape_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)
Apr 16 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.