PHP/MYSQL help

Discussion in 'OT Technology' started by BaZ, Jun 29, 2005.

  1. BaZ

    BaZ 2004 ACC Champions

    Joined:
    Jun 12, 2001
    Messages:
    2,005
    Likes Received:
    0
    Location:
    Hokieville, USA
    i am trying to teach myself php and mysql and am writing a simple project to keep track of the number of hours i work at my job, per day. it will add entries to my mysql database just fine and will clear the database fine too. however, when i try to delete an individual entry, it will not do it. i mean, it will tell me that it does, but it doesnt.

    my database is called bazhour, and it has a single table, hours. within the table, it has three fields: id (smallint), date (varchar, 32), and howlong (int, 11). i am including two files: deletetime.php, and deletetimehandler.php.

    Code:
    deletetime.php
    
    <html>
    
    <head>
    	<title>Basil's Hours</title>
    </head>
    
    <body>
    	Showing all records:<br>
    	<?php
    
    		$link = mysql_connect("localhost", "root", "");	
    		mysql_select_db("bazhour") or die(mysql_error());
    
    		if($link){
    			$query = "SELECT * FROM hours";
    			$result = mysql_query($query,$link) or die(mysql_error());
    			$num_rec = 0;
    			while($row = mysql_fetch_array($result)){
    				$num_rec++;
    				echo "Date: $row[date], ";
                                    echo "Hours: $row[howlong]<br>";
    			}
    			if ($num_rec == 0){
    				 echo "No records exist!<br>";
    			}
    		}
    
    	?>
    
    	<form method="post" action="deletetimehandler.php">
    		<br>Date:  <input type="text" name="date" size="32" maxlength="64" /> <br />
    		* By typing ALL, all records will be cleared * <br>
    		<input type="submit" name="submit" value="Delete Time" /> <br />
    	</form>
    </body>
    
    </html>
    
    Code:
    deletetimehandler.php
    
    	$date = mysql_escape_string($_POST['date']);
    	$link = mysql_connect("localhost", "root", "");
    	mysql_select_db("bazhour") or die(mysql_error());
    	
    	if ($link){
    		$query = "";
    		if ($date == "ALL"){
    			echo "Deleting all records...<br>";
    			$query = "DELETE FROM hours";
    		} else {
    			echo "Deleting record: $date...<br>";
    			$query = "DELETE FROM hours WHERE date = $date";
    		}
    	
    		echo "$query<br>";
    		$result = mysql_query($query,$link) or die(mysql_error());
    	}
    
    if anybody sees anything i'm doing wrong in either of these, let me know
     
  2. zabuni

    zabuni New Member

    Joined:
    Jan 2, 2005
    Messages:
    64
    Likes Received:
    0
    You may wish to make sure that your date is formatted in mysql's style. YYYYMMDD or some such. Take a look here:

    Date and Time MYSQL Reference

    You may also need to put the Date in quotes:
    $query = "DELETE FROM hours WHERE date = '$date'";

    What's the output look like?
     
  3. BaZ

    BaZ 2004 ACC Champions

    Joined:
    Jun 12, 2001
    Messages:
    2,005
    Likes Received:
    0
    Location:
    Hokieville, USA
    the single quotes did it, thanks! :bigthumb:
     
  4. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    Just so you know, it's a good idea in general to use the PEAR DB library whenever you're doing database access. You only need to learn one set of syntax to use any DB you like.
     

Share This Page