yogibear Posted January 19, 2007 Share Posted January 19, 2007 HiI am very new to everything php so please go easy. This is what I want a drop down menu with content from a mysql database and when a user selects one of the options in the menu it is deleted from the database, is this possible.This is what i have so far[code]<?php$connect = mysql_connect("localhost","***","***") ordie ("Could not connect to database.");mysql_select_db("***");$result = mysql_query('SELECT last_name, first_name FROM userinformation')or die (mysql_error());echo '<select name="userlist">';while ($row = mysql_fetch_assoc($result)){echo '<option value="' . $row['last_name'] . '">' . $row['first_name']. '</option>';}echo '</select>';?>[/code]Many thanks I look forward to hearing from youyogibear Quote Link to comment Share on other sites More sharing options...
.josh Posted January 19, 2007 Share Posted January 19, 2007 okay, so you make a form with that dropdown loop, and when user selects the info and hits the submit button, delete from userinformation where last_name = $_POST['userlist'] Quote Link to comment Share on other sites More sharing options...
micmania1 Posted January 19, 2007 Share Posted January 19, 2007 [quote author=Crayon Violent link=topic=123164.msg508732#msg508732 date=1169232087]okay, so you make a form with that dropdown loop, and when user selects the info and hits the submit button, delete from userinformation where last_name = $_POST['userlist'][/quote]One problem with that though is if there are two people with the same last name sign up, im not sure whether it would delete just 1 or both.if you have a user_id column make that the option value. If you don't, create one. Make sure its auto incremented.From the look of the coding you havnt got the form tags or a submit button on there so put one of the on. Make the form action come back to the page your on and make the method="post".add this before the $result query put this...[code]if (isset($_POST['userlist'])) { $user_id = $_POST['userlist']; $result = mysql_query("DELETE FROM userinformation WHERE user_id={$_POST['userlist']}"); $affrows = mysql_affected_rows(); if ($affrows = 1) { echo 'Successfully deleted user.<br><br>'; }}// Display your existing code.[/code]You may also like to ask the user if they're sure about deleting the user so its not as easy to make a mistake.Hope this helps. :) Quote Link to comment Share on other sites More sharing options...
yogibear Posted January 19, 2007 Author Share Posted January 19, 2007 [quote author=Crayon Violent link=topic=123164.msg508732#msg508732 date=1169232087]okay, so you make a form with that dropdown loop, and when user selects the info and hits the submit button, delete from userinformation where last_name = $_POST['userlist'][/quote]That makes sense, can you tell me how to add delete from userinformation where last_name = $_POST['userlist'] to the submit button.thanksyogi Quote Link to comment Share on other sites More sharing options...
.josh Posted January 19, 2007 Share Posted January 19, 2007 [quote]One problem with that though is if there are two people with the same last name sign up, im not sure whether it would delete just 1 or both.[/quote]It would delete both. I agree with using a unique Id. But I'm not here to show him how to write [i]better[/i] code...I'm here just to help him with his current problem. yogibear: you would do it just like micmania1 has shown. Quote Link to comment Share on other sites More sharing options...
yogibear Posted January 19, 2007 Author Share Posted January 19, 2007 Hithis is what i have put[code]<form action="new.php" method="post"><?php$connect = mysql_connect("localhost","***","***") ordie ("Could not connect to database.");mysql_select_db("***");if (isset($_POST['userlist'])) { $user_id = $_POST['userlist']; $result = mysql_query("DELETE FROM userinformation WHERE user_id={$_POST['userlist']}"); $affrows = mysql_affected_rows(); if ($affrows = 1) { echo 'Successfully deleted user.<br><br>'; }}// Display your existing code.$result = mysql_query('SELECT last_name, first_name FROM userinformation')or die (mysql_error());echo '<select name="userlist">';while ($row = mysql_fetch_assoc($result)){echo '<option value="' . $row['last_name'] . '">' . $row['first_name']. '</option>';}echo '</select>';?><input type="submit" />[/code]I don’t get any errors and when I click submit it says Successfully deleted user but nothing is deleted I’m sure its something I have done or not done I’m not very confident with my code.Thank for your help Quote Link to comment Share on other sites More sharing options...
printf Posted January 19, 2007 Share Posted January 19, 2007 I think this...[code]echo '<option value="' . $row['last_name'] . '">' . $row['first_name']. '</option>';[/code]should be[code]echo '<option value="' . $row['user_id'] . '">' . $row['first_name'] . ' ' . $row['last_name']</option>';[/code]Then add [b]user_id, [/b] to the beginning of the SELECT part of the query that is filling the select box!In other words, your trying to delete a user based on the [b]last_name[/b] value, when your [b]WHERE[/b] is using the [b]user_id[/b] column!printf Quote Link to comment Share on other sites More sharing options...
micmania1 Posted January 19, 2007 Share Posted January 19, 2007 You need to create a new colum in your userinformation table called user_id, and make it auto_incremented.Chage the parts I have put in bold...[quote]<form action="new.php" method="post"><?php$connect = mysql_connect("localhost","***","***") ordie ("Could not connect to database.");mysql_select_db("***");if (isset($_POST['userlist'])) { $user_id = $_POST['userlist']; $result = mysql_query("DELETE FROM userinformation WHERE user_id={$_POST['userlist']}"); $affrows = mysql_affected_rows(); if ($affrows = 1) { echo 'Successfully deleted user.<br><br>'; }}// Display your existing code.$result = mysql_query('SELECT last_name, first_name FROM userinformation')or die (mysql_error());echo '<select name="userlist">';while ($row = mysql_fetch_assoc($result)){echo '<option value="' . [b]$row['user_id'] [/b]. '">' . $row['first_name']. '</option>';}echo '</select>';?><input type="submit" />[b]</form>[/b][/quote] Quote Link to comment Share on other sites More sharing options...
yogibear Posted January 19, 2007 Author Share Posted January 19, 2007 IT WORKS! ;DThank you so much for all your help everyone Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.