Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2012, 02:03 PM   PM User | #1
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
Unhappy Cannot delete a field in MySQL using PHP!

I have made a simple form where users who have been subscribed and unsubscribe by inserting their email address.

In my database using PHPMyAdmin, my database to store the emails is 'Links', the table is 'email' and the fields are the 'id' and 'emailaddress'.

What I have tried is making a text input field, where the user ill insert his or her email address, to unsubscribe on the website. As a result the user's field for his or her email address will be delete in the database which is saving the emails for all users who have subscribed.

My HTML codes are:

Code:
<p>Subscribe for newsletters:</p>
<img src="images/k-newsletter-icon.png" width="96" height="96" alt="subscri"/>
<form action="index.php" method="post">
<input type="text" size="25" placeholder="Your email address..." name="enter"/>
<input class="submit" type="submit" value="Subscribe" name="subscribe"/>
</form>
My PHP codes are:

PHP Code:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$email $_POST['enter'];
@
mysql_connect ('localhost''root''') or die ('Error');
@
mysql_select_db ('links') or die ('Error');
if (!
filter_var($emailFILTER_VALIDATE_EMAIL)) {
echo 
"Not an email";
return 
false;    
} else {
mysql_query("DELETE FROM email WHERE emailaddress ='$email'");
echo 
"deleted";
}
}
?>
When I test it,it is not working, as I see the email which was saved, is still in the database! Help!
angelali is offline   Reply With Quote
Old 02-21-2012, 03:14 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Does the script print anything to the screen?
Also, enable your error reporting:
PHP Code:
ini_set('display_errors'1);
error_reporting(E_ALL); 
Fou-Lu is offline   Reply With Quote
Old 02-21-2012, 03:17 PM   PM User | #3
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
Try changing
PHP Code:
mysql_query("DELETE FROM email WHERE emailaddress ='$email'"
to
PHP Code:
mysql_query("DELETE FROM email WHERE emailaddress ='$email'") or die(mysql_error()); 
and see if that says what's wrong
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 02-21-2012, 03:29 PM   PM User | #4
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
Yes, error_reporting is ON by default, I just checked it!

@NightFire, I changed the code to what you told me, again here, it seems the email is not being deleted in PHPMyAdmin when I refresh!
angelali is offline   Reply With Quote
Old 02-21-2012, 03:31 PM   PM User | #5
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
Here are codes including what you suggested me, but the problem is the same! Is this because my emailaddress field in the table is UNIQUE KEY?

PHP Code:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$email $_POST['enter'];
@
mysql_connect ('localhost''root''') or die ('Error');
@
mysql_select_db ('links') or die ('Error');
if (!
filter_var($emailFILTER_VALIDATE_EMAIL)) {
echo 
"Not an email";
return 
false;    
} else {
mysql_query("DELETE FROM email WHERE emailaddress ='$email'") or die(mysql_error()); 
echo 
"deleted";
}
}
?>
angelali is offline   Reply With Quote
Old 02-21-2012, 04:04 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Error reporting on doesn't necessarily mean that its on for all errors.
It would be preferable that the key is unique. Otherwise you can potentially delete multiple records; both are valid for a design depending on purpose. It is possible that you are violating a key constraint not set to cascade which will cause it to reject the deletion.

What of output, is it blank?
Fou-Lu is offline   Reply With Quote
Old 02-21-2012, 04:27 PM   PM User | #7
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
It just tells me that the email has been deleted, but when I refresh the database in PHPMyAdmin, it is still there.. I mean it has not been deleted at all..
angelali is offline   Reply With Quote
Old 02-21-2012, 05:40 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Okay, so or die(mysql_error()); produces no failure results. The query is therefore successful indicating a deficiency with the data involved.
Now you check how many were deleted:
PHP Code:
mysql_query("DELETE FROM email WHERE emailaddress ='$email'") or die(mysql_error()); 
printf("Deleted %d records." PHP_EOLmysql_affected_rows()); 
How many does it say were deleted? If its 0, there is no matching emailaddress to the given $email. Capture the query into a variable and print that out instead.
Fou-Lu is offline   Reply With Quote
Old 02-21-2012, 05:44 PM   PM User | #9
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
I have already done it. It is working now. It was a problem of a dot in the database. When performing the INSERT code, I included a dot in it thus thats is why the email was not deleting as it did not match. Thank you to all of you who have helped me!
angelali is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:55 PM.


Advertisement
Log in to turn off these ads.