Form to edit mysql records. how to do select field

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

donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Form to edit mysql records. how to do select field

Post by donny »

hello,

i have a page where people can edit their profile information. the page pulls data from a database and puts it right into text input forms so they can change what they need to and then click update. the process page simply updates the database row information. all that works fine but i need to know how make it so they can edit a field that was a select field. so when they go to the edit page there will be a select field for their state and the state they already have in the database will automatically be selected.. so if they go to edit their info their name and address lines appear in text inputs and their state will be a drop down select field option with their current state already selected.

anybody can point me in the right direction so i can make this work?

thank you.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form to edit mysql records. how to do select field

Post by Celauran »

You need to grab the elements for the select from wherever they're defined, iterate over them to generate the select, and compare against the user's profile.

Pseudo code, obviously, but this is the idea.

Code: Select all

<select name="foo">
	<?php foreach ($foo_values as $value): ?>
	<option value="<?= $value->id; ?>" <?= ($value->id == $user->foo) ? 'selected="selected"' : ''; ?>>
		<?= $value->name; ?>
	</option>
	<?php endforeach; ?>
</select>
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Form to edit mysql records. how to do select field

Post by donny »

so i would put all the states into an array as $foo_values?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form to edit mysql records. how to do select field

Post by Celauran »

You've already got the states listed somewhere, no? Just iterate over that array.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Form to edit mysql records. how to do select field

Post by donny »

i just had them on the html page on the form code. sorry i am new this is gunna take me some time to figure out lol
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form to edit mysql records. how to do select field

Post by Celauran »

That's something you're going to want to reuse, so I'd move that array either to a config file that's available everywhere, into the database, or into a method that returns an array. Anything that's going to make them easily and globally accessible.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Form to edit mysql records. how to do select field

Post by donny »

there is only going to be 1 or 2 pages with the states field so i can do it on the page i need it on.. can you please show me an example with like 3 states in it.. then tell me what i need to assign to variables to make them work.. like $thisvariable is the state in the database . i need to make sure thats set.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Form to edit mysql records. how to do select field

Post by donny »

also what will the final option tag look like with that script you made? the whole selected="selected" got me confused.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form to edit mysql records. how to do select field

Post by Celauran »

Code: Select all

<?php $states = ['New Hampshire', 'New York', 'Vermont']; ?>

<form>
	...
	Stuff
	...
	<select name="state">
		<?php foreach ($states as $state): ?>
		<option value="<?= $state; ?>" <?= ($state == $user['state']) ? 'selected="selected"' : ''; ?>>
				<?= $state; ?>
		</option>
		<?php endforeach; ?>
	</select>
	...
</form>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form to edit mysql records. how to do select field

Post by Celauran »

donny wrote:also what will the final option tag look like with that script you made? the whole selected="selected" got me confused.
That's how you specify which item in the select list is currently selected. I'm comparing the current value against the user's value on each iteration and, when they match, marking the current item as selected.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Form to edit mysql records. how to do select field

Post by donny »

ok what variable needs to be set as the state already in the database
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form to edit mysql records. how to do select field

Post by Celauran »

Depends on how it's stored. The code I posted assumed you retrieved the user from the database, stored it in an array called $user, and you stored the state value in a DB column called state.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Form to edit mysql records. how to do select field

Post by donny »

i am getting an error with this string.


Parse error: syntax error, unexpected '[' in /home/newyorki/public_html/order/order/shipping_info_edit.php on line 20

line 20 = $states = ['NY', 'CA', 'VT'];
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form to edit mysql records. how to do select field

Post by Celauran »

Old version of PHP? You'll need to use array() instead of []

Code: Select all

$states = array('NY', 'CA', 'VT');
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Form to edit mysql records. how to do select field

Post by donny »

Parse error: syntax error, unexpected ';' in /home/newyorki/public_html/order/order/shipping_info_edit.php on line 86


line 86:

Code: Select all

 <option value="<?= $state; ?>" <?= ($state == $tablesqlrow['state'];) ? 'selected="selected"' : ''; ?>>
Post Reply