Solved

PHP to delete values in MySQL if conditions are met

Posted on 2002-04-27
11
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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.

710 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