Log in

View Full Version : drop down value from mysql problem



sarahmx
01-09-2010, 06:02 PM
Hi

First of all sorry for my English

I'm new here and 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




<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>




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 edit form
the value that i selected in the input form is not selected



update record




<?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>

sarahmx
01-10-2010, 01:18 AM
ok nevermind i solved this

i just changed the if statement and sql query




<?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").",".$xoopsDB->prefix("infounit")." WHERE log_id=$log_id");

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>