Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Calling A Variable From A Dynamic List In A Sql Statement

Recommended Posts

I would be greatful if anyone could help me out, I have been stuck on this for a while. The problem is that I have a drop down menu which I get the contents of from a database. Then when I press the get details button I want to use the value selected in my drop down menu and pass it into an SQL statement to recieve all te relevent info. Everything works if I specify a known variable, but the problem seems to be passing a variable in which is part of the array. Below is the code for the drop down menu and the Sql statement i'm trying to execute. drop down menu:$query ="SELECT sub_title FROM sport_specials"; //run query$result = mysql_query ($query);if ($result) {echo "<select name='cso' class='textarea' id='cso'>";while ($special = mysql_fetch_array($result, MYSQL_NUM)) { echo "<option value=".$special[0].">$special[0]</option>"; //Display available sports drop down menu}SQL statement:if($_POST['submit'] == 'Get Details') {echo 'start';$sql = "SELECT title, sub_title, details, price FROM sport_specials WHERE sub_title = '$cso' ";$result = mysql_query($sql);if(mysql_num_rows($result) > 0) {echo 'hello';$title = htmlentities(mysql_result($result, 0, "title"));$sub_title = htmlentities(mysql_result($result, 0, "sub_title"));$details = htmlentities(mysql_result($result, 0, "details"));$price = htmlentities(mysql_result($result, 0, "price"));}else {echo 'error';$error[sizeof($error)] = "There is no description in this offer for selected special";}} I would appreciate it greatly if anyone could help.

Share this post


Link to post
Share on other sites

I'm assuming you included a config.php or atleast a connect query?

 

Also, (that must be me), i dont see an actualy sql query taking place, All your sql querys are inside set variables, to my noobish eye it doesn't seem to ultimately actually run one of those querys

 

P.S. next time use

, because not using it is considered hosting credit spam..

Share this post


Link to post
Share on other sites

There is an SQL query executed:

$sql = "SELECT title, sub_title, details, price FROM sport_specials WHERE sub_title = '$cso' ";$result = mysql_query($sql);
Seeing as they aren't getting any connection errors (at least, they haven't mentioned it) I think it's safe to assume that they are connecting to the database prior to this. It doesn't appear to be the entire script.

kvarnerexpress, exactly where does the error occur, and what is it that happens? You are obviously free to do as you will, but I would rewrite the given code to something along the lines of:

// Drop down menu$query ='SELECT sub_title FROM sport_specials'; //run query$result = mysql_query($query);if ($result) {  $data = mysql_fetch_array($result);  echo '<select name="cso" class="textarea" id="cso">';  while ($special = mysql_fetch_array($result, MYSQL_NUM)) {      echo "<option value=\"".$special[0]."\">$special[0]</option>\n"; //Display available sports drop down menu  }}

I don't think you're structuring the mysql_result() function properly either. So:
// SQL 'statement'if( isset($_POST['submit']) && $_POST['submit'] == 'Get Details' ) {   echo 'start';   // I'm going to assume that $cso already has a value.   $sql = "SELECT title, sub_title, details, price FROM sport_specials WHERE sub_title = '$cso' ";   $result = mysql_query($sql);   if( mysql_num_rows($result) > 0 ) {      echo 'hello';      $db = mysql_fetch_assoc($result);      $title = htmlentities($db['title']);      $sub_title = htmlentities($db['sub_title']);      $details = htmlentities($db['details']);      $price = htmlentities($db['price']);   } else {      echo 'error';      $error[sizeof($error)] = "There is no description in this offer for selected special";   }}

Formatting and coding style obviously doesn't matter, and I don't know if this will fix your error or not - a little more detail of the problem wouldn't go astray.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.