Select Option

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Select Option

Post by nitediver »

Im using this page for updating data inside db.

Code: Select all

<?
if(isset($_POST['submit'])){
$user_name = $_POST['user_name'];
$user_address = $_POST['user_address'];
$user_city= htmlentities($_POST['user_city'], ENT_QUOTES);

$qupdate = mysql_query("UPDATE user SET user_name='$user_name',user_address='$user_address',user_city='$user_city' WHERE user_id='$user_id'");
}

function city(){
global $quser;
$qship =mysql_query("SELECT DISTINCT ship_city FROM ship ORDER BY ship_city ASC");
echo "<select name=\"user_city\"><option select> - choose city - </option>";
while($rship =  mysql_fetch_array($qship))
	{echo "<option value=\"$rship[ship_city]\">$rship[ship_city]</option>";}
echo "</select>";
echo " $quser[user_city]";
}
?>
Problem:
Current Value
[text]
user_name address city
john street 1 nyc
[/text]

After Update(if I only change the user_name=max)
[text]
user_name address city
max street 1 choose city
[/text]

Even I dont use "choose city" as default, it will post first value on the list.
How stop the city from keep submit into db?
Is there anyway to post only what I want to post?
Like if I only change the user_name, and keep the current address and city.

thanks.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Select Option

Post by andyhoneycutt »

You'll want to see if they've actually selected anything. The way I would do this is set the 'choose city' option value to nothing, then look to see if it has a value before you update:

Code: Select all

<?
//... check to see if user_city even has a value. If it doesn't, run the query that 
//... doesn't have user_city in the update...
$user_city= htmlentities($_POST['user_city'], ENT_QUOTES);
$query    = ($user_city != '') ? 
	"UPDATE user SET user_name='$user_name', user_address='$user_address', user_city='$user_city' WHERE user_id='$user_id'" 
	: 
	"UPDATE user SET user_name='$user_name', user_address='$user_address' WHERE user_id='$user_id'";
$qupdate = mysql_query($query);

//... give the first, default option a nil value.
function city(){
global $quser;
$qship = mysql_query("SELECT DISTINCT ship_city FROM ship ORDER BY ship_city ASC");
echo "<select name=\"user_city\"><option value=\"\"> - choose city - </option>";
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Select Option

Post by nitediver »

Never mind I already solved it, thanks anyway.
Post Reply