Jump to content

help: it always say cannot be null

- - - - -

  • Please log in to reply
5 replies to this topic

#1
wheay

wheay

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
do you know whats the problem of this code.
when i submit, it always say "Column 'contact_no' cannot be null" with out the quote.

i have assigned everything right, maybe its a single quote or something.

pls. help me. Thank you.


//recieve the variables

	$fname = $_POST['fname'];

	$mname = $_POST['mname'];

	$lname = $_POST['lname'];

	$address = $_POST['haddress'];

	$email = $_POST['email'];	

	$contact = $_POST['contact_no'];

	$job = $_POST['job_title'];

	$fguess = $_POST['firstguess'];

	$sguess = $_POST['secondguess'];

	$tguess = $_POST['thirdguess'];

	$ip = gethostbyname($_SERVER['REMOTE_ADDR']);

	

	//save the data on the DB

	

	mysql_select_db($database_connection, $connection);

	$insert_query = sprintf("INSERT INTO shinygame (fname, mname, lname, haddress, email, contact_no, job_title, firstguess, secondguess, thirdguess, date, ip) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), %s)", 

		sanitize($fname, "text"),

		sanitize($mname, "text"),

		sanitize($lname, "text"),

		sanitize($address, "text"),

		sanitize($email, "text"),

		sanitize($contact, "text"),

		sanitize($job, "text"),

		sanitize($fguess, "int"),

		sanitize($sguess, "int"),

		sanitize($lguess, "int"),	

		sanitize($ip, "text"));	

	

	$result = mysql_query($insert_query, $connection) or die(mysql_error());



#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
If a $_POST key does not exist it should be NULL, and likely your sanitize function just passes NULL again. Try using print_r on $_POST to test if it exists, and as well use isset() to verify on every one.

We can only guess why this happens, as you have not provided any error checking code or the sanitize function.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
wheay

wheay

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
hi sorry here is the sanitary code:

i just copied the code from another source(free).
he's code is working i just change it to fit mine.
i wonder why?

Thank for your help!

function sanitize($value, $type) 

{

  $value = (!get_magic_quotes_gpc()) ? addslashes($value) : $value;


  switch ($type) {

    case "text":

      $value = ($value != "") ? "'" . $value . "'" : "NULL";

      break;    

    case "long":

    case "int":

      $value = ($value != "") ? intval($value) : "NULL";

      break;

    case "double":

      $value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL";

      break;

    case "date":

      $value = ($value != "") ? "'" . $value . "'" : "NULL";

      break;

  }

  

  return $value;

}


#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You see, they use ternary operators. If $value were NULL (or empty), it would return NULL as a string. MySQL does not allow NULL field wherever it is, so this means that contact_no has not properly been formed, you must debug this and ensure that contact_no is properly set.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
rySch

rySch

    Newbie

  • Members
  • Pip
  • 8 posts
I thought the ternary operator was formatted like this:

$value = (<condition>?<true>:<false>);

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

rySch said:

I thought the ternary operator was formatted like this:

$value = (<condition>?<true>:<false>);

It is, his use is this:
$value = ($value != "") ? "'" . $value . "'" : "NULL";
or 
$value = <comparison> ? true (assign 'value') : false (assign NULL)

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users