Solved

PHP to delete values in MySQL if conditions are met

Posted on 2002-04-27
11
364 Views
Last Modified: 2011-10-03
Hello all,

 I need a simple script to delete values in MySQL if certain conditions are met. Data would post via a HTTP form.
Condition 1: The form must have the email value specified.
Condition 2: The email must be present in the database if not it should notify the user
Condition 3: If the email does exist, delete the value and display a confirmation.


This is the script so far it doesn’t work but I think I am on the right track. Please advise. I am very new to PHP/MySQL. Thanks.


<?php
require("config.inc");

mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");

$remove = mysql_query("DELETE FROM emails WHERE email ='$email'") or die ("Your address could not be removed. Please try again later.");

$validemail = mysql_query("SELECT * from emails ");

while ($row = mysql_fetch_row($validemail))
       

if ($email =="")
{
echo ("You must enter a valid email address.");
}
elseif ($email != "$validemail")
{
echo ("The email was not found in the database");
}
else ($email == "$validemail");
{
$remove;
echo ("Email has been removed");
}
mysql_close();

?>














 
0
Comment
Question by:ipc
  • 5
  • 3
11 Comments
 
LVL 4

Accepted Solution

by:
lokeshv earned 100 total points
ID: 6974336
hmm try this...


if($email==""){
echo 'Please enter the email';
die;
}

mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");



$select_query="select * from ur_table where email='$email'";

$qid=mysql_query($select_query);

if(!$qid){
echo 'Error :'.mysql_error();
die;
}

if(mysql_num_rows($qid)==1){
     $delete_query="delete from ur_table where email='$email';
$qid=mysql_query($delte_query);
if($qid){
    echo 'Record Delted';
}
else{
     echo 'Error:'.mysql_error();
       die;
}

}
else{

        echo 'Email Address doesnt exists";

        die;      
}


Hope this helps..
0
 

Author Comment

by:ipc
ID: 6975803
Thanks for the input. The first condition works as it did in my script as well. The second condition reults in a blank sreen as does the third condition. No error messages are displayed. Any other ideas would be appreciated.


Thanks
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6976236
typo error

$qid=mysql_query($delete_query);

and if u still get ..please post ur code here ..

LK
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Expert Comment

by:harwantgrewal
ID: 6976505
It is definately going to give you a error cause you are executing the query before checking anything it is better you just declate a variable and set that variable to the query and validate and then delete


Harry
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6976516
checking anything ?

what i m supposed to check there ?

Lk
0
 
LVL 5

Expert Comment

by:harwantgrewal
ID: 6976547
Here is the right Code

<?php
require("config.inc");

mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");

$remove = "DELETE FROM emails WHERE email ='$email'";
if ($email =="")
{
echo ("You must enter a valid email address.");
}
else
{
$validemail = mysql_query("SELECT * from emails where email='$email'");
if (mysql_num_rows($validemail))
{
echo ("The email was not found in the database");
}
else
{
mysql_query($remove) or die("Try Again");
echo ("Email has been removed");
}
}
mysql_close();

?>















0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6976558
ok..

what is the positive points of ur code ? and how its better ?

Just courious ? :o)


Lk
0
 
LVL 5

Expert Comment

by:harwantgrewal
ID: 6976564
Condations are more and suppose person leave blank field than what. Alway try to validate things in each step and never pop the mysql error as it is ver ugly to display this error to user. Most important thing use Die instead of checking the true false condation for executing the query somtime it result a different error.

Harry
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6976581
check my code on top .deww line are there to check blank email field

if($email==""){
echo 'Please enter the email';
die;
}

and

yeah for user but for devloper its very helpful, suppose u have a parse error in ur validemail query then ??

if u r not using mysql_error u never know wats happens ?

and u can trigger off ur mysql_error statements just set a variable in .inc file....just set it off after debugging..


Lk

0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question