Jump to content


Photo

Isset question


  • Please log in to reply
1 reply to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 21 July 2006 - 04:18 AM

I am attempting to create a mysql chat script and am running into a little trouble with the login script.

The problem that I am having is that the errors are being processed before the user has a chance to enter the required information. Here is a relivant section of the script.

<?php
include('includes/db.php');
if (isset($submit)) {
    array_pop($_POST);
    if ( get_magic_quotes_gpc() ) {
       $_POST= array_map('stripslashes', $_POST);
    }
    $first_name = mysql_real_escape_string(trim($_POST['first_name']));
    $last_name = mysql_real_escape_string(trim($_POST['last_name']));
    $email = mysql_real_escape_string(trim($_POST['email']));
    $status = "waiting";
   
    if ((!$first_name) || (!$last_name) || (!$email) || (!$deparment)) {
        $message = "error";
	    if (!$first_name) {
	        $error = "first_name";
	    }
	    if (!last_name) {
	        $error = "last_name";
	    }
	    if (!$email) {
	        $error = "email";
	    }
	    if (!$department) {
	        $error = "department";
	    }
        exit();
    }

    $sql = mysql_query("INSERT INTO chat (first_name, last_name, email, department, status, chat_date, chat_time)
           VALUES ('$first_name', '$last_name', '$email', '$department', '$status', now(), now())") or die(mysql_error());
	   
    if (!$sql) {
	    $message = "sql";
    }
    session_start();

    $session_sql = "SELECT chat_id FROM chat WHERE email ='$email' AND status ='waiting'";
    $get_session = mysql_query($session_sql) or die(mysql_error());

    if (!$get_session) {
         $message = "session";
    }
    $_SESSION['chat_id'] = "$get_session";
    header("Location: http://www.owpt.biz/chat.php");	
}		    
?>


Should I use


<?php
if (isset($submit)) {
   if (empty($variable)) {
      $error = "whatever"
   }
   //the rest of my error checking
}



Any suggestions would be great.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 21 July 2006 - 04:57 AM

you should know the different between isset and empty.

case1:
<?php
  $var = null;

  isset($var) will return true here;
  empty($var) will return true here;
?>

case 2:
<?php
  isset($var) will return false here;
  empty($var) will return true here;
?>

case 3:
<?php
  $var = 'something';

  isset($var) will return true here;
  empty($var) will return false here;
?>
Life's too short for arguing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users