mark103 Posted January 20, 2011 Share Posted January 20, 2011 Hi guys, I have a problem with the code on below. When I input the value into $name and $email method, the page come into blank page. <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mydbpassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $name = clean($_GET['name']); $email = clean($_GET['email']); $comments = clean($_GET['comments']); $type = clean($_GET['type']); if($name == '') { $errmsg_arr[] = 'name or member ID missing'; $errflag = true; } if($email == '') { $errmsg_arr[] = 'email address ID missing'; $errflag = true; } else { } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['name'])) { $insert[] = 'name = \'' . clean($_GET['name']) .'\''; // echo "tested"; } if(isset($_GET['email'])) { $insert[] = 'email = \'' . clean($_GET['email']) . '\''; } if(isset($_GET['type'])) { $insert[] = 'type = \'' . clean($_GET['type']) . '\''; } if(isset($_GET['comments'])) { $insert[] = 'comments = \'' . clean($_GET['comments']) . '\''; } if (count($insert)>0) { $names = implode(',',$insert); if(isset($name)) { $name = $_GET['name']; $headers = "From: "-f .$name."@myemail.com"; $to = "[email protected]"; $subject = $type; $message = $comments . '; $header .= "MIME-Version: 1.0\l\n"; $add = "-f". $name ."@myemail.com"; mail($to, $subject, $message, $header, $add); echo "Thank you for sent us your feedback"; } else { if(isset($email)) { $email = $_GET['email']; $headers = "From: "$email"; $to = "[email protected]"; $subject = $type; $message = $comments . '; $header .= "MIME-Version: 1.0\l\n"; $add = "-f". $email ."; mail($to, $subject, $message, $headers, $add); } echo "Thank you for sent us your email"; } } } ?> There must be the problem coming from this: if(isset($name)) { $name = $_GET['name']; $headers = "From: "-f .$name."@myemail.com"; $to = "[email protected]"; $subject = $type; $message = $comments . '; $header .= "MIME-Version: 1.0\l\n"; $add = "-f". $name ."@myemail.com"; mail($to, $subject, $message, $header, $add); echo "Thank you for sent us your feedback"; } else { if(isset($email)) { $email = $_GET['email']; $headers = "From: "$email"; $to = "[email protected]"; $subject = $type; $message = $comments . '; $header .= "MIME-Version: 1.0\l\n"; $add = "-f". $email ."; mail($to, $subject, $message, $headers, $add); } I am not sure where the problem is, so please could you help me?? Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/ Share on other sites More sharing options...
Pikachu2000 Posted January 20, 2011 Share Posted January 20, 2011 Look at the syntax coloring in the code you just posted, and the problem is right there, staring you in the face. Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162545 Share on other sites More sharing options...
Maq Posted January 20, 2011 Share Posted January 20, 2011 Also a tip for when you see a blank page that contains PHP, it usually is some fatal error. To temporarily turn on error reporting you can put these 2 lines directly below your opening <?php tag: ini_set ("display_errors", "1"); error_reporting(E_ALL); Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162548 Share on other sites More sharing options...
mark103 Posted January 20, 2011 Author Share Posted January 20, 2011 still the same, any idea??? Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162552 Share on other sites More sharing options...
Maq Posted January 20, 2011 Share Posted January 20, 2011 still the same, any idea??? Did you add the error reporting lines? Post the current code. Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162559 Share on other sites More sharing options...
mark103 Posted January 20, 2011 Author Share Posted January 20, 2011 yes i did so here it is: <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mydbpassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $name = clean($_GET['name']); $email = clean($_GET['email']); $comments = clean($_GET['comments']); $type = clean($_GET['type']); if($name == '') { $errmsg_arr[] = 'name or member ID missing'; $errflag = true; } if($email == '') { $errmsg_arr[] = 'email address ID missing'; $errflag = true; } else { } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['name'])) { $insert[] = 'name = \'' . clean($_GET['name']) .'\''; // echo "tested"; } if(isset($_GET['email'])) { $insert[] = 'email = \'' . clean($_GET['email']) . '\''; } if(isset($_GET['type'])) { $insert[] = 'type = \'' . clean($_GET['type']) . '\''; } if(isset($_GET['comments'])) { $insert[] = 'comments = \'' . clean($_GET['comments']) . '\''; } if (count($insert)>0) { $names = implode(',',$insert); if(isset($name)) { $name = $_GET['name']; $headers = "From: "-f .$name."@myemail.com"; $to = "[email protected]"; $subject = $type; $message = $comments . '; $header .= "MIME-Version: 1.0\l\n"; $add = "-f". $name ."@myemail.com"; mail($to, $subject, $message, $header, $add); echo "Thank you for sent us your feedback"; } else { if(isset($email)) { $email = $_GET['email']; $headers = "From: "$email"; $to = "[email protected]"; $subject = $type; $message = $comments . '; $header .= "MIME-Version: 1.0\l\n"; $add = "-f". $email ."; mail($to, $subject, $message, $headers, $add); } echo "Thank you for sent us your email"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162563 Share on other sites More sharing options...
TOA Posted January 20, 2011 Share Posted January 20, 2011 Look at the syntax coloring in the code you just posted, and the problem is right there, staring you in the face. He told you the problem. You have missing single quotes Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162566 Share on other sites More sharing options...
Maq Posted January 20, 2011 Share Posted January 20, 2011 still the same, any idea??? Did you add the error reporting lines? Post the current code. Or an extra unnecessary single quote, on two separate lines. Surprised the error reporting didn't pick that up. Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162567 Share on other sites More sharing options...
mark103 Posted January 20, 2011 Author Share Posted January 20, 2011 Look at the syntax coloring in the code you just posted, and the problem is right there, staring you in the face. He told you the problem. You have missing single quotes Look at the syntax coloring in the code you just posted, and the problem is right there, staring you in the face. He told you the problem. You have missing single quotes where I have missing the single quotes? I don't see anything with this code: <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'user'); define('DB_PASSWORD', 'pass'); define('DB_DATABASE', 'dbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $name = clean($_GET['name']); $email = clean($_GET['email']); $type = clean($_GET['type']); $comments = clean($_GET['comments']); if($name == '') { $errmsg_arr[] = 'name or member ID missing'; $errflag = true; } if($email == '') { $errmsg_arr[] = 'email address ID missing'; $errflag = true; } else { } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['name'])) { $insert[] = 'name = \'' . clean($_GET['name']) .'\''; // echo "tested"; } if(isset($_GET['email'])) { $insert[] = 'email = \'' . clean($_GET['email']) . '\''; } if(isset($_GET['type'])) { $insert[] = 'type = \'' . clean($_GET['type']) . '\''; } if(isset($_GET['comments'])) { $insert[] = 'comments = \'' . clean($_GET['comments']) . '\''; } if (count($insert)>0) { $names = implode(',',$insert); if(isset($name)) { $name = $_GET['name']; $headers = "From: "-f .$name."@myemail.com"; $to = "[email protected]"; $subject = $type; $message = $comments . ; $header .= "MIME-Version: 1.0\l\n"; $add = "-f". $name ."@myemail.com"; mail($to, $subject, $message, $header, $add); } else { if(isset($email)) { $email = $_GET['email']; $headers = "From: "-f .$email."; $to = "[email protected]"; $subject = $type; $message = $comments . ; $header .= "MIME-Version: 1.0\l\n"; $add = "-f". $email ."; mail($to, $subject, $message, $header, $add); echo "Thank you for sent us your feedback"; } } } } ?> Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162575 Share on other sites More sharing options...
Maq Posted January 20, 2011 Share Posted January 20, 2011 Please, when we ask you to post your current code don't change it. Previously you had: $message = $comments . '; Now you have: $message = $comments . ; Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162577 Share on other sites More sharing options...
mark103 Posted January 20, 2011 Author Share Posted January 20, 2011 the code i have post on above are my current code. I don't change anything? Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162580 Share on other sites More sharing options...
Maq Posted January 20, 2011 Share Posted January 20, 2011 the code i have post on above are my current code. I don't change anything? You did change it. Remove the dot on the line I pointed out (there are two). Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162582 Share on other sites More sharing options...
mark103 Posted January 20, 2011 Author Share Posted January 20, 2011 I did it so thanks for your help. However I have got a problem with this: $email = $_GET['email']; $headers = "From: "$email; $to = "[email protected]"; $subject = $type; $message = "$comments" ; $header .= "MIME-Version: 1.0\l\n"; $add = " $email; mail($to, $subject, $message, $header, $add) ; I am trying to get the value from the $email method if I am using the $email method, but the php page goes on blank. Any idea? Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162620 Share on other sites More sharing options...
radi8 Posted January 20, 2011 Share Posted January 20, 2011 you posted: $email = $_GET['email']; $headers = "From: "$email; $to = "[email protected]"; $subject = $type; $message = "$comments" ; $header .= "MIME-Version: 1.0\l\n"; $add = " $email; mail($to, $subject, $message, $header, $add) Please change the following line to this: // $add = " $email; <-- original $add = $email; // <-- new // mail($to, $subject, $message, $header, $add) <-- original mail($to, $subject, $message, $header, $add); // <--new Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162630 Share on other sites More sharing options...
Maq Posted January 20, 2011 Share Posted January 20, 2011 Are you kidding me? STOP changing your code! If you do it again I'm not helping you. This line is causing the error, similar to before. $add = " $email; Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162631 Share on other sites More sharing options...
radi8 Posted January 20, 2011 Share Posted January 20, 2011 If you do not understand basic syntax and PHP code structure, get a book and read. Stop this copy and pasting of other peoples code and expecting it to work. Learn this language, you will be so much better off. Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162632 Share on other sites More sharing options...
mark103 Posted January 20, 2011 Author Share Posted January 20, 2011 Thanks guys! I am sorry about before, as I had to find a way to correct the problem as I had got. So the problem have been fixed. Now I want to know how I can use one of these method at a time, like I meant that if I use $name to extract the value then don't use $email otherwise use $email to extract the value while I don't use $name? Something got to do with this: if($name == '') { $errmsg_arr[] = 'name or member ID is missing'; $errflag = true; } else { } if($email == '') { $errmsg_arr[] = 'email address ID is missing'; $errflag = true; } Here's the update code: <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'myusername'); define('DB_PASSWORD', 'mypassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $name = clean($_GET['name']); $email = clean($_GET['email']); $type = clean($_GET['type']); $comments = clean($_GET['comments']); if($name == '') { $errmsg_arr[] = 'name or member ID is missing'; $errflag = true; } else { } if($email == '') { $errmsg_arr[] = 'email address ID is missing'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['name'])) { $insert[] = 'name = \'' . clean($_GET['name']) .'\''; } if(isset($_GET['email'])) { $insert[] = 'email = \'' . clean($_GET['email']) . '\''; } if(isset($_GET['type'])) { $insert[] = 'type = \'' . clean($_GET['type']) . '\''; } if(isset($_GET['comments'])) { $insert[] = 'comments = \'' . clean($_GET['comments']) . '\''; } if (count($insert)>0) { $names = implode(',',$insert); if(isset($name)) { $name = $_GET['name']; $headers = "From: "-f .$name."@myemail.com"; $to = "[email protected]"; $subject = $type; $message = "$comments" ; $header .= "MIME-Version: 1.0\l\n"; $add = "-f". $name ."@myemail.com"; mail($to, $subject, $message, $header, $add); echo "Thank you for sent us your feedback"; } else { if(isset($email)) { $email = $_GET['email']; $headers = "From: $email"; $to = "[email protected]"; $subject = $type; $message = "$comments"; $header .= "MIME-Version: 1.0\l\n"; $add = $email; mail($to, $subject, $message, $header, $add); } echo "Thank you for sent us your email"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162636 Share on other sites More sharing options...
Maq Posted January 20, 2011 Share Posted January 20, 2011 Use elseif's so only 1 block will be executed. Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162644 Share on other sites More sharing options...
mark103 Posted January 20, 2011 Author Share Posted January 20, 2011 Use elseif's so only 1 block will be executed. yeah, it has been executed so how can i use them either at a time?? Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162770 Share on other sites More sharing options...
Pikachu2000 Posted January 20, 2011 Share Posted January 20, 2011 http://www.php.net/manual/en/control-structures.elseif.php Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162777 Share on other sites More sharing options...
Maq Posted January 20, 2011 Share Posted January 20, 2011 Use elseif's so only 1 block will be executed. yeah, it has been executed so how can i use them either at a time?? Yes, I already explained it. Remove the else in between the two blocks and use an elseif for the 'email' block. This way, only one will be executed. Read Pika's link before you try. Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162783 Share on other sites More sharing options...
mark103 Posted January 21, 2011 Author Share Posted January 21, 2011 Thanks for the help guys, I have tried this to use one to be executed at a time, but it have been required which both have to be executed at the same time which I only want one to be executed at a time. if($name == ''): $errmsg_arr[] = 'name or member ID is missing'; $errflag = true; elseif($email == ''): $errmsg_arr[] = 'email address ID is missing'; $errflag = true; endif; Any idea? Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162791 Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 Surprised the error reporting didn't pick that up. Php is a parsed, tokenized, interpreted language. If the code never reaches the interpreted stage due to a fatal parse error, how could the lines of code setting the error_reporting/display_errors have an effect? If you set the error_reporting and display_errors settings in your php.ini, php will display all the errors it detects, even fatal parse errors in the main file (it turns out that putting the two settings into your main code will display parse errors in included files.) You also won't need to remember to put those settings into your code or remember to remove them when you put your code onto a live server. As to your current problem, you are apparently asking that your validation logic test if both are empty or if both are set (assuming that you don't want both to be set at the same time.) This would detect if only one is set and produce error messages if both are not set or if both are set - // if both are empty, error // if both are set, error if($name == '' && $email == ''){ // both are empty $errmsg_arr[] = 'Both name and email are missing. You must enter one or the other.'; $errflag = true; } elseif($name != '' && $email != '' ){ // both are set $errmsg_arr[] = 'Both name and email are set. You must only enter one or the other.'; $errflag = true; } Also, why did you switch to using the alternate if:/elseif:/endif; syntax? You shouldn't mix regular and the alternate syntax in your code. Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1162907 Share on other sites More sharing options...
Maq Posted January 21, 2011 Share Posted January 21, 2011 Php is a parsed, tokenized, interpreted language. If the code never reaches the interpreted stage due to a fatal parse error, how could the lines of code setting the error_reporting/display_errors have an effect? Yes, makes perfect sense. I think I've actually made that comment before and you've explained it. :/ Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1163078 Share on other sites More sharing options...
litebearer Posted January 21, 2011 Share Posted January 21, 2011 "honest, dad/mom/honey/dear/techguy/cat/dog, I didn't do anything!" Link to comment https://forums.phpfreaks.com/topic/225089-if-variable-not-working/#findComment-1163106 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.