Current location: Hot Scripts Forums » Programming Languages » PHP » drop down select - php/mysql help


drop down select - php/mysql help

Reply
  #1 (permalink)  
Old 01-09-10, 08:36 AM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
drop down select - php/mysql help

First of all sorry for my English

Im a php coding newbie..i'm currently testing

doing a simple program for xoops cms...

i'm having trouble with this can anyone help me

ok i have this



two table

info_unit : log_id, occupation
myocc : id, occ_list


input

PHP Code:


<select name='occupation'>
 <option value="">Sellect Occupation</option>
   <?php
 
global $xoopsDB
 
$result mysql_query("SELECT * FROM ".$xoopsDB->prefix("myocc")."");
 
 while(
$row mysql_fetch_array($result))
{
$occ_list=$row['occ_list'];
echo 
"<option value='$occupation'>$occ_list</option>";
}
?>


</select>
the above code working properly...the drop down will display value from myocc..

and the value is submitted to info_unit table successfully

the problem is this code below when i want to edit a record in the dit form
the value that i selected in the input form is not display/choosen



update record

PHP Code:


<?php
$log_id
=$_POST[log_id];

if(
$log_id=='')
{
echo 
"<center>Please Select a record</center><br /><br />";
}

else 
{
$result mysql_query("SELECT * FROM info_unit WHERE log_id=$log_id");
while(
$row mysql_fetch_array($result))
{
$log_id=$row['log_id'];
$occupation=$row['occupation'];
}
?>
<select name='occupation'>
<option value=''>Select occupation</option>
 
<?php
 
  
global $xoopsDB
  
$result mysql_query("SELECT * FROM ".$xoopsDB->prefix("myocc")."");

 while(
$row mysql_fetch_array($result))
{
$occ_list=$row['occ_list'];
 
        if(
$occupation == '$occ_list') {
    
    echo 
"<option value='$occupation' selected>$occ_list</option>";
}
 
else
{   

echo 
"<option value='$occupation'>$occ_list</option>";
}  

}
?>

  </select>

Last edited by sarahmx; 01-09-10 at 08:43 AM.
Reply With Quote
  #2 (permalink)  
Old 01-09-10, 10:20 AM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Try this, you was missing some syntax in the mysql_query. I added some validation to it as well.

PHP Code:

<?php
$log_id
= (isset($_POST['log_id']) && is_numeric($_POST['log_id'])) ? intval($_POST['log_id']) : die('You did not specify a valid log id number');

if(
$log_id=='')
{
echo 
"<center>Please Select a record</center><br /><br />";
}

else 
{
$result mysql_query("SELECT * FROM `info_unit` WHERE `log_id`='$log_id'");
if(
mysql_num_rows($result) > 0) {
$row mysql_fetch_array($result);
$log_id=$row['log_id'];
$occupation=$row['occupation'];
}
else {
    echo 
'The log you selected does not exist!';
}
?>
<select name='occupation'>
<option value=''>Select occupation</option>
 
<?php
 
  
global $xoopsDB
  
$result mysql_query("SELECT * FROM ".$xoopsDB->prefix("myocc")."");

 while(
$row mysql_fetch_array($result))
{
$occ_list=$row['occ_list'];
 
        if(
$occupation == '$occ_list') {
    
    echo 
"<option value='$occupation' selected>$occ_list</option>";
}
 
else
{   

echo 
"<option value='$occupation'>$occ_list</option>";
}  

}
?>

  </select>
Reply With Quote
  #3 (permalink)  
Old 01-09-10, 10:45 AM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
Hi jcbones

thanks a lot for the validation...i'm still learning....

but its not working

no value is selected ..i only see 'select occupation'

yeah.....in the input code
PHP Code:

<select name='occupation'>
 <option value="">Sellect Occupation</option>
   <?php
 
global $xoopsDB
 
$result mysql_query("SELECT * FROM ".$xoopsDB->prefix("myocc")."");
 
 while(
$row mysql_fetch_array($result))
{
$occ_list=$row['occ_list'];
$occupation=$occ_list;
echo 
"<option value='$occupation'>$occ_list</option>";
}
?>


</select>
i forgot to put
$occupation=$occ_list; other occupation is empty
Reply With Quote
  #4 (permalink)  
Old 01-09-10, 02:41 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
I'm sorry, I mis-understood the situation.



All it does is:
PHP Code:

isset() //Check to make sure a variable is set.
is_numeric() //check to make sure this is a number, but it could be a positive number(+) or a float(.);
intval() // will change a positive, negative, or float to a integer(whole number).
die() //Stops execution of the page, and prints out the string. synonym for exit(); 
So, it's working now?
Why is $occupation empty from the first query? Is it not a valid column in the table?
Why couldn't you write your option line as : <option>$occ_list</option>? If you don't specify a value, the option returns the text, and since they are the same...
Reply With Quote
  #5 (permalink)  
Old 01-09-10, 07:20 PM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks

ok nevermind i solved this

i just changed the if statement and mysql query...

now i have to validate using jcbones code....

PHP Code:


<?php
$log_id
=$_POST[log_id];

if(
$log_id=='')
{
echo 
"<center>Please Select a record</center><br /><br />";
}

else 
{
$result mysql_query("SELECT * FROM info_unit WHERE log_id=$log_id");
while(
$row mysql_fetch_array($result))
{
$log_id=$row['log_id'];
$occupation=$row['occupation'];
}
?>
<select name='occupation'>
<option value=''>Select occupation</option>
 
<?php
 
  
global $xoopsDB


 while(
$row mysql_fetch_array($result))
{
$occ_list=$row['occ_list'];
$occupation=$row['occupation'];
 
        if(
$occ_list == $occupation) {
    
    echo 
"<option value='$occupation' selected>$occ_list</option>";
}
 
else
{   
$occupation=$occ_list;
echo 
"<option value='$occupation'>$occ_list</option>";
}  

}
?>

  </select>

Last edited by sarahmx; 01-09-10 at 07:34 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto populate drop down list? awiekupo PHP 6 10-31-09 11:47 AM
dynamic drop down missing something soloWebDev PHP 3 12-26-07 04:53 AM
Help - SQL syntax curious Database 2 01-05-07 05:13 PM
Drop down menu population from another and db update minority ASP 15 07-22-05 02:03 AM
Drop down trouble phplearner PHP 1 07-15-05 12:39 PM


All times are GMT -5. The time now is 08:06 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.