searching database with a dropdown

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
scifirocket
Forum Commoner
Posts: 31
Joined: Fri Aug 13, 2010 1:24 am

searching database with a dropdown

Post by scifirocket »

I've been searching around for a while, so not I've joined this forum to ask this question.

I'm trying to execute a SQL query through the input of a dropdown box. Here is what I have in my search file so far:

Code: Select all

<?php
  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  $RPrice = $_POST['RPrice']; 
  
  //connect  to the database
  $db=mysql_connect ("beep",  "boop", "beep") or die ('I cannot connect  to the database because: ' . mysql_error());
  //-select  the database to use
  $mydb=mysql_select_db("blup");
  //-query  the database table
  $SQL = "SELECT ID, name, price FROM Foodlist WHERE price="$RPrice"";
  //-run  the query against the mysql query function
  $result=mysql_query($sql);
  //-create  while loop and loop through result set
	 
  while($row=mysql_fetch_array($result)){
          $name  =$row['name'];
          $ID=$row['ID'];

  //-display the result of the array
  echo "<ul>\n";
  echo "<li>" . "<a  href=\"search.php?id=$ID\">"  .$name . "</a></li>\n";
  echo "</ul>";
  }
  }
  else{
  echo  "<p>You did it wrong.</p>";
  }
  }
?>
where RPrice is the variable for the selection from the dropdown. I want to return the name of place that is associated with the price in the table. as of now, it returns the following error:
Parse error: syntax error, unexpected T_VARIABLE in /home/eatade5/public_html/dropsearch.php on line 11

If you need any more information, please let me know. Thanks for your help.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: searching database with a dropdown

Post by JakeJ »

Variable names are case sensitive.

$SQL = "SELECT ID, name, price FROM Foodlist WHERE price="$RPrice"";
//-run the query against the mysql query function
$result=mysql_query($sql);

$SQL is not the same as $Sql, $sQl, $sql, etc.
scifirocket
Forum Commoner
Posts: 31
Joined: Fri Aug 13, 2010 1:24 am

Re: searching database with a dropdown

Post by scifirocket »

thanks for that fix, but I'm still getting the same error as before.
scifirocket
Forum Commoner
Posts: 31
Joined: Fri Aug 13, 2010 1:24 am

Re: searching database with a dropdown

Post by scifirocket »

fix it. i needed to remove the quotes around a variable. but the other issue you described definitely helped.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: searching database with a dropdown

Post by JakeJ »

Glad I could help.
Post Reply