MNSarahG Posted February 5, 2008 Share Posted February 5, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/ Share on other sites More sharing options...
affordit Posted February 5, 2008 Share Posted February 5, 2008 Post the code for deleteUser2.php Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-459143 Share on other sites More sharing options...
MNSarahG Posted February 5, 2008 Author Share Posted February 5, 2008 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-459150 Share on other sites More sharing options...
MNSarahG Posted February 5, 2008 Author Share Posted February 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-459156 Share on other sites More sharing options...
affordit Posted February 5, 2008 Share Posted February 5, 2008 Do you get any errors? What happens I assume you get record could not be deleted? Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-459160 Share on other sites More sharing options...
affordit Posted February 5, 2008 Share Posted February 5, 2008 Try this add the first line to the top of the page, I think I got the syntax right $emp=$_POST['employeeID']; mysql_query("DELETE FROM $table WHERE employeeID='$emp'"); Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-459165 Share on other sites More sharing options...
haku Posted February 5, 2008 Share Posted February 5, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-459258 Share on other sites More sharing options...
Moon-Man.net Posted February 6, 2008 Share Posted February 6, 2008 haku is correct with the form name tags. no need. He is also correct on the posting issue, you could also use: <form action=deleteUser2.php method="get"> and $sql = 'DELETE FROM employees WHERE employeeID='.$_GET['deleteUser].''; Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-459273 Share on other sites More sharing options...
MNSarahG Posted February 7, 2008 Author Share Posted February 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-461263 Share on other sites More sharing options...
Moon-Man.net Posted February 7, 2008 Share Posted February 7, 2008 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'].'\''; Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-461270 Share on other sites More sharing options...
MNSarahG Posted February 7, 2008 Author Share Posted February 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-461275 Share on other sites More sharing options...
Moon-Man.net Posted February 7, 2008 Share Posted February 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89613-solved-formsdrop-downs/#findComment-461284 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.