Jump to content

Getting Value from <select><option>


Recommended Posts

<?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
Link to comment
https://forums.phpfreaks.com/topic/294895-getting-value-from/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506723
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506726
Share on other sites

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

 

  1. echo 'Select a group :';
  2. echo '<select name="code1">';
  3. echo '<br>';
  4. while($row2=mysql_fetch_array($query2)){
  5. echo '<br>';
  6. echo "<option value=\"" . $row2['g_code'] . "\">" . $row2['g_code'] . "</option> ";
  7. }
  8. echo '</select>';
  9. echo '<input type="submit" name="submit" value="GO">';
  10. echo '</form>';
  11. if(isset($_POST['submit'])){
  12.  
  13. $go = $_POST['code1'];
  14. $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

Link to comment
https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506759
Share on other sites

sorry for the late reply 

 

the problem is this only

 

  1. echo 'Select a group :';
  2. echo '<select name="code1">';
  3. echo '<br>';
  4. while($row2=mysql_fetch_array($query2)){
  5. echo '<br>';
  6. echo "<option value=\"" . $row2['g_code'] . "\">" . $row2['g_code'] . "</option> ";
  7. }
  8. echo '</select>';
  9. echo '<input type="submit" name="submit" value="GO">';
  10. echo '</form>';
  11. if(isset($_POST['submit'])){
  12.  
  13. $go = $_POST['code1'];
  14. $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 :)

Link to comment
https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506762
Share on other sites

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'";
Link to comment
https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506813
Share on other sites

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!';
}
Link to comment
https://forums.phpfreaks.com/topic/294895-getting-value-from/#findComment-1506831
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.