Jump to content

Deleting using a drop down menu


yogibear

Recommended Posts

Hi

I 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","***","***") or
die ("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 you
yogibear
Link to comment
https://forums.phpfreaks.com/topic/34906-deleting-using-a-drop-down-menu/
Share on other sites

[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 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.

thanks

yogi
[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. 
Hi

this is what i have put
[code]<form action="new.php" method="post">
<?php

$connect = mysql_connect("localhost","***","***") or
die ("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
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
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","***","***") or
die ("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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.