/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] How to Populate a Select Box from MySQL DB

Day 3 on this and I’m no closer than when I started. I have a db with various fields. One of them is fname. I’m trying to populate a Select box’s options with it. A daunting task if there ever was one! Here is my code:

[code=php]
<form method=”post” action=”<?php echo htmlentities($_SERVER[‘PHP_SELF’]);?>”>
<select name=”view”>
<?php
require_once ‘root_login.php’;
try{
$stmt = $db->prepare(“SELECT fname FROM attendees”);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) { ?>
<option value=”<?php echo $row[‘fname’]; ?>”><?php echo $row[‘fname’]; ?></option>
<?php }

catch (PDOException $ex){
/*** if we are here, something has gone wrong with the database ***/
echo ‘We are unable to process your request. Please try again later’;
}
?>

</select>
<input type=”submit” value=”Go”>
</form>[/code]

Here is root_login.php:

[code=php]
<?php
$db = new PDO(‘mysql:host=localhost;dbname=myDB’, ‘adminUsername’, ‘myPW’);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
?>[/code]

I get no errors. What I do get is an empty Select box and the button. Not one field value gets put in the Select box. If you know how to do this, will you please show me [B]how[/B] it’s done? I’m really struggling with this.

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@rootFeb 28.2015 — you look like you are missing a brace from your try / catch at this point --> catch (PDOException $ex){

if you look at the closing brace <?php } <-- here, this corresponds to the opening brace at the foreach loop.

[code=php]<?php
require_once 'root_login.php';
try{
$stmt = $db->prepare("SELECT fname FROM attendees");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) { ?>
<option value="<?php echo $row['fname']; ?>"><?php echo $row['fname']; ?></option>
<?php }

}catch (PDOException $ex){
/*** if we are here, something has gone wrong with the database ***/
echo 'We are unable to process your request. Please try again later';
}
?>[/code]


My sugesttion is that you use a text editor like Notepad++ which helps you identify mismatched code blocks, nests, etc.
Copy linkTweet thisAlerts:
@LandslydeauthorFeb 28.2015 — You're right about that. I caugh that after my post and forgot to update. But that bracket in place does nothing to my failing code. I have also tried this:

[code=php]<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<select name="view">
<?php
require_once 'root_login.php';
try{
$stmt = $db->prepare("SELECT fname FROM attendees");
$sth = $db->query($stmt);
while($row = $sth->fetch(PDO::FECTH_ASSOC)) { ?>
<option value="<?php echo $row['fname']; ?>"><?php echo $row['fname']; ?></option>
<?php }
}
catch (PDOException $ex){
/*** if we are here, something has gone wrong with the database ***/
echo 'We are unable to process your request. Please try again later';
}
?>

</select>
<input type="submit" value="Go">
</form>[/code]

I have several rows in my db, so fname is there. But my code is wrong. Can someone please help? A small example is all I'm asking for.
Copy linkTweet thisAlerts:
@LandslydeauthorFeb 28.2015 — Persistance pays dividends ?

I finally got my code to work, so I thought I'd post it to help others:

[code=php]
<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<select name="view">
<?php
require_once 'root_login.php';

$stmt = $db->prepare('SELECT * FROM attendees');
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row){ ?>
<option><?php echo $row[1]; ?></option>
<?php }
?>

</select>
<input type="submit" value="Go">
</form>
[/code]

Of course it needs to be in a Try/Catch block. Hope this helps someone when help is hard to find.
Copy linkTweet thisAlerts:
@rootMar 01.2015 — It does, you could have just as easily have with this [code=php]?>
<option value="<?php echo $row['fname']; ?>"><?php echo $row['fname']; ?></option>

[/code]

done this
[code=php]$out = "";
foreach($result as $row){
$out .= sprintf("<option value='%s'>%s</option>rn",$row[1],$row[1]);
}
echo $out;
[/code]
Copy linkTweet thisAlerts:
@NogDogMar 01.2015 — Just because there are many ways to do it...many people like to use the alternate control structure syntax along with the <?= tag to keep things looking more like a templating syntax:
[code=php]
<select name="foo">
<?php foreach($result as $row): ?>
<option value="<?= $row[1] ?>"><?= $row[1] ?></option>
<?php endforeach; ?>
</select>
[/code]
Copy linkTweet thisAlerts:
@LandslydeauthorMar 01.2015 — It wld be easy for you because you know how. I didn't. I was here asking for help, but I got none. So I figured it out the best way I cld. Works for me ?
Copy linkTweet thisAlerts:
@LandslydeauthorMar 01.2015 — It wld be easy for you because you know how. I didn't. I was here asking for help, but I got none. So I figured it out the best way I cld. Works for me ?[/QUOTE]

