Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Deleting selected data help

    Hi everyone,

    I'm hoping to get some assistance with deleting selected data from my database as I am having some issues.

    I've created a page where a user and create a profile (multiple profiles). All is done and working fine. I've then echoed the selected data as per an an image below.


    I've echoed some profile details including an edit and delete button
    using
    Code:
    $query = "SELECT * FROM individual_profile WHERE username = '$username'";
    Now what I want is to be able to delete the profile data in my database by clicking the delete button. But i'm having some issues doing this and beleive the query has to be identified by more then just the $username... etc by username, first_name and last_name but i'm not sure how is best to go about it.

    I would really appreciate some helps!

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,293
    Thanks
    4
    Thanked 202 Times in 199 Posts
    PHP Code:
    $query "DELETE FROM individual_profile WHERE username = '$username'"
    Dave .... HostMonster for all of your hosting needs

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by djm0219 View Post
    PHP Code:
    $query "DELETE FROM individual_profile WHERE username = '$username'"
    That seems too simple. I thought it would have to be more distinctive and delete based on the name selected? I will give it a shot none the less

    Thank you!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    If you build your <form> and your DELETE button correctly, it really is that simple.

    Example:
    Code:
    $query = "SELECT * FROM individual_profile WHERE username = '$username'";
    $result = mysql_query($query);
    if ( $row = mysql_fetch_assoc($result) )
    {
        echo '<form method="post" action="processRequest.php">';
        echo '<input type="hidden" name="username" value="' . $username . "'/>";
        echo $row["firstname"] . " " . $row["lastname"] . "<br/>";
        echo $row["email"];
        echo '<input type="submit" name="edit" value="Edit" class="editbutton"/>';
        echo '<input type="submit" name="delete" value="Delete" class="deletebutton"/>';
        echo "</form>\n";
    }
    So now your HTML will look like
    Code:
    <form method="post" action="processRequest.php">
    <input type="hidden" name="username" value="john3371"/>
    John Smith
    johnsmith@realestaate.com.au
    <input type="submit" name="edit" value="Edit"/>
    <input type="submit" name="delete" value="Delete"/>
    </form>
    And so, in your "processRequest.php" page, you can do something like this:
    Code:
    <?php
    $username = $_POST["username"]
    if ( ! isset($username) )
    {
        echo "No username passed...aborting";
        exit( );
    }
    if ( isset($_POST["delete"]) && $_POST["delete"] == "Delete")
    {
        $sql = "DELETE FROM table WHERE username = '$username'";
        ... execute that query ...
    }
    else if ( isset($_POST["edit"]) && $_POST["edit"] == "Edit")
    {
        ... edit that user ...
    }
    ... etc. ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    But another way to do it, if you want to have your edit and delete code in different PHP pages, is to simply have two <form>s, one for each button.

    So you'd have something like this:
    Code:
    John Smith
    johnsmith@realestate.com.au
    <form method="post" action="edituser.php" style="display: inline;">
    <input type="hidden" name="username" value="<?php echo $username; ?>" />
    <input type="submit" value="Edit"/>
    </form>
    <form method="post" action="deleteuser.php" style="display: inline;">
    <input type="hidden" name="username" value="<?php echo $username; ?>" />
    <input type="submit" value="Delete"/>
    </form>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    If you build your <form> and your DELETE button correctly, it really is that simple.

    Example:
    Code:
    $query = "SELECT * FROM individual_profile WHERE username = '$username'";
    $result = mysql_query($query);
    if ( $row = mysql_fetch_assoc($result) )
    {
        echo '<form method="post" action="processRequest.php">';
        echo '<input type="hidden" name="username" value="' . $username . "'/>";
        echo $row["firstname"] . " " . $row["lastname"] . "<br/>";
        echo $row["email"];
        echo '<input type="submit" name="edit" value="Edit" class="editbutton"/>';
        echo '<input type="submit" name="delete" value="Delete" class="deletebutton"/>';
        echo "</form>\n";
    }
    So now your HTML will look like
    Code:
    <form method="post" action="processRequest.php">
    <input type="hidden" name="username" value="john3371"/>
    John Smith
    johnsmith@realestaate.com.au
    <input type="submit" name="edit" value="Edit"/>
    <input type="submit" name="delete" value="Delete"/>
    </form>
    And so, in your "processRequest.php" page, you can do something like this:
    Code:
    <?php
    $username = $_POST["username"]
    if ( ! isset($username) )
    {
        echo "No username passed...aborting";
        exit( );
    }
    if ( isset($_POST["delete"]) && $_POST["delete"] == "Delete")
    {
        $sql = "DELETE FROM table WHERE username = '$username'";
        ... execute that query ...
    }
    else if ( isset($_POST["edit"]) && $_POST["edit"] == "Edit")
    {
        ... edit that user ...
    }
    ... etc. ...

    Mmm... i've done my code as per your suggestion but when deleting the data, it deletes ALL profiles based on $username and not only the one selected...

    Am I doing something wrong?

    Code:
    if ($query_run = mysql_query($query))
    {
    	while ($query_row = mysql_fetch_assoc($query_run))
    	{
    		$fname = $query_row['fname'];
    		$lname = $query_row['lname'];
    		$email = $query_row['email'];
    		echo  '<form method="post" action="profileRequest.php">';
    		echo  '<input type="hidden" name="username"       value="'.$username.'"/>';
    		echo "<b>$fname "."$lname</b>" . '<br>' . "$email" . '<input type="button" value="Edit" name="Edit" class="edit">' . '<input type="button" value="Delete" class="delete" name="Delete">';
    		echo "</form>\n";
    		echo '<br><br>';
    		}
    }
    Last edited by rexhvn; 09-17-2013 at 01:27 PM.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    If you have more than one record with the same username value, then of course it will delete all of them.

    You need to use a field that is UNIQUE, then, instead of username.

    Surely you have a field that is your PRIMARY KEY? Use it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    If you have more than one record with the same username value, then of course it will delete all of them.

    You need to use a field that is UNIQUE, then, instead of username.

    Surely you have a field that is your PRIMARY KEY? Use it.
    Of course! Derrr...

    I've got a primary key called 'id' which is Auto_incremented based on all profiles created... (1,2,3,4 etc)

    How could I include that into my DELETE query?

  • #9
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,293
    Thanks
    4
    Thanked 202 Times in 199 Posts
    If you know which ID is to be deleted use:

    PHP Code:
    $query "DELETE FROM individual_profile WHERE `username` = '$username' AND `id` = 1"
    Changing the 1 to the correct value of course or using a variable like you do with username.
    Dave .... HostMonster for all of your hosting needs

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Actually, if id is the PRIMARY KEY, there is no reason to use username in the query, at all.

    So now the "trick" is to send the id to the server, from the HTML <form>, instead of the username.

    Trivial change to the code that creates the <form>s:
    Code:
    if ($query_run = mysql_query($query))
    {
    	while ($query_row = mysql_fetch_assoc($query_run))
    	{
    		$id = $query_row['id'];
    		$fname = $query_row['fname'];
    		$lname = $query_row['lname'];
    		$email = $query_row['email'];
    		echo  '<form method="post" action="profileRequest.php">';
    		echo  '<input type="hidden" name="userid"       value="'.$id.'"/>';
    		echo "<b>$fname "."$lname</b>" . '<br>' . "$email" . '<input type="button" value="Edit" name="Edit" class="edit">' . '<input type="button" value="Delete" class="delete" name="Delete">';
    		echo "</form>\n";
    		echo '<br><br>';
    	}
    and then "profileRequest.php" makes the corresponding change:
    Code:
    <?php
    $userid = $_POST["userid"]l
    if ( ! isset($userid) )
    {
        echo "No user id passed...aborting";
        exit( );
    }
    $userid = (int) $userid; // cast to integer for sql injection safety!
    if ( isset($_POST["delete"]) && $_POST["delete"] == "Delete")
    {
    
        $sql = "DELETE FROM table WHERE id = $userid";
        ... execute that query ...
    }
    else if ( isset($_POST["edit"]) && $_POST["edit"] == "Edit")
    {
        ... edit that user ...
    }
    Trivial changes.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    rexhvn (09-19-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •