/    Sign up×
Community /Pin to ProfileBookmark

PHP and MySQL – Drop down box to display data

I have the following code:

[code=php]<?php
if(isset($select)&&$select!=””){
$select=$_GET[‘select’];
}
?>

<form id=”form1″ name=”form1″ method=”get” action=”<? echo $PHP_SELF; ?> “>
Select Councillor :
<select name=”select”>
<option value=””>— Select —</option>

<?php
// Get records from database (table “name_list”).
$list=mysql_query(“select * from members order by firstname asc”);

// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
?>

<option value=”<? echo $row_list[‘id’]; ?>” <? if($row_list[‘id’]==$select){ echo “selected”; } ?>><? echo $row_list[‘id’]; ?> <? echo $row_list[‘firstname’]; ?> <?php echo $row_list[‘surname’]; ?> – <? echo $row_list[‘ward’]; ?></option>

<?php
// End while loop.
}
?>

</select>
<input type=”submit” name=”Submit” value=”Select” />
</form>
<hr>
<p>
<?php
// If you have selected from list box.
if(isset($select)&&$select!=””){

// Get records from database (table “name_list”).
$result=mysql_query(“select * from mebers where id=’$select'”);
$row=mysql_fetch_assoc($result);
?>
Are you sure you would you like to delete <? echo $row[‘firstname’]; ?> <? echo $row[‘surname’]; ?> from the Database?</p>

<?php
// End if statement.
}

?>
[/code]

The drop down box is working fine, displaying all the data it should be, but when i select an item from the dropdown and click submit this bit:

[code=php]<?php
// If you have selected from list box.
if(isset($select)&&$select!=””){

// Get records from database (table “name_list”).
$result=mysql_query(“select * from mebers where id=’$select'”);
$row=mysql_fetch_assoc($result);
?>
Are you sure you would you like to delete <? echo $row[‘firstname’]; ?> <? echo $row[‘surname’]; ?> from the Database?</p>
[/code]

doesnt work, it just completely skips the PHP and just displays:

“Are you sure you would you like to delete from the Database?”

instead of:

“Are you sure you would you like to delete [B]Firstname lastname[/B] from the Database?”

Cheers,

Dan

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@flashcusNov 15.2009 — [CODE]while($row=mysql_fetch_assoc($result)) {
?>
Are you sure you would you like to delete <? echo $row['firstname']; ?> <? echo $row['surname']; ?> from the Database?</p>

<?php
//end while loop
}
... [/CODE]


You need a while loop ?
Copy linkTweet thisAlerts:
@dankellysauthorNov 15.2009 — tried:

[code=php]<?php
// If you have selected from list box.
if(isset($select)&&$select!=""){

// Get records from database (table "name_list").
$result=mysql_query("select * from mebers where id='$select'");
while ($row=mysql_fetch_assoc($result)) {
?>
Are you sure you would you like to delete <? echo $row['firstname']; ?> <? echo $row['surname']; ?> from the Database?</p>

<?php
// End While loop
}
// End if statement.
}
?>[/code]


Now i dont see that line at all... :s
Copy linkTweet thisAlerts:
@flashcusNov 15.2009 — your query is missing an "m" in members ?
Copy linkTweet thisAlerts:
@dankellysauthorNov 16.2009 — hahaha thank you very much, worked a treat.

I could do with one more tiny piece of help ? So now that i have the right code to get the member, i want to then create a delete button undernieth.

So, i currently have this:

[code=php]<?php
if(isset($select)&&$select!=""){
$select=$_GET['select'];
}
?>
<?php
if(isset($delete)&&$delete!=""){
$delete=$_GET['delete'];
}
?>
<form id="form1" name="form1" method="get" action="<? echo $PHP_SELF; ?> ">
Select Councillor :
<select name="select">
<option value="">--- Select ---</option>

<?php
// Get records from database (table "name_list").
$list=mysql_query("select * from members order by firstname asc");

// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
?>

<option value="<? echo $row_list['id']; ?>" <? if($row_list['id']==$select){ echo "selected"; } ?>><? echo $row_list['firstname']; ?> <?php echo $row_list['surname']; ?> - <? echo $row_list['ward']; ?></option>

<?php
// End while loop.
}
?>

</select>
<input type="submit" name="Submit" value="Select" />
</form>
<hr>
<p>
<?php
// If you have selected from list box.
if(isset($select)&&$select!=""){

// Get records from database (table "name_list").
$result=mysql_query("select * from members where id='$select'");
while ($row=mysql_fetch_assoc($result)) {
?>
Are you sure you would you like to delete <strong><? echo $row['firstname']; ?> <? echo $row['surname']; ?></strong> from the Database?</p>

<?php
// End While loop
}
// End if statement.
}
?>
[/code]


So after [code=php]Are you sure you would you like to delete <strong><? echo $row['firstname']; ?> <? echo $row['surname']; ?></strong> from the Database?[/code] that bit, i want a button to delete the selected user, but just cant figure it out.

Im new to PHP ?

thank you

Dan
×

Success!

Help @dankellys 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.9,
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,
)...