That was for you, . I appreciate your input, but I've been at this for 3 days and got zero help. If you knew how to help me, why didn't you offer up when I was asking?
Copy linkTweet thisAlerts:
@LandslydeauthorMar 01.2015 — Just because there are many ways to do it...many people like to use the alternate control structure syntax along with the <?= tag to keep things looking more like a templating syntax:
[code=php]
<select name="foo">
<?php foreach($result as $row): ?>
<option value="<?= $row[1] ?>"><?= $row[1] ?></option>
<?php endforeach; ?>
</select>
[/code]
[/QUOTE]


Thank you, NogDog. I have a new book, PHP Cookbook by O'Reilly, but I didn't see anything like that in there. And that's my point, really. I bought it so I wldn't have to come to the forums and ask (sometimes it feels like begging) for help. I see what you've done, though. As for my resulting code, it works ? Was a tough fight, but I won. And no matter which name I select, it porperly $_POSTs. So I'll study on yours but am satisfied with mine. Btw, how many posts do I have to make before I can edit my posts? I had to double-post to . because I wasn't able to edit the one I goofed on. Thanks again. Much appreciated
Copy linkTweet thisAlerts:
@NogDogMar 01.2015 — I believe there is a time limit past when you can no longer edit a post, regardless of user status (except for mods and admins, of course). I'm pretty sure that with your current post count there are no features left to unlock for you -- though admittedly I don't know what any of the exact numbers are, but suspect it's around 10 or so.
Copy linkTweet thisAlerts:
@NogDogMar 01.2015 — That was for you, . I appreciate your input, but I've been at this for 3 days and got zero help. If you knew how to help me, why didn't you offer up when I was asking?[/QUOTE]

Not to put words in his mouth, but maybe because he has a life and doesn't spend every available minute here looking to see who needs help? ? In my case, even as a (unpaid, mind you) moderator here, I generally stop in a handful of times each day, but that can vary a lot based on work and life in general; and admittedly I don't even look at some threads if they look "boring" for some reason (e.g.: yet another poster asking the same question that's been answered here 1000 times -- not saying that about this one, btw) or it looks like someone has already replied and that someone is a member with a track record here of generally knowing what s/he's talking about.

Okay, enough rambling by me -- guess it's not one of those days where I have no time for the forums. ?
Copy linkTweet thisAlerts:
@LandslydeauthorMar 01.2015 — Yeah NogDog, I was kinda bent when I posted that to . I don't make those kind of feelings a habit, and even less do I show them. Anyway, at the bottom of my screen here, I have this:

[CODE]
Posting Permissions

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


BB code is On
Smilies are On
[IMG] code is Off
[VIDEO] code is Off
HTML code is Off

Forum Rules[/CODE]


And one other thing: How do I send personal messages here? Looking for how to do that is what put me to sleep last night. I clicked and clicked and clicked, but nowhere did I find a way to send any user here a pm. I see in the top right of the forum page a Notification bar for me. So I'm assuming pm's are possible. Dunno.
Copy linkTweet thisAlerts:
@NogDogMar 01.2015 — I have a "Private Messages" menu option near the top of the page, just below the "Forum" tab and just to the right of the "New Posts" option. Do you have that?

On further review, I think the admins may have upped the post count when signatures are enabled, due to the myriad of spammers who were making largely meaningless posts until that ability was granted, at which point they'd put their spam links into a sig; and it looks like that ability was tied into one user status setting which might also include the PM ability. I'll be posting a question in the admin/moderator sub-forum to see what they say (and if they want to re-think it or reconfigure things).
Copy linkTweet thisAlerts:
@LandslydeauthorMar 01.2015 — I appreciate that, NogDog. All I have up there are my settings, notifications, etc...no PM stuff.
×

Success!

Help @Landslyde spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...