Petite-Dragon Posted February 25, 2015 Share Posted February 25, 2015 <?php $connection=mysql_connect('localhost', '',''); mysql_select_db('db'); $username= $_SESSION['username']; $query1 = mysql_query("SELECT * FROM instructor WHERE username='$username'"); $row1 = mysql_fetch_assoc($query1); $i_id = $row1['id']; $query2 = mysql_query("SELECT g_code FROM i_group WHERE i_id='$i_id' "); $row2 = mysql_fetch_assoc($query2); <---PROBLEM STARTS HERE (not part of the code)---> echo 'Select a group :'; echo '<select name="code1">'; echo '<br>'; while($row2=mysql_fetch_array($query2)){ echo '<br>'; echo "<option value=\"" . $row2['g_code'] . "\">" . $row2['g_code'] . "</option> "; } echo '</select>'; echo '<input type="submit" name="submit" value="GO">'; echo '</form>'; if(isset($_POST['submit'])){ $go = $_POST['code1']; $query = "SELECT * FROM student WHERE group='$go'"; im getting the row2 values but when i select from the option and click submit it says that the query is wrong any idea about my problem? advance thanks Quote Link to comment https://forums.phpfreaks.com/topic/294895-getting-value-from/ Share on other sites More sharing options...
maxxd Posted February 25, 2015 Share Posted February 25, 2015 Could you explain what you mean by 'it says that the query is wrong'? What I see right off the bat is that you don't appear to be calling session_start() before using $_SESSION, but without more information I have no idea if that's the issue or if you didn't copy/paste the session_start() call. Quote Link to comment https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506723 Share on other sites More sharing options...
Psycho Posted February 25, 2015 Share Posted February 25, 2015 That code looks to be incomplete. There isn't a complete form (there's no opening FORM tag). So, if the form fields are being sent, they are likely being sent as GET variables since that is the default method unless the method is set to POST in the opening FORM tag. Also, why are you storing the username in the session? You are then running a query to get the User ID each time the page is loaded. You should be storing the User ID in the session if you are going to need that on a frequent basis. Quote Link to comment https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506726 Share on other sites More sharing options...
Petite-Dragon Posted February 25, 2015 Author Share Posted February 25, 2015 That code looks to be incomplete. There isn't a complete form (there's no opening FORM tag). So, if the form fields are being sent, they are likely being sent as GET variables since that is the default method unless the method is set to POST in the opening FORM tag. Also, why are you storing the username in the session? You are then running a query to get the User ID each time the page is loaded. You should be storing the User ID in the session if you are going to need that on a frequent basis. That code looks to be incomplete. There isn't a complete form (there's no opening FORM tag). So, if the form fields are being sent, they are likely being sent as GET variables since that is the default method unless the method is set to POST in the opening FORM tag. Also, why are you storing the username in the session? You are then running a query to get the User ID each time the page is loaded. You should be storing the User ID in the session if you are going to need that on a frequent basis. sorry for the late reply the problem is this only echo 'Select a group :'; echo '<select name="code1">'; echo '<br>'; while($row2=mysql_fetch_array($query2)){ echo '<br>'; echo "<option value=\"" . $row2['g_code'] . "\">" . $row2['g_code'] . "</option> "; } echo '</select>'; echo '<input type="submit" name="submit" value="GO">'; echo '</form>'; if(isset($_POST['submit'])){ $go = $_POST['code1']; $query = "SELECT * FROM student WHERE group='$go'"; getting the option value is working fine this where the problem starts now i have the submit button for the $go = $_POST['code1']; getting value from the option select i am not getting any value from &_POST['code1']; which is in the options, so after submitting $go still remains empty and the query does not execute because the $go remains empty after submitting the button Quote Link to comment https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506759 Share on other sites More sharing options...
Petite-Dragon Posted February 25, 2015 Author Share Posted February 25, 2015 sorry for the late reply the problem is this only echo 'Select a group :'; echo '<select name="code1">'; echo '<br>'; while($row2=mysql_fetch_array($query2)){ echo '<br>'; echo "<option value=\"" . $row2['g_code'] . "\">" . $row2['g_code'] . "</option> "; } echo '</select>'; echo '<input type="submit" name="submit" value="GO">'; echo '</form>'; if(isset($_POST['submit'])){ $go = $_POST['code1']; $query = "SELECT * FROM student WHERE group='$go'"; getting the option value is working fine this where the problem starts now i have the submit button for the $go = $_POST['code1']; getting value from the option select i am not getting any value from &_POST['code1']; which is in the options, so after submitting $go still remains empty and the query does not execute because the $go remains empty after submitting the button i think i found my problem, many says the the group that im using is a reserve word thats why im not getting any value anyways thank you for the reply Quote Link to comment https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506762 Share on other sites More sharing options...
cyberRobot Posted February 26, 2015 Share Posted February 26, 2015 i think i found my problem, many says the the group that im using is a reserve word thats why im not getting any value In case it helps, a list of reserved words can be found here: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html You could surround the reserved word with back ticks. $query = "SELECT * FROM student WHERE `group`='$go'"; Quote Link to comment https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506813 Share on other sites More sharing options...
jcbones Posted February 26, 2015 Share Posted February 26, 2015 There are a couple of things to note. 1. You should learn JOIN's, you can pull all of this form data with one query. 2. You should migrate to the MySQLi or PDO libraries. <?php $connection=mysql_connect('localhost', '',''); mysql_select_db('db'); $username= $_SESSION['username']; $query = "SELECT g.g_code FROM i_group AS g JOIN instructor AS i ON g.i_id = i.id WHERE i.username = '$username'"; $query2 = mysql_query($query) or trigger_error(mysql_error()); $row2 = mysql_fetch_assoc($query2); <---PROBLEM STARTS HERE (not part of the code)---> echo 'Select a group :'; echo '<form method="post"><select name="code1">'; echo '<br>'; while($row2=mysql_fetch_array($query2)){ echo '<br>'; echo "<option value=\"" . $row2['g_code'] . "\">" . $row2['g_code'] . "</option> "; } echo '</select>'; echo '<input type="submit" name="submit" value="GO">'; echo '</form>'; if(!empty($_POST['code1'])){ $go = $_POST['code1']; $query = "SELECT * FROM student WHERE `group`='$go'"; ///blah,blah } else { echo 'You didn\'t receive a go code!'; } Quote Link to comment https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506831 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.