MjM8082 Posted August 2, 2011 Share Posted August 2, 2011 I am able to add a user to my database with my code. But I can't figure out how to delete a user from my database when the delete button is clicked on. Please help! Thanks. This is the code I have.... I am attaching some Notepad++ files. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted August 2, 2011 Author Share Posted August 2, 2011 My delete button is located on the update_user page Quote Link to comment Share on other sites More sharing options...
voip03 Posted August 2, 2011 Share Posted August 2, 2011 mysql_query("DELETE FROM Persons WHERE LastName='Griffin'"); http://www.w3schools.com/php/php_mysql_delete.asp www.tizag.com/mysqlTutorial/mysqldelete.php Quote Link to comment Share on other sites More sharing options...
IrOnMaSk Posted August 2, 2011 Share Posted August 2, 2011 i'm not openning the zip file hehe,,, what you want to do is probly populate all the users from db onto a a page with that delete button... and use the check button to to delete the user you want to delete by doing the delete from db where user = john blabla... can you post the code instead of zip it... Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted August 2, 2011 Author Share Posted August 2, 2011 Not sure how to do any of that.. New to php still. But mostly everything is working on my database. I'm able to add a user, but I want it to delete the user when I click the delete button on the website. This is the page with the delete button on it... I will post the other pages too. <html xmlns="http://www.w3.org/1999/xhtml"> <!-- the head section --> <head> <title>Update User</title> </head> <!-- the body section --> <body> <h1>Update User</h1> <?php //echo $_GET['id']; if (isset($_POST['btn_update'])) { echo "My update stuff goes here"; } else if (isset($_POST['btn_delete'])) { echo "My delete stuff goes here"; } ?> <form name="my_form" method="post" action="update_user.php"> User Name: <input type="text" name="user_name"><br /> First Name: <input type="text" name="first_name"><br /> Last Name: <input type="text" name="last_name"><br /> Password: <input type="text" name="pwd"><br /> Admin: <input type="text" name="is_admin"> <input type="submit" name="btn_update" value="Update User" /> <input type="submit" name="btn_delete" value="Delete User" /> </form> </body> </html> This is the database page... <?php $dsn = 'mysql:host=localhost;dbname=se266'; $username = 'se266_user'; $password = 'pwd'; try { $db = new PDO($dsn, $username, $password); } catch (PDOException $e) { $error_message = $e->getMessage(); include('database_error.php'); exit(); } ?> This is the home page to add a user <html xmlns="http://www.w3.org/1999/xhtml"> <!-- the head section --> <head> <title>Lab 2</title> </head> <!-- the body section --> <body> <?php require_once('database.php'); if (isset($_POST['btn_add'])) { $user_name = $_POST['user_name']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $pwd = $_POST['pwd']; $is_admin = $_POST['is_admin']; $sql = "INSERT INTO users (user_first, user_last, user_name, "; $sql .= "user_password, user_is_admin) VALUES ('$first_name', '$last_name', '$user_name', '$pwd', $is_admin);"; $db->exec($sql); } $query = "SELECT * FROM users"; $users = $db->query($query); // display each of these users echo "<ul>"; foreach ($users as $u) { $first = $u['user_first']; $last = $u['user_last']; $id = $u['user_id']; echo "<li><a href='update_user.php?id=$id'>$first $last</a></li>"; } echo "</ul>"; ?> <hr /> <form name="my_form" method="post" action="lab2.php"> User Name: <input type="text" name="user_name"><br /> First Name: <input type="text" name="first_name"><br /> Last Name: <input type="text" name="last_name"><br /> Password: <input type="text" name="pwd"><br /> Admin: <input type="text" name="is_admin"> <input type="submit" name="btn_add" value="Add User" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
IrOnMaSk Posted August 2, 2011 Share Posted August 2, 2011 ok,,, here's what I would do: <form method="post" action="deleteuser.php"> <?php //connection goes here if (isset($_POST['submit'])){ foreach($_POST['delete'] as $delete_user){ $query = "delete from dbname where user = $delete_user"; mysql_query($connection, $query) or die ('die now'); } echo 'user has been deleted.<br />'; //display users info with checkbox to delete $query = "select*from dbname"; $result = mysql_query($connection, $query); while($row = mysql_fetch_array($result)){ echo '<input type="checkbox" value="' .$row['user'] . '"name="delete[]" />; echo ' ' .$row['user']; echo '<br />'; } mysql_close($connection); ?> <input type="submit" name="submit" value="Remove" /> </form> dbname is your databse name you connection to $connection is your connection variable tweak it to fit your desire... the code pretty much display all the users you have in the database with checkbox next to them. check the one you want to delete and press the button and it'll delete the one checked... Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted August 2, 2011 Author Share Posted August 2, 2011 None of this is working still, I think there is a error in the code you sent me. Also I'm not sure exactly where to put that code in my code? Quote Link to comment Share on other sites More sharing options...
IrOnMaSk Posted August 2, 2011 Share Posted August 2, 2011 my code should be just by itself just for organizational purposes... or you can put it in your update_user page (i'm guessing that's what you want?)... you know base on your form structure, you can't have add or remove using just that... you need another form for delete user... and my code work just fine if you give it the right info, my code won't work bcause i don't have your ur database name, form name, talbe name in the database, you have to supply those according to your info... or you can me your database info and structure in fill it in for ya So let me know where do you want the delete user for to show up at? what page? and are those three separate codes in three separate page? if you tested my code, tell me what error you got... Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted August 2, 2011 Author Share Posted August 2, 2011 This is my first time building a database and I am new to php, so bare with me lol. I'm using Notepad++ for my php codes. I have 3 separate pages, update_user.php, database.php, and index.php (which is the front page). I tried using your code in Notepad++, but it's giving me grayed out code at the end (<input type="submit" name="submit" value="Remove" /> </form>) like its being commented out or something. It should be lighting up in html colors. What I am trying to do is on the index.php page the user will type in there information and it will go into the MySQL database. (which works great) Next I want the user to be able to click on their username that they created. (which works fine) And finally it will bring them to the update_user.php page which will have a Update and Delete button. There is also a form on the page for them to fill out. And If they click update it will update the information. Or they can click delete so that their name is deleted off the database completely. I will post my database page so that you can see the database name and connection... <?php $dsn = 'mysql:host=localhost;dbname=se266'; $username = 'se266_user'; $password = 'pwd'; try { $db = new PDO($dsn, $username, $password); } catch (PDOException $e) { $error_message = $e->getMessage(); include('database_error.php'); exit(); } ?> All the other code for other pages is posted above in a previous post. Quote Link to comment Share on other sites More sharing options...
IrOnMaSk Posted August 2, 2011 Share Posted August 2, 2011 ok let me get the logic right first... 1) index.php - user can register into your site and if they continue their info is saved into your database 2) So after they completed the process in index.php it will direct them to update_user.php? which they can see the info they just filled in? 3) update_user.php they can either update or delete their info from your database? if that's correct, i have some suggestions for ya... 1) never a good idea to allow user to delete themselves from YOUR database. they should only be able to register themselves and that's it. what you can do is have them notify in some way, and you go delete them from your database... and i fixed the code, i just missed a closing echo in the while loop here's the code, you can test it... <?php //connection goes here $dsn = mysql_connect('localhost', 'se266_user', 'pwd') or die('can\'t connect to the database'); mysql_select_db('se266') or die('can\'t select the database'); if (isset($_POST['submit'])) { foreach($_POST['delete'] as $delete_user) { $query = "delete from se266 where user_name = $delete_user"; mysql_query($dsn, $query) or die ('die now'); } echo 'user has been deleted.<br />'; } //display users info with checkbox to delete $query = "select*from se266"; $result = mysql_query($dsn, $query); while($row = mysql_fetch_array($result)) { echo '<input type="checkbox" value="' .$row['user_name'] . '"name="delete[]" />'; echo ' ' .$row['user_first']; echo ' ' .$row['user_last']; echo '<br />'; } mysql_close($dsn); ?> <input type="submit" name="submit" value="Remove" /> </form> concerning the code, it's the best idea that you the only one can delete from the database... but of course that the basic step to use if you want to delete user from the database... oh and i'm totally baring with you lol Let's continue to work on this, it's fun for me Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted August 3, 2011 Author Share Posted August 3, 2011 Okay thanks for the reply! Sorry I was at work, but I am home now. I put in the code you gave me and the only thing that pops up on the screen is a remove button when I go to the website. I'm not sure if I need to put this code in a certain spot? And YES! You have all the steps down correctly. Quote Link to comment Share on other sites More sharing options...
IrOnMaSk Posted August 3, 2011 Share Posted August 3, 2011 srry, i was little bit bz. now i'm back ok, if you see only the remove button and no error which means it work!!! do you have any data in your database? if the database you connect to is empty then it will just show nothing because it pulling the data from it... and also i'm thinking that the database you're using is for testing right? if so can you give me the structure of your database, like the table columns... from your code it seems like you have this database "se266" with columns user_name, user_first, user_lastname, pwd, is_admin? ok tell you what, i'ma create a test database exactly as that, give me about 30 mins, b right back Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted August 3, 2011 Author Share Posted August 3, 2011 Yeah thats exactly what I am doing. I'm using micrsoft workbench for my database.. connecting with localhost. This is what the database code looks like... DROP DATABASE IF EXISTS se266; CREATE DATABASE se266; use se266; CREATE TABLE users ( user_id INT(11) NOT NULL AUTO_INCREMENT, user_first VARCHAR(50), user_last VARCHAR(50) NOT NULL, user_name VARCHAR(50) NOT NULL, user_password VARCHAR(50), user_is_admin BIT, PRIMARY KEY (user_id) ); -- create the users and grant priveleges to those users GRANT SELECT, INSERT, DELETE, UPDATE ON se266.* TO se266_user@localhost IDENTIFIED BY 'pwd'; INSERT INTO users (user_first, user_last, user_name, user_password, user_is_admin) VALUES ('Matt', 'McCarthy', 'matt', 'pwd', 1); use se266; SELECT * FROM users; So once it creates the database I went over to Notepad++ and made my form. On the website I want the form to show a update and delete button and add button. The add button works. I fill out the form and then the information goes into the database. Now I want to be able to delete that when i click the delete button on the website. I know I probably repeated my self multiple times but I just want to make it as clear as possible for you, what i am trying to do, and working with. Quote Link to comment Share on other sites More sharing options...
IrOnMaSk Posted August 3, 2011 Share Posted August 3, 2011 try this, explaination in the inbox [attachment deleted by admin] 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.