Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with newbie script writing to database

Status
Not open for further replies.

sophielois

Technical User
Sep 8, 2005
66
GB
Hi ,

Just starting to learn the simple things to do with PHP. Got a little problem.

I have a form which has a checked tick box so users can subscribe to newsletter
**This is just a extract from the form
Code:
<tr>
<td height="20" class="contactform"> 
<input name="mail" type="checkbox" style="border:0px;" value="mail" checked> Receive FREE Newsletter</td>
</tr>

on submit it goes to a script called post1.php
Having problems with this section:
Code:
<?
if(mail == "checked") {

   $res = mysql_query ("SELECT COUNT(*) FROM mailinglist
                        WHERE email='email' ");

   if (mysql_result($res,0)==0) {

       mysql_query("INSERT INTO mailinglist (fullname, email, signup_date)
                VALUES('$name', '$email', now())") or die (mysql_error());

   }
}

?>

Im basically trying to do the following:

If the check box is ticked and the email address is not already subscribed to the mailing list, insert email into the mailinglist table and continue with script. If the checkbox is ticked but the email address is already subscribed continue with the script

Everything works in the full script except i cant get the script to insert the email address into the mailing list

What am i doing wrong??

Thanks for any help that can be offered

Soph
 
hi ....

this is now what i have, unfortnately its still not putting the email address from the form into the datbase

Code:
<?

$name= $_POST['name']; 
$email= $_POST['email'];
$company= $_POST['company'];
$telephone= $_POST['tel'];
$location= $_POST['location'];
$comments= $_POST['comments'];
$mail= $_POST['mail'];


if($mail == "checked") {

   $res = mysql_query ("SELECT COUNT(*) FROM mailinglist
                        WHERE email='email' ");

   if (mysql_result($res,0)==0) {

       mysql_query("INSERT INTO mailinglist (fullname, email, signup_date)
                VALUES('$name', '$email', now())") or die (mysql_error());

   }
}

?>

not getting any errors

thanks
Soph
 
In the lines:

Code:
$mail= $_POST['mail'];

if($mail == "checked") {

I assume that "mail" is the name of a checkbox on your form. If so, I recommend against the two lines you are using.

First, if the checkbox is not set, the $_POST['mail'] will not exist, so the first of the lines may deliver an error or warning.

Second, the value that is returned when a checkbox is checked is browser-dependent. Opera, for example, sends "on", not "checked". A much better way (again, assuming $_POST['mail'] is coming from a form checkbox) is:

Code:
if(isset ($_POST['mail']) {




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
You posted you section of your form:
Code:
<tr>
<td height="20" class="contactform">
<input name="mail" type="checkbox" style="border:0px;" value="mail" checked> Receive FREE Newsletter</td>
</tr>
and the section of code that's causing the problem:
Code:
$mail= $_POST['mail'];
if($mail == "checked") {
   $res = mysql_query ("SELECT COUNT(*) FROM mailinglist
                        WHERE email='email' ");
   if (mysql_result($res,0)==0) {
       mysql_query("INSERT INTO mailinglist (fullname, email, signup_date)
                VALUES('$name', '$email', now())") or die (mysql_error());
   }
}
?>
Notice in your form, when the checkbox is checked the value of the variable passed as $_POST['mail'] is "mail", not "checked". You're doing the wrong comparison, that's why it's not working.
You should have:
Code:
$mail= (isset($_POST['mail']))?$_POST['mail']:'';
if($mail == 'mail') {

Which should fix at least that problem.

Ken
 
Thanks for helping guys

used this

Code:
$mail= (isset($_POST['mail']))?$_POST['mail']:'';
if($mail == 'mail') {

and works perfectly, no other problems cropping up

Thanks again

Soph
 
ok so i was wrong, its not working perfectly......

sorry about that.

The problem is if i choose to subscribe to the email list by checking the box and my email address is already in the database i get this message

Code:
Duplicate entry 'soph@me.co.uk' for key 1

What do i need to change so if i choose to subscribe but i am already a subscriber the script doesn't attempt to put my email address in the database and carrys on with the rest of the script.

Thanks for any more help,

really appreciate it

Soph
 
Im still stick on this one, can anyone offer any more advice?

Thanks again

Sophie
 
first of all whats wrong here:
Code:
... WHERE email='[red]email[/red]' "); ...

As previously mentio0ned you are trying to compare a variable, so it need $ before the name. and to compare strings in mysql you use LIKE not =.

Such as Where email LIKE '$email'

This will compare strings exactly

Second:

this comparison will never work adequately
Code:
... if (mysql_result($res,0)==0) { ...
[code]


I personally would check whether or not I got rows, using the following
[code]
$rows=mysql_num_rows($res);

if(!$rows){.... insertion script ....}

[code]

I suggest you read up in the PHP manual to learn what the different functions actually return as values. 

If there are no rows which is what you want mysql_result will return an error never a 0.
[quote=PHP.net Manual]
Description
string mysql_result ( resource result, int row [, mixed field] )
Retrieves the contents of one cell from a MySQL result set. 
[/quote]

Hope this helps

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top