Forum Moderators: coopster

Message Too Old, No Replies

data from Drop Down List

         

sfast

9:22 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



I have a drop down list in the form and when I submit the form, only the first word in that selected list gets inserted in the database.

Example app_applyfor= "Customer Service"
Only Customer gets inserted in the table.

Can somebody guide me what am I missing here?

Thanks


echo '<select name="app_applyfor" >';
if ( $app_applyfor!= "")
echo "<option value=".$app_applyfor." selected> " .$app_applyfor. "</option>" ;
else
{
echo '<option value="" selected>Select Position </option>' ;
for ($iPO = 0; $iPO < $totalPO; $iPO++)
{
echo '<option value="' , $positionList[$iPO] , '"';
echo '>', $positionList[$iPO] , '</option>';
}
echo '</select>' ;


if (($_POST['app_applyfor'] ) == '')
$applyforError = "Invalid or missing information" ;
else
$app_applyfor = $_POST['app_applyfor'] ;

eelixduppy

10:39 pm on Dec 18, 2007 (gmt 0)



Where is your query? Make sure you are using quotes appropriately in the query, as well as escaping user-defined variables in your query.

sfast

10:54 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



I should have written the query as well.


$sql = sprintf("INSERT INTO applicant (app_applyfor, region) VALUES ('%s', '%s') ", $app_applyfor, $region );
$result = mysql_query($sql);

eelixduppy

7:18 pm on Dec 23, 2007 (gmt 0)



echo out the value to see if it's all there because it might not be:

echo $app_applyfor;
$sql = sprintf("INSERT INTO applicant (app_applyfor, region) VALUES ('%s', '%s')", $app_applyfor, $region );
echo $sql; #check the syntax here
$result = mysql_query($sql) or die(mysql_error()); #make sure you don't get errors

Try the above code. Make sure everything is as correct as it seems to be. Also, you should be escaping your variables with mysql_real_escape_string [php.net] so it would look more like this:


$sql = sprintf("INSERT INTO applicant (app_applyfor, region) VALUES ('%s', '%s')", mysql_real_escape_string($app_applyfor), mysql_real_escape_string($region));

mooger35

11:50 pm on Dec 24, 2007 (gmt 0)

10+ Year Member



Just going over this quickly it seems to me there are 2 errors.

echo '<select name="app_applyfor" >';
if ( $app_applyfor!= "")
echo "<option value=\"".$app_applyfor."\" selected> " .$app_applyfor. "</option>" ;
// added quotes to contain value of $app_applyfor
else
{
echo '<option value="" selected>Select Position </option>' ;
for ($iPO = 0; $iPO < $totalPO; $iPO++)
{
echo '<option value="' , $positionList[$iPO] , '"';
echo '>', $positionList[$iPO] , '</option>';
}
} // missing an end bracket. I assume it goes here?
echo '</select>' ;

eelixduppy

12:07 am on Dec 25, 2007 (gmt 0)



Nice catch, mooger35. That is definitely it. :)

mooger35

6:11 pm on Dec 25, 2007 (gmt 0)

10+ Year Member



and actually... on second look. The end bracket isn't what was missing, it was an extra beginning bracket that should be taken out.

I think this will do what you want.

echo '<select name="app_applyfor" >';
if ( $app_applyfor!= "")
echo "<option value=\"".$app_applyfor."\" selected> " .$app_applyfor. "</option>" ;
else
echo '<option value="" selected>Select Position </option>';

for ($iPO = 0; $iPO < $totalPO; $iPO++)
{
echo '<option value="' , $positionList[$iPO] , '">', $positionList[$iPO] , '</option>';
}
echo '</select>' ;

sfast

5:09 pm on Jan 3, 2008 (gmt 0)

10+ Year Member



Thanks a lot, Mooger35.

That was a great help.