Remove Address from Database

Hi (again), apologies, this is my third post today, learning well but need a little help…

I have written a script to delete an email address from the database when a user writes in a feild their address. It works perfectly and does remove their email address but if I type in an address that does not exist in the database, the message is still echoed that it has been removed, want it to say ‘Email Address not found’.

Sure this is something small and simple to do :slight_smile:

<?php
	if ($_POST['remove']!=""){
	include_once('connection.php');
	$remove = $_POST['remove'];
	$sql_search = mysql_query("SELECT * FROM addresses WHERE email='$remove'");
	if ($sql_search) {
	$sql_remove = mysql_query("DELETE FROM addresses WHERE email='$remove'");
	echo $msg_to_user = '<div class="msg_to_user_success">Email address removed successfully.</div>';
	}
	else {
	echo $msg_to_user = '<div class="msg_to_user">Cannot find address in Database.</div>';
	}
	}
	else { $msg_to_user = ""; }
?>

Because if your select doesn’t return any rows, the ‘if’ is still true. It’s only false if the query ends with an error. Check for mysql_num_rows($sql_search) > 0 instead.

Hi, thanks for your reply. Right ok, where do I place it? I can’t work out where it needs to go?

Instead of

if ($sql_search) {

use

if (mysql_num_rows($sql_search) > 0) {

Hey there, just tried that but doesn’t seem to be working, still says ‘Email address removed successfully.’. Any ideas?


<?php
	if ($_POST['remove']!=""){
	include_once('connection.php');
	$remove = $_POST['remove'];
	$sql_search = mysql_query("SELECT * FROM addresses WHERE email='$remove'");
	if (mysql_num_rows($sql_search) > 0) {
	$sql_remove = mysql_query("DELETE FROM addresses WHERE email='$remove'");
	echo $msg_to_user = '<div class="msg_to_user_success">Email address removed successfully.</div>';
	}
	else {
	echo $msg_to_user = '<div class="msg_to_user">Cannot find address in Database.</div>';
	}
	}
	else { $msg_to_user = ""; }
?>


<?php
$email = filter_input(INPUT_POST, 'email');

mysql_query(sprintf(
  "DELETE FROM table WHERE email = '%s' LIMIT 1;",
  mysql_real_escape_string($email)
));

if(1 === mysql_affected_rows()){
  echo 'Email removed.';
}else{
  echo 'Email not found.';
}
?>

Hi, that’s perfect, all working swimmingly now. Appreciate your guys help and advice, thank you.