Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP to delete values in MySQL if conditions are met

Posted on 2002-04-27
11
Medium Priority
?
368 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 400 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
Industry Leaders: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

721 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