zer0uk Posted March 13, 2020 Share Posted March 13, 2020 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> Quote Link to comment https://forums.phpfreaks.com/topic/310286-php-sql-select-error-in-code-please-help/ Share on other sites More sharing options...
benanamen Posted March 13, 2020 Share Posted March 13, 2020 WHERE `district`= {$_SESSION['g_district']} "); Quote Link to comment https://forums.phpfreaks.com/topic/310286-php-sql-select-error-in-code-please-help/#findComment-1575416 Share on other sites More sharing options...
ginerjm Posted March 13, 2020 Share Posted March 13, 2020 And - echo "<option value='" . $row['course_id'] . "'>" . $row['course_name'] . "</option>"; Quote Link to comment https://forums.phpfreaks.com/topic/310286-php-sql-select-error-in-code-please-help/#findComment-1575418 Share on other sites More sharing options...
requinix Posted March 13, 2020 Share Posted March 13, 2020 In other words, read this. Quote Link to comment https://forums.phpfreaks.com/topic/310286-php-sql-select-error-in-code-please-help/#findComment-1575419 Share on other sites More sharing options...
Barand Posted March 13, 2020 Share Posted March 13, 2020 Better still, use prepared statements and don't embed values in the query at all. Quote Link to comment https://forums.phpfreaks.com/topic/310286-php-sql-select-error-in-code-please-help/#findComment-1575420 Share on other sites More sharing options...
zer0uk Posted March 13, 2020 Author Share Posted March 13, 2020 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> Quote Link to comment https://forums.phpfreaks.com/topic/310286-php-sql-select-error-in-code-please-help/#findComment-1575422 Share on other sites More sharing options...
ginerjm Posted March 13, 2020 Share Posted March 13, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310286-php-sql-select-error-in-code-please-help/#findComment-1575424 Share on other sites More sharing options...
Barand Posted March 13, 2020 Share Posted March 13, 2020 String literals need to be in single quotes ... WHERE `district`= '{$_SESSION['g_district']}' ^ ^ Quote Link to comment https://forums.phpfreaks.com/topic/310286-php-sql-select-error-in-code-please-help/#findComment-1575426 Share on other sites More sharing options...
mac_gyver Posted March 13, 2020 Share Posted March 13, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310286-php-sql-select-error-in-code-please-help/#findComment-1575430 Share on other sites More sharing options...
zer0uk Posted March 14, 2020 Author Share Posted March 14, 2020 2 hours ago, Barand said: String literals need to be in single quotes ... WHERE `district`= '{$_SESSION['g_district']}' ^ ^ Brilliant thank you so much ... works a charm I would have never figured that out !!! Quote Link to comment https://forums.phpfreaks.com/topic/310286-php-sql-select-error-in-code-please-help/#findComment-1575431 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.