Deleting Issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oyedele
Forum Newbie
Posts: 17
Joined: Mon Jun 27, 2011 6:59 am

Deleting Issues

Post by oyedele »

I could successfully delete a record in the database but if i type anything not in d database it will still perform the content inside the echo paragraph. Below is the code i used.

Code: Select all

<?php                            
                     if(isset($_POST['Delete']))
					{
				 	$sql="Delete from student_details where Username='".$_POST['Username']."'";
					
					$result = mysql_query($sql);
					if($result){
					echo '<p class=delstudent>';
                  	echo "You have Deleted Successfully";
					echo '</p>';
					header("refresh:2;URL=admindel.php");
					}
					}
					else 
					{
						echo mysql_error();
						}
					?>
Please kindly sort out any correction.
Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Deleting Issues

Post by Christopher »

First, for security always escape values from the request with mysql_real_escape_string(), so:

Code: Select all

$sql="Delete from student_details where Username='".mysql_real_escape_string($_POST['Username'])."'";
Second, if you want to see if your DELETE actually deleted rows, check if mysql_affected_rows() > 0.
(#10850)
Post Reply