Solved

PHP to delete values in MySQL if conditions are met

Posted on 2002-04-27
11
361 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
 
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

758 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

22 Experts available now in Live!

Get 1:1 Help Now