Jump to content

PHP SQL SELECT error in code please help


zer0uk

Recommended Posts

please can you help .. I'm passing a value from another page using $_SESSION and trying to then use it in a SELECT statement in SQL think I'm missing something ... if hard code the value it works , and I have also checked at the other end that the variable is being assigned ...

 

code is (its the = $_SESSION['g_district'] "); that's causing the issue

 

 

I'm getting 

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /Applications/XAMPP/xamppfiles/htdocs/mytimekeeper/add/add.php on line 170 in the browser

     <div class="row s2 m2 l2">    
                <div>
                <label for="course_id">Choose the course raced:</label>
                <select name="course_id">
    	            <?php
    	                // query to create course_id dropdown
    	                $resultset = $conn->query("SELECT `course_id`,`course_name` FROM `tbl_courses` WHERE `district`= $_SESSION['g_district'] ");
                        while($row = $resultset->fetch_assoc())
                        { 
                        echo "<option value='{$row[course_id]}'>{$row['course_name']}</option>";   
                        }
      	            ?>  
	            </select>

 

Link to comment
Share on other sites

Thanks for the replies .....

I don't get an error anymore 🙂  but the drop down box (which the code populates from db.) no longer appears so somethings up !! 😞 

remove the code and hardcode "J" ... code works and the drop down list is populated with results filtered by "J"

so I now have ... 1st pic code , 2nd pic code of working code

 

div class="row s2 m2 l2">    
                <div>
                <label for="course_id">Choose the course raced:</label>
                <select name="course_id">
    	            <?php
    	                // query to create course_id dropdown
    	                $resultset = $conn->query("SELECT `course_id`,`course_name` FROM `tbl_courses` WHERE `district`= {$_SESSION['g_district']} ");
                        while($row = $resultset->fetch_assoc())
                        { 
                            echo "<option value='" . $row['course_id'] . "'>" . $row['course_name'] . "</option>";  
                        }
      	            ?>  
	            </select>
<div class="row s2 m2 l2">    
                <div>
                <label for="course_id">Choose the course raced:</label>
                <select name="course_id">
    	            <?php
    	                // query to create course_id dropdown
    	                $resultset = $conn->query("SELECT `course_id`,`course_name` FROM `tbl_courses` WHERE `district`= 'J' ");
                        while($row = $resultset->fetch_assoc())
                        { 
                            echo "<option value='" . $row['course_id'] . "'>" . $row['course_name'] . "</option>";  
                        }
      	            ?>  
	            </select>

 

Link to comment
Share on other sites

First question is - have you checked if any rows are ever returned from your query?  I don't see that happening in your current code, which is something that you should be doing.

Then - if nothing else, try echoing out the query statement as well as taking a look at the source of your web page to see what the drop down looks like.

Link to comment
Share on other sites

in addition, you need to always have error handling for database statements. if you had, you would know that the query is failing, with an error about a non-existent column of the same name as the session variable value. also, using a prepared query would have eliminated the problem, since you would not be trying to put data directly into the sql query statement.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.