4 Replies - 626 Views - Last Post: 08 August 2011 - 10:47 AM Rate Topic: -----

#1 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 817
  • Joined: 16-September 09

Can't seem to get my script to recognize NULL values

Posted 08 August 2011 - 10:29 AM

Hey guys!!

I've tried everything here but I can't get my script to do the right stuff if the value of one of my fields is null...could anyone take a peek please?

I have this so script
<?php
  //THIS IS WHERE WE VERIFY THE POST HAS THE RIGHT INFORMATION IN IT AND ADD TO DB
  session_start();
  require '/scripts/maindbconnect.php';
  
  $title=(string)$_POST['title'];
  $posting=(string)$_POST['posting'];
  $msg='';
  $status="OK";
  $createdate=date("m/d/y H:i:s", time());
  
  /*var_dump($title);
  var_dump($posting);
  exit;*/
  
  if(!isset($title) && strlen($title)>3){
    $status="NOTOK";
    $msg='Must have a title for every post!!! <br/> PLEASE TRY AGAIN';  
  }
  
  if(!isset($posting) && strlen($posting)>3){
    $status="NOTOK";
    $msg='Posting my have a body or there will be nothing to read <br/> PLEASE TRY AGAIN';
  }
  
  $myid=$_SESSION['userid'];
  $posting=str_
  replace("\n", '<br />', $posting);
  
  //var_dump($_SESSION['userid']);
  //var_dump($_SESSION[userid]);
  //var_dump($myqry);
  
  if($status<>"OK"){ 
  echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' value='Retry' onclick='history.go(-1)'>"; 
  }else{ // if all validations are passed.
  mysql_query("INSERT INTO posts (createdate, userid, title, body, views, active) VALUES('".$createdate. "'," .$myid. ", '" .$title. "', '" .$posting. "', 0, 1)") or die(mysql_error());
  header ('location: http://www.iwictu.com/viewmyposts.php');
  }
?>




I added the strlen piece figuring it might help but if I go to post a blank post it accepts it..

If anyone could point me in the right direction here that would be greatly appreciated!! Thanks in advance!!

Is This A Good Question/Topic? 0
  • +

Replies To: Can't seem to get my script to recognize NULL values

#2 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Can't seem to get my script to recognize NULL values

Posted 08 August 2011 - 10:33 AM

Check out empty();

if(empty($var)){
// $var is either false, 0, NULL, "", etc.
}


Was This Post Helpful? 1
  • +
  • -

#3 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 817
  • Joined: 16-September 09

Re: Can't seem to get my script to recognize NULL values

Posted 08 August 2011 - 10:38 AM

Thanks! I'll give it a shot...doesn't isset also check for null though? I just want to make sure I'm writing my scripts right in the future...

Thanks again! I'll report back after I try it!
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,239
  • Joined: 08-June 10

Re: Can't seem to get my script to recognize NULL values

Posted 08 August 2011 - 10:45 AM

if you only want a test for NULL, use if (NULL === $var) or if (is_null($var))
Was This Post Helpful? 0
  • +
  • -

#5 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 817
  • Joined: 16-September 09

Re: Can't seem to get my script to recognize NULL values

Posted 08 August 2011 - 10:47 AM

That works perfect...thank you very much!! I'll have to keep this in mind for the future!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1