SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Deletion

  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Canada
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Deletion

    I'm trying to run a query in php, but i just get the error msg. I'm wondering what I might be doing wrong?

    I am getting a specific dvd_id in a form, so I can delete that table row in the database.

    Code:
    $rowID = $_REQUEST["dvd_id"];
    
    
    // Delete data in mysql from row that has this id
    $sql="DELETE FROM dvds WHERE dvd_id='$rowID'";
    $result=mysql_query($sql);
      
      
      // if successfully deleted
    if($result){
    echo "Deleted Successfully";
    
    }
    
    else {
    echo "ERROR";
    }

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try to see the physical query by echoing it after interpreting the variable $rowID.
    PHP Code:
    echo $sql
    Whether it does have the correct dvd_id or not.

    Also try to see mysql's system error with:
    PHP Code:
    mysql_query($sql) or die(mysql_error()); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Further to rajug's answer, avoid using $_REQUEST as it is too random. Be specific where the incoming variable is from $_POST, $_GET etc but not $_REQUEST.

    Also consider not deleting from your database but setup another field that has a simple boolean 0 or 1. 0 means its not displayed, 1 means it is. This saves a whole lot of problems with things being accidentaly deleted!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Canada
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm wondering.. is the PEAR system different?

    I am using the PEAR and mdb2 system....


    Code:
    require_once 'MDB2.php';
    
    $dsn = 'mysqli://a4500_lab14:******@localhost/a4500_DVDRentals';
    
    $mdb2 =& MDB2::connect($dsn);
    
    if (PEAR::isError($mdb2)) {
      die($mdb2->getMessage());
    }
    
    //check to see if user wants to add
    if (isset($_REQUEST["Add"])) 
    
    {
     
    //insert the record
    if ( isset( $_REQUEST["customer_id"] ) ) {
      $sth = $mdb2->prepare( 'INSERT INTO customers (customer_id, fname, lname, city, phone, address, postal) VALUES (?, ?, ?, ?, ?, ?, ?)',
    			 array('integer', 'text', 'text', 'text', 'integer', 'text', 'text' ),
    			 MDB2_PREPARE_MANIP );
      $sth->execute( array( $_REQUEST["customer_id"],
    			$_REQUEST["fname"],
    			$_REQUEST["lname"],
    			$_REQUEST["city"],
    			$_REQUEST["phone"],
    			$_REQUEST["address"],
    			$_REQUEST["postal"] ) );
     }
     
     }
     
     
    //check to see if user wants to remove
    if (isset($_REQUEST["Remove"])) 
    
    {
     //delete the record
     if ( isset( $_REQUEST["customer_id"] ) ) {
      $sth = $mdb2->prepare( 'DELETE FROM customers WHERE customer_id = $_REQUEST["customer_id"]');
                
    }
    }
    $res =& $mdb2->query('SELECT * FROM customers');
    
    ?>
    my delete code won't work properly.

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You don't execute the delete query

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes please do not use $_Request[] use appropriate one

  7. #7
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Canada
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so how would you run the DELETE query then?

    It seems a bit different on PEAR?

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    How about:
    Code PHP:
    if (isset($_REQUEST['customer_id'])) {
      $sth = $mdb2->prepare( 'DELETE FROM customers WHERE customer_id = $_REQUEST["customer_id"]');
      $sth->execute();            
    }
    I'm not sure, I never used PEAR. But the SELECT you do before the DELETE has a prepare and an execute statement, I imagine the same must be done for the DELETE


Bookmarks

Posting Permissions

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