Page 1 of 1

Deleting Certain Records.

Posted: Thu Mar 31, 2005 12:23 pm
by erick
I need the code delete all the records from the table that contain a certain date. I wrote the following code but it doesn't work. If anybody would be so kind to assist me into fixing it I would be forever thankfull.

Code: Select all

<?php
    $submit = $_POST["submit"]; 
     
    if ($submit) { 
        $month = $_POST["month"]; 

    $connection = mysql_connect("localhost", "***", "****") or die("connect"); 

    mysql_select_db("*****",$connection) or die ("Unable to select requested database.");
    $deleteresult = mysql_query;
    ("DELETE FROM `auth` WHERE month = '$month'");
    $result=mysql_query($deleteresult, $connection);
    $affected_rows=mysql_affected_rows( $connection);
    echo "<b>Check</b> was executed.";
	}
?>
Thanks in advance.

Posted: Thu Mar 31, 2005 12:34 pm
by anjanesh
Your $month in the php code must be in the same format as month in your database.

Re: Deleting Certain Records.

Posted: Thu Mar 31, 2005 1:00 pm
by John Cartwright

Code: Select all

<?php
    
    //do not reply on the button being pressed
    $month= (!empty($_POST['month']) ? $_POST['month'])  : '');
    
    //only execute when month has a value
    //should also think about validating the string so it is proper format 
    if (!empty($month))
    { 
         //should always use mysql_error() for accurate reports         
         $connection = mysql_connect("localhost", "***", "****") or die(mysql_error()); 
         
         //should always use mysql_error() for accurate reports
         mysql_select_db("*****",$connection) or die (mysql_error());
         
         //you were trying to execute the query twice, or something  
         //should have error reporting on ALL queries
         //good habbit of escaping variables -- easier to read
         $deleteresult = mysql_query("DELETE FROM `auth` WHERE month = '".$month."'") or die(mysql_error());
         
         //not really sure why you need $affected_rows.. it has no apparent purpose here
         $affected_rows = mysql_affected_rows($deleteresult,$connection);
         echo "<b>Check</b> was executed.";
    }
?>
You had some weird stuff going on in your snipplet, this is the cleaned up version.

Posted: Thu Mar 31, 2005 1:11 pm
by erick
Uh Thanks a billion