Jump to content

if variable not working


mark103

Recommended Posts

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 = "myname@mymail.com";
   $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 = "myname@mymail.com";
   $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 = "myname@mymail.com";
   $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 = "myname@mymail.com";
   $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
Share on other sites

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
Share on other sites

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 = "myname@mymail.com";
   $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 = "myname@mymail.com";
   $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
Share on other sites

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
Share on other sites

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 = "myname@myemail.com";
   $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 = "myname@myemail.com";
   $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
Share on other sites

I did it so thanks for your help. However I have got a problem with this:

 

   $email = $_GET['email'];
   $headers = "From: "$email; 
   $to = "myname@myemail.com";
   $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
Share on other sites

 

you posted:

$email = $_GET['email'];
   $headers = "From: "$email; 
   $to = "myname@myemail.com";
   $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
Share on other sites

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 = "myname@myemail.com";
   $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 = "myname@myemail.com";
   $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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.