Solved

PHP to delete values in MySQL if conditions are met

Posted on 2002-04-27
11
363 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

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.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now