script for mail and sql insert not working (no errors)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
farkewie
Forum Newbie
Posts: 22
Joined: Sat Jun 02, 2007 11:25 pm

script for mail and sql insert not working (no errors)

Post by farkewie »

hi my script wont work im not getting any errors any more bt none of the things its supposed to do are working.

1:) it wont go to the submit page when complete
2:) the image verification doesnt work
3:) emails arnt sent
4:) data not inserted in the database

i have been looking everywhere and i can work it out? i downloaded zend and checked the code and even that had no errors??

here is my code, i have explained the image verification in the comments, ( i know it works because im using it on another script)

Code: Select all

<html> 
<head> 
</head> 
<body> 

<?php 
/* grabs the POST variables and puts them into variables that we can use */ 
$time = date("d/m/Y ", time());
$date = date("d/m/Y H:i:s", time());
$tname=$_POST['tname']; 
$temail=$_POST['temail']; 
$yname=$_POST['yname']; 
$yemail=$_POST['yemail']; 
$cat=$_POST['cat'];
$msg=$_POST['mag']; 
$site_name=$_POST["mysite.com"];
$site_email=$_POST["mail@mail.com"]; 
$site_url=$_POST["www.mysite.com"]; 
$verif_box = $_REQUEST["verif_box"];

// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
	// if verification code was correct send the message and show this page 

//---------VALIDATION--------> 
    if($tname){//----> CHECK input 
        } 
        else{ 
            $error.="Please, go back and fill out your there name<br>\n";//----> ERROR if no input 
            } 

    if($temail){//----> CHECK input 
        } 
        else{ 
            $error.="Please, go back and fill out your there email<br>\n";//----> ERROR if no input 
            } 

    if($yname){//----> CHECK input 
        } 
        else{ 
            $error.="Please, go back and fill out your name<br>\n";//----> ERROR if no input 
            } 

    if($yemail){//----> CHECK input 
        } 
        else{ 
            $error.="Please, go back and fill out your email<br>\n";//----> ERROR if no input 
            } 

    if($msg){//----> CHECK input 
        } 
        else{ 
            $error.="Please, go back and fill out your message<br>\n";//----> ERROR if no input 
            } 


//-------->ERROR FREE?? 
    if($error==""){ 
        echo 'submit.php'; 
//---------------------------------- 
$mailContent="" 
            ."Hello xxxxsomeone has posted on the site.\n\n"            
			 ." ".$msg."\n\n\n\n" 

            ." ".$site_name."\n" 
			."--------------------------\n" 
            ." ".$site_url."\n";
//---------------------------------- 
$toAddress="mysite@mail.com";/* this is line 70 */ 
$subject="my subject"; /* change this! */ 
$recipientSubject="a new post"; /* change this! */ 
$receiptMessage = "" 
."Hello ".$tname." you.nn"             ." ".$msg."nnnn" 

            ." ".$site_name."n" 
			."--------------------------n" 
            ." ".$site_url."n";
//---------------------------------- 
mail($email, $subject, $receiptMessage,"From:$toAddress"); 
//---------------------------------- 
mail($toAddress,$recipientSubject,$mailContent,"From:$email"); 
//--->echo $mailContent; 

////////////////////////////////////////  CONNECT TO MYSQL DB  //////////////////// 
// OPEN CONNECTION ---> 
$connection=mysql_connect("localhost","user", "pass") or die("Unable to connect!"); /* change this! */ 

mysql_select_db("database") or die("Unable to select database!"); /* change this! */ 

//  EXECUTE QUERY ---> 
$query="INSERT INTO name ( 
            name, 
            email, 
            msg, 
            date, 
            category, 
            yname, 
            yemail, 
            time,) 
        VALUES( 
            '".$tname."', 
            '".$temail."', 
            '".$msg."', 
            '".$date."', 
            '".$cat."', 
            '".$yname."', 
            '".$yemail."', 
            '".$time."',)"; 
//////-----> 
$result=mysql_query($query) or die("Error in query:".mysql_error()); 
//if ($result) 
    //echo mysql_affected_rows()." row inserted into the database effectively."; 

//  CLOSE CONNECTION ---> 
mysql_close($connection); 

	// delete the cookie so it cannot sent again by refreshing this page
	setcookie('tntcon','');
} else {
	// if verification code was incorrect then return to contact page and show error amd keep all the field fillex out
	header("Location:".$_SERVER['HTTP_REFERER']."?tname=$tname&temail=$temail&yname=$yname&yemail=$yemail&wrong_code=true");
	exit;

/////////////////////////////////////////////////////////////////////////////////// 
        } 
    else{ 

            print "Sorry, but the form cannot be sent until the fields indicated are filled out completely - <br>n"; 
            print "$error<br>n"; 
            print "<br>n"; 
            print "<br>n"; 
            print "Please use your "Back" button to return to the form to correct the omissions.  Thank you.<br>\n"; 
        } 

?> 
</body> 
</html>

any help would be great thanks
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

a couple things to point out:

Code: Select all

$site_name=$_POST["mysite.com"];
$site_email=$_POST["mail@mail.com"];
$site_url=$_POST["www.mysite.com"];
these are invalid post names. you should NEVER use anything that has any form of url character in it, specifially the . or @

also, i wouldnt use if($varname) in this instance, because you never really know what that will be equal to... i would reccomend using something like

Code: Select all

if(empty($var)){
empty(); not only works on something that is a boolean false ( 0 or FALSE ), but also works on "" (if no data was submitted).

also be sure that your error reporting is on and properly working. add this to the beginning of your code:

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
?>
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Your query will definitely fail, so if you're not getting an error returned from mysql_error(), it's above that.

(You have a comma dangling after each reference to the `time` column)

Code: Select all

//-------->ERROR FREE??
    if($error==""){
        echo 'submit.php';
//-------------------------
What purpose does this serve, exactly? You want to display "submit.php"?
farkewie
Forum Newbie
Posts: 22
Joined: Sat Jun 02, 2007 11:25 pm

Post by farkewie »

Hello, thanks for your really fast reply, i have changed the code with your sugestions, i am now getting 2 errors,

here is my new code,

Code: Select all

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', true); 

/* grabs the POST variables and puts them into variables that we can use */ 
$time = date("d/m/Y " time());
$date = date("d/m/Y H:i:s" time());
$tname=$_POST['tname']; 
$temail=$_POST['temail']; 
$yname=$_POST['yname']; 
$yemail=$_POST['yemail']; 
$cat=$_POST['cat'];
$msg=$_POST['mag']; 
$site_name="mysite.com";
$site_email="mail@mail.com"; 
$site_url="www.mysite.com"; 
$verif_box = $_REQUEST["verif_box"];

// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
	// if verification code was correct send the message and show this page 



//---------------------------------- 
$mailContent="" 
            ."Hello xxxxsomeone has posted on the site.\n\n"            
			 ." ".$msg."\n\n\n\n" 

            ." ".$site_name."\n" 
			."--------------------------\n" 
            ." ".$site_url."\n";
//---------------------------------- 
$toAddress="mysite@mail.com";/* this is line 70 */ 
$subject="my subject"; /* change this! */ 
$recipientSubject="a new post"; /* change this! */ 
$receiptMessage = "" 
."Hello ".$tname." you.nn"             ." ".$msg."nnnn" 

            ." ".$site_name."n" 
			."--------------------------n" 
            ." ".$site_url."n";
//---------------------------------- 
mail($email, $subject, $receiptMessage,"From:$toAddress"); 
//---------------------------------- 
mail($toAddress,$recipientSubject,$mailContent,"From:$email"); 
//--->echo $mailContent; 

////////////////////////////////////////  CONNECT TO MYSQL DB  //////////////////// 
// OPEN CONNECTION ---> 
$connection=mysql_connect("localhost","user", "pass") or die("Unable to connect!"); /* change this! */ 

mysql_select_db("database") or die("Unable to select database!"); /* change this! */ 

//  EXECUTE QUERY ---> 
$query="INSERT INTO name ( 
            name, 
            email, 
            msg, 
            date, 
            category, 
            yname, 
            yemail, 
            time,) 
        VALUES( 
            '".$tname."', 
            '".$temail."', 
            '".$msg."', 
            '".$date."', 
            '".$cat."', 
            '".$yname."', 
            '".$yemail."', 
            '".$time."',)"; 
//////-----> 
$result=mysql_query($query) or die("Error in query:".mysql_error()); 
//if ($result) 
    //echo mysql_affected_rows()." row inserted into the database effectively."; 

//  CLOSE CONNECTION ---> 
mysql_close($connection); 

	// delete the cookie so it cannot sent again by refreshing this page
	setcookie('tntcon','');
} else {
	// if verification code was incorrect then return to contact page and show error amd keep all the field fillex out
	header("Location:".$_SERVER['HTTP_REFERER']."?tname=$tname&temail=$temail&yname=$yname&yemail=$yemail&wrong_code=true");
	exit;

/////////////////////////////////////////////////////////////////////////////////// 
        } 
    else{ 

            print "Sorry, but the form cannot be sent until the fields indicated are filled out completely - <br>n"; 
            print "$error<br>n"; 
            print "<br>n"; 
            print "<br>n"; 
            print "Please use your "Back" button to return to the form to correct the omissions.  Thank you.<br>\n"; 
        } 

?>
errors are

Code: Select all

Notice: Undefined index: mag in /home/tyspicsc/public_html/posthate/test submit/submit_action.php on line 12

Warning: Cannot modify header information - headers already sent by (output started at /home/tyspicsc/public_html/posthate/test submit/submit_action.php:12) in /home/tyspicsc/public_html/posthate/test submit/submit_action.php on line 84
i have seen the headers error before but nomally it is because i have a space after my closing tags but i cant find any this time???
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

farkewie wrote:i have seen the headers error before but nomally it is because i have a space after my closing tags but i cant find any this time???
this happens any time you echo something before you send a header, or if there are any characters/spaces/ANYTHING before the <?php tag
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

Also,

Code: Select all

Notice: Undefined index: mag in /home/tyspicsc/public_html/posthate/test submit/submit_action.php on line 12
is probably because you forgot to at the mag item to your form, or named it something else
farkewie
Forum Newbie
Posts: 22
Joined: Sat Jun 02, 2007 11:25 pm

Post by farkewie »

sorry im still new to php but im looking at the code i cant see any spaces or anything before my '<?php' tags,
i also cant see anything being echoed? mind you i am only looking for the word echo, i may not be undrstanding what you mean my echo completely
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

echo is as alias of print

also, you are getting the headers already sent before you have output being sent to the browser.. the notice about 'mag' being undefined is output :wink:
farkewie
Forum Newbie
Posts: 22
Joined: Sat Jun 02, 2007 11:25 pm

Post by farkewie »

well, i think i need to get a bit better at checking my spelling...lol.

'mag' was a miss spell was supposed to be 'msg' after fixing that i got a few more miss spell errors, and now the only error i get is this

Code: Select all

Error in query:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES( 'test@gmail.com', 'test' at line 9
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

change the ,) to ) in your query. (you have this in two places)
farkewie
Forum Newbie
Posts: 22
Joined: Sat Jun 02, 2007 11:25 pm

Post by farkewie »

Sorry i had another look before you replied and changed it to this,

Code: Select all

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', true); 

/* grabs the POST variables and puts them into variables that we can use */ 
$time = date("d/m/Y " time());
$date = date("d/m/Y H:i:s" time());
$tname=$_POST['tname']; 
$temail=$_POST['temail']; 
$yname=$_POST['yname']; 
$yemail=$_POST['yemail']; 
$cat=$_POST['cat'];
$msg=$_POST['msg']; 
$site_name="mysite.com";
$site_email="mail@mail.com"; 
$site_url="www.mysite.com"; 
$verif_box = $_REQUEST["verif_box"];

// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
	// if verification code was correct send the message and show this page 



//---------------------------------- 
$mailContent="" 
            ."Hello xxxxsomeone has posted on the site.\n\n"            
			 ." ".$msg."\n\n\n\n" 

            ." ".$site_name."\n" 
			."--------------------------\n" 
            ." ".$site_url."\n";
//---------------------------------- 
$toAddress="mysite@mail.com";/* this is line 70 */ 
$subject="my subject"; /* change this! */ 
$recipientSubject="a new post"; /* change this! */ 
$receiptMessage = "" 
."Hello ".$tname." you.nn"             ." ".$msg."nnnn" 

            ." ".$site_name."n" 
			."--------------------------n" 
            ." ".$site_url."n";
//---------------------------------- 
mail($temail, $subject, $receiptMessage,"From:$toAddress"); 
//---------------------------------- 
mail($toAddress,$recipientSubject,$mailContent,"From:$yemail"); 
//--->echo $mailContent; 

////////////////////////////////////////  CONNECT TO MYSQL DB  //////////////////// 
// OPEN CONNECTION ---> 
$connection=mysql_connect("localhost","user", "pass") or die("Unable to connect!"); /* change this! */ 

mysql_select_db("database") or die("Unable to select database!"); /* change this! */ 

//  EXECUTE QUERY ---> 
$query = "INSERT INTO name VALUES ('$tname','$temail','$msg','$time','$cat','$yname','$yemail','$date')";
	mysql_query($query);
$result=mysql_query($query) or die("Error in query:".mysql_error()); 
//if ($result) 
    //echo mysql_affected_rows()." row inserted into the database effectively."; 

//  CLOSE CONNECTION ---> 
mysql_close($connection); 

	// delete the cookie so it cannot sent again by refreshing this page
	setcookie('tntcon','');
} else {
	// if verification code was incorrect then return to contact page and show error amd keep all the field fillex out
	header("Location:".$_SERVER['HTTP_REFERER']."?tname=$tname&temail=$temail&yname=$yname&yemail=$yemail&wrong_code=true");
	exit;

?>
now emails work ok but it gets inserted into the database twice?

also if i type the image verification in wrong i get the below errors

Code: Select all

Warning: Header may not contain more than a single header, new line detected. in /home/tyspicsc/public_html/posthate/test submit/submit_action.php on line 67
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Code: Select all

mysql_query($query);
$result=mysql_query($query) or die("Error in query:".mysql_error());
You have two calls to mysql_query(), this is why you're getting two records inserted per script call.
farkewie
Forum Newbie
Posts: 22
Joined: Sat Jun 02, 2007 11:25 pm

Post by farkewie »

once again thanks for your help now everything works fine exept for when the verification image is typed wrong,


here is my latest code

Code: Select all

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', true); 

/* grabs the POST variables and puts them into variables that we can use */ 
$time = date("d/m/Y " time());
$date = date("d/m/Y H:i:s" time());
$tname=$_POST['tname']; 
$temail=$_POST['temail']; 
$yname=$_POST['yname']; 
$yemail=$_POST['yemail']; 
$cat=$_POST['cat'];
$msg=$_POST['msg']; 
$site_name="mysite.com";
$site_email="mail@mail.com"; 
$site_url="www.mysite.com"; 
$verif_box = $_REQUEST["verif_box"];

// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
	// if verification code was correct send the message and show this page 



//---------------------------------- 
$mailContent="" 
            ."Hello xxxxsomeone has posted on the site.\n\n"            
			 ." ".$msg."\n\n\n\n" 

            ." ".$site_name."\n" 
			."--------------------------\n" 
            ." ".$site_url."\n";
//---------------------------------- 
$toAddress="mysite@mail.com";/* this is line 70 */ 
$subject="my subject"; /* change this! */ 
$recipientSubject="a new post"; /* change this! */ 
$receiptMessage = "" 
."Hello ".$tname." you.nn"             ." ".$msg."nnnn" 

            ." ".$site_name."n" 
			."--------------------------n" 
            ." ".$site_url."n";
//---------------------------------- 
mail($temail, $subject, $receiptMessage,"From:$toAddress"); 
//---------------------------------- 
mail($toAddress,$recipientSubject,$mailContent,"From:$yemail"); 
//--->echo $mailContent; 

////////////////////////////////////////  CONNECT TO MYSQL DB  //////////////////// 
// OPEN CONNECTION ---> 
$connection=mysql_connect("localhost","user", "pass") or die("Unable to connect!"); /* change this! */ 

mysql_select_db("database") or die("Unable to select database!"); /* change this! */ 

//  EXECUTE QUERY ---> 
$query = "INSERT INTO name VALUES ('$tname','$temail','$msg','$time','$cat','$yname','$yemail','$date')";

$result=mysql_query($query) or die("Error in query:".mysql_error()); 
//if ($result) 
    //echo mysql_affected_rows()." row inserted into the database effectively."; 

//  CLOSE CONNECTION ---> 
mysql_close($connection); 

	// delete the cookie so it cannot sent again by refreshing this page
	setcookie('tntcon','');
} else {
	// if verification code was incorrect then return to contact page and show error amd keep all the field fillex out
	header ("Location:".$_SERVER['HTTP_REFERER']."?tname=$tname&temail=$temail&yname=$yname&yemail=$yemail&wrong_code=true&msg= .urlencode($msg)");	exit;


?>
i looked through the php manual, and found the "urlencode' so i managed to get rid of the headers on multiple lines but am still getting headers errors i dont see anything being printed or echoed, , below is the eror code.

Code: Select all

Notice: Undefined index: tntcon in /home/tyspicsc/public_html/posthate/test submit/submit_action.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/tyspicsc/public_html/posthate/test submit/submit_action.php:19) in /home/tyspicsc/public_html/posthate/test submit/submit_action.php on line 67
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

once again, your header warning is because of the first error. fix that and it should go away ($_COOKIE['tntcon'] is not defined)
farkewie
Forum Newbie
Posts: 22
Joined: Sat Jun 02, 2007 11:25 pm

thank oyu

Post by farkewie »

ok thanks you everyone for you help have never had so much help so fast and i really like the way you point me in the right direction instead of just telling me it really lets me learn,

this is the first script ive ever really done myself (stealing code from here and therre for some parts) thanks again
Post Reply