Deleting Certain Records.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
erick
Forum Newbie
Posts: 3
Joined: Thu Mar 31, 2005 12:14 pm

Deleting Certain Records.

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Your $month in the php code must be in the same format as month in your database.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Deleting Certain Records.

Post 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.
erick
Forum Newbie
Posts: 3
Joined: Thu Mar 31, 2005 12:14 pm

Post by erick »

Uh Thanks a billion
Post Reply