My Delete Query does not work.

Discussion in 'PHP' started by wwwbryan, May 2, 2009.

  1. #1
    if (isset($_GET['delete'])) {
    if ($_SESSION[mid] == $id) {
    $_GET['delete'] = $deleteid;
    
    mysql_query("DELETE FROM portfoliocomments WHERE pcomment='{$_GET['delete']}'");
    
    $_SESSION[justdelportfoliocomment] = true;
    header( 'Location: ./index.php?member=' . $id . '&page=last');
    
    }
    else
    {
    header( 'Location: ./index.php?member=' . $id . '&page=last');
    }
    }
    Code (markup):
    Which when the browser is set at index.php?member=1&delete=11
    it will delete row 11.

    But it does not delete anything, and it does not give an error.
     
    wwwbryan, May 2, 2009 IP
  2. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    You need to manage the errors yourself, try this:

    
    if (isset($_GET['delete'])) {
    	if ($_SESSION['mid'] == $id) {
    		$_GET['delete'] = $deleteid;
    
    		$boolQry = mysql_query("DELETE FROM portfoliocomments WHERE pcomment='{$_GET['delete']}'");
    
    		if($boolQry) {
    			$_SESSION[justdelportfoliocomment] = true;
    			header( 'Location: ./index.php?member=' . $id . '&page=last');
    		} else {
    			die('Invalid query: ' . mysql_error());
     		}
    	} else {
    		header( 'Location: ./index.php?member=' . $id . '&page=last');
    	}
    }
    PHP:
    It will check to make sure you do not have a MySQL error before re-directing to the next page. Also, is this the full code - You refer to $_GET['delete'] = $deleteid; but there is no $deleteid being set in this code.

    It isn't good practice to use $_GET[] variables in MySQL queries, users can 'inject' SQL commands into your database as they please, have a look at using mysql_real_escape_string() and checking the data type before inserting it into the database.

    Regards,

    Steve
     
    Steve136, May 2, 2009 IP
  3. wwwbryan

    wwwbryan Well-Known Member

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Oh ok it was a stupid mistake. The
    $deleteid = $_GET['delete'];

    Was backwards. :D

    Thanks a lot pal!
     
    wwwbryan, May 2, 2009 IP