Jump to content

Recommended Posts

Hi,

 

I'm trying to get this Delete User form to work right, and I'm stuck on the part that should be easy.  After connecting to the DB, I've got code that queries the database and outputs a list of names into a drop down that works, but what I want the form to do is go to another page with the Delete script (which also works) and carry the employeeID variable over in the URL to delete the right user.  I think I'm close...

 

if ($result = mysqli_query($link, 'SELECT employeeID, firstName, lastName FROM employees ORDER BY lastName')) {

?>

<form name="deleteUser" action="deleteUser2.php?employeeID=<?php $_POST['employeeID'] ?> ">
<select name="deleteUser">

<?php
   while( $row = mysqli_fetch_assoc($result) )
   {printf("<option>%s, %s</option>\n", $row['lastName'], $row['firstName'], $row['employeeID']);} ?>
   
   </select>
   <br /><br />
   <input type="submit" value="Delete" />
   
</form>

Link to comment
https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/
Share on other sites

Here's the action page...

 

<?php

$hostname = 'localhost';
$username = 'zzz';
$password = 'zzz';

$mysqli = @new mysqli($hostname, $username, $password, 'greenspring');

if(!mysqli_connect_errno())
    {
    
$ID = $_GET['employeeID'];

    $sql = 'DELETE FROM employees WHERE employeeID='.$_GET["employeeID"].'';

    if($mysqli->query($sql) === TRUE)
        {
        echo 'Record Deleted successfully<br />';
        }
    else
        {
        echo 'Unable to Delete Record<br />'.$sql.'<br />' . $mysqli->error;
        }

    $mysqli->close();
    }
else
    {

    echo 'Unable to connect';
    exit();
    }
?>

Also... here's the error I get:  "Unable to Delete Record

DELETE FROM employees WHERE employeeID=

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

 

Because the script from the first page goes to this URL - deleteUser2.php?deleteUser=German%2C+Sarah - for a user with the first name Sarah and last name German.  If I put in the URL I want it to go to - deleteUser2.php?employeeID=3 (in this case... the 3 is the variable) - the action works alright.  Something's just off with my form.

There are a couple problems. First, your form name and your select name are the same. You shouldn't do that. Also, form names are deprecated, so you should actually take name right out of your <form> tag.

 

Second:

 

<form name="deleteUser" action="deleteUser2.php?employeeID=<?php $_POST['employeeID'] ?> ">

 

Where are you trying to get $_POST['employeeID'] from?

 

This is actually where your problem is. When this form is submitted, it will go to this address, but $_POST['employeeID'] is not set, so when it goes to the next page and trys to get $_GET['employeeID'], its empty.

But it doesn't make sense to put the employeeID into the action anyways - you haven't selected an employee to delete. What you need to do is remove the employeeID from the URL, and then on your second page, change your script to this:

 

$sql = 'DELETE FROM employees WHERE employeeID='.$_POST['deleteUser].'';

 

But this may not work until you take the name attribute out of your <form> tag, since your form is also named deleteUser.

 

Good luck!

Thanks so much for the replies!  I'm really close... I followed Haku's suggestions and it almost works.  My error message now is this:

Unable to Delete Record

DELETE FROM employees WHERE employeeID=German, Sarah

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' Sarah' at line 1

 

Here, the "German, Sarah" is the name that's on the drop-down from the 1st page that I selected to delete.  What needs to happen is that the employeeID associated with that name makes it into the SQL DELETE instead of the name so that the delete action can work.  Any suggestions?

 

 

When the SQL server gets that string, any values (not table/field names) need to be in quotes.

DELETE FROM employees WHERE employeeID=German, Sarah

needs to read:

DELETE FROM employees WHERE employeeID='German, Sarah'

so your SQL line to:

$sql = 'DELETE FROM employees WHERE employeeID=\''.$_POST['deleteUser'].'\'';

Sorry... I didn't explain properly (that totally did work though!).  The employeeID is a number and in its own column in the DB (on the same table as firstName and lastName).  The idea is that the user picks the name from the drop down, hits Delete, and the SQL query deletes the record based on the employeeID.  So I need to carry that over to the action page somehow - that's why I was trying to get it to go through on the URL.  Any ideas?  Thanks again for the replies.

Ok, well we are very close.

The issue here now is the first page, where the <option> tag is built. you need a "value=" bit in each of your options containing the employee ID.

Replace your printf line with this

{printf("<option value=%s>%s, %s</option>\n", $row['employeeID'], $row['lastName'], $row['firstName']);} ?>

Let me know if you don't understand anything and I'll explain in a little more detail.

There's no use having it work and you not understand what was wrong.  ::)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.