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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Question Unable to delete row with PHP

    I'm trying to delete a row of data but I can't get mySQL to delete the row with PHP. I'm not getting any error so I have been echoing commands out to see what could be going wrong. I have been racking my brain on this for too long and I can't see what is wrong with it.

    PHP Code:
    <?php
    session_start
    ();
    // Set the includes
    include("config/config.php");

    $dbConf = new RugbyConfig();
    $databaseURL $dbConf->get_databaseURL();
    $databaseUName $dbConf->get_databaseUName();
    $databasePWord $dbConf->get_databasePWord();
    $databaseName $dbConf->get_databaseName();

    // Get Database Connection
    $connect =mysql_connect($databaseURL,$databaseUName,$databasePWord);

    // Connect to database
    $queryreg mysql_select_db ($databaseName);

    //Get session data to fill the form
    $nest_id $_GET['id'];
    $results mysql_query("select * from pronest_master_part_list where id = $nest_id"); 
    $row mysql_fetch_assoc($results); 

    // The disired part to remove
    $part_number $_POST['part_number'];

    // Stores the delete command to remove a row from the database
    $part_delete =strtolower(trim($_POST['delete']));

    $submit $_POST['submit'];
    $uid $_POST['id'];

    // Test to determine if the submit button was pressed
    if($submit)
    {
        
    // A quasi fail safe so the user knowningly has to type "delete" into the text box to remove the data row.
        // This functionality will be removed when the CMS comes online and an ACL will be in place.
        
        
    if($part_delete  == "delete")
        {
            
    // removed from database
            // $result = mysql_query("DELETE FROM pronest_master_part_list WHERE id=$nest_id") or die('Could not delete: ' . mysql_error());
            // if($result)
            // {
                // echo Die("<p>delete succesful. <h3>NOTE:</h3> The part number was removed from the databasae. <p>Try another <a href='search_nest.php'>search</a>");
            // }
            
    $tmp "DELETE FROM pronest_master_part_list WHERE id=\"$nest_id\"";
            echo die(
    "<p>SQL command: ".$tmp." Your uid: ".$uid);
        }

    }

    ?>

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    where's the line where you actually run the delete query?

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Sorry it was commented out and I forgot to bring it back.

    PHP Code:

    if($part_delete  == "delete"
        { 
            
    // removed from database 
             
    $result mysql_query("DELETE FROM pronest_master_part_list WHERE id=$nest_id") or die('Could not delete: ' mysql_error()); 
            
    // if($result) 
            // { 
                // echo Die("<p>delete succesful. <h3>NOTE:</h3> The part number was removed from the databasae. <p>Try another <a href='search_nest.php'>search</a>"); 
            // } 
            
    $tmp "DELETE FROM pronest_master_part_list WHERE id=\"$nest_id\""
            echo die(
    "<p>SQL command: ".$tmp." Your uid: ".$uid); 
        } 
    I'm echoing the mysql command and it looks like the $nest_id clears itself but I don't know why.

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    In

    PHP Code:
    $result mysql_query("DELETE FROM pronest_master_part_list WHERE id=$nest_id"
    $nest_id is not wrapped in quotes where in $tmp it is, so $tmp is not echoing out the actual query being run.

    if $nest_id is string data type of some kind (varchar etc) in pronest_master_part_list then $nest_id needs to be wrapped in quotes in the query.

    To help debug, I usually store the query in a separate variable and echo it out prior to running to make sure it is ok and then use that variable in mysql_query()

    PHP Code:
    $query 'select blah blah';
     
    echo 
    $query; die();  //for debugging only
     
    $rs mysql_query($query$conn); //run the query 
    Last edited by bullant; 07-29-2011 at 03:29 AM.

  • #5
    New Coder
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    And if $nest_id is an int then I don't use quotes?

    when I run
    PHP Code:

    if($nesd_id)
        {
            echo (
    "<p>Nest id: $nest_id");
            
            if(
    $part_delete  == "delete")
            {
                
    // removed from database
                 // $query = 'DELETE FROM pronest_master_part_list WHERE id=$nest_id';
                 // $result = mysql_query($query) or die('Could not delete: ' . mysql_error());
                 // if($result)
                 // {
                     // echo Die("<p>delete succesful. <h3>NOTE:</h3> The part number was removed from the databasae. <p>Try another <a href='search_nest.php'>search</a>");
                 // }
                
    $tmp "DELETE FROM pronest_master_part_list WHERE id=$nest_id";
                echo die(
    "<p>SQL command: $tmp" );
            }
        }
        else echo die(
    "<p>Nest ID doesn't have a value."); 
    I always get the else result... why is $nest_id clearing after I submit the form?
    Last edited by Tith; 07-29-2011 at 04:21 AM. Reason: clarity

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by Tith View Post
    And if $nest_id is an int then I don't use quotes?
    You shouldn't, technically, but MySQL is sloppy and allows them anyway.

    It seems to be a truism that most PHP programmers put them in all the time whereas those who grew up with other databases and/or other languages know when they should be there and when they shouldn't. But so long are you are using MySQL, you can do it either way.

    As evidence that they aren't needed, consider your own code:
    Code:
    $results = mysql_query("select * from pronest_master_part_list where id = $nest_id");
    That worked correctly without wrapping '...' around $nest_id, didn't it? Ergo...

  • #7
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by Tith View Post
    And if $nest_id is an int then I don't use quotes?
    I grew up with the Oracle database but MySQL allows integers to be wrapped in quotes.

    Personally, I would suggest wrapping integers in quotes when using MySQL since it provides a last line of defence against sql injection but should not be relied upon solely.

  • #8
    New Coder
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    this:
    PHP Code:
    $tmp "DELETE FROM pronest_master_part_list WHERE id=$nest_id"
    gives me this:

    DELETE FROM pronest_master_part_list WHERE id=

    I don't understand what happens to $nest_id

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I don't understand how this works (from your first post):
    Code:
    $nest_id = $_GET['id'];
    ...
    $uid = $_POST['id'];
    How are you passing *BOTH* a querystring and post date? And both with the same name?

    I mean, I know it can be done:
    Code:
    <form method="post" action="xyz.php?id=1117">
    <input name="id" >
    ...
    But are you *REALLY* doing that??? Makes me more than a little suspicious.

  • #10
    New Coder
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Old Pedant View Post
    I don't understand how this works (from your first
    How are you passing *BOTH* a querystring and post date? And both with the same name?
    I was playing around, my thought was when the page loads I have:
    PHP Code:
    value='<?php echo $row["id"];?>' disabled >
    So if that textbox has the correct $nest_id value I would get the value on submit. (because I wasn't getting a value for $nest_id) but it actually did nothing and I should've removed it.

    But are you *REALLY* doing that??? Makes me more than a little suspicious.
    No, that isn't my intention, if I am I'm doing it by mistake.

    I'm confused to what happens to my variable $nest_id when I submit the form. I don't understand why it seems to get set to nothing.

  • #11
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by Tith View Post
    I'm confused to what happens to my variable $nest_id when I submit the form. I don't understand why it seems to get set to nothing.
    Looks like you just need to do some Basic Debugging 101.

    Post the form's html.

  • #12
    New Coder
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    here is just the html

    PHP Code:
    <html>
    <Title></Title>
        <Body> 
            
            <form action ='delete_nest.php' method='post'>
                <table>
                    <tr>
                        <td>Part ID:</td>
                        <td><input type='text' name='id' value='<?php echo $row["id"];?>' disabled ></td>
                        <td>Part Number:</td>
                        <td><input type='text' name='part_number' value='<?php echo $row["part_number"];?>' ></td>
                    </tr>
                    
                    <tr>
                        <td>Type delete:</td>
                        <td><input type='text' name='delete' ></td>
                    </tr>
                </table>
               
               <input type='submit' name='submit' value='Delete' >
            
            </form>
                    
        </Body>

    </html>

  • #13
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    $nest_id = $_GET['id'];
    Since your form has got method="post", the above should be
    PHP Code:
    $nest_id $_POST['id']; // you need to validate this value to prevent SQl injections 
    $tmp = "DELETE FROM pronest_master_part_list WHERE id=$nest_id";
    echo die("<p>SQL command: $tmp" );
    And, where's your call to mysql_query() for that sql?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #14
    New Coder
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    The sql_query is in the if($delete){} statement.

    How would I validate $nest_id = $_GET['id']; or is there a better way to do this?
    I get the id from a search query from a previous page and I want to either delete the row or update the row. Everything works but editing and deleting the existing row.

  • Users who have thanked Tith for this post:

    AndrewCollins (08-10-2011)

  • #15
    New Coder
    Join Date
    Jul 2011
    Location
    Berkeley, CA
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 1 Post
    How exactly does this function?

    Code:
    $nest_id = $_GET['id']; ... $uid = $_POST['id'];


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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