Guest book, NOT NULL response.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JW09
Forum Newbie
Posts: 14
Joined: Mon Feb 09, 2009 10:30 pm

Guest book, NOT NULL response.

Post by JW09 »

Hi again, here's a guest book I'm working on guest book. One problem I have is the NOT NULL response. I don't want a record created if any fields are empty so all sql fields are set to NOT NULL but the response is nasty. Is it possible to use NOT NULL and disable the response completely. I have other code to deal with the required field but the respone is triggered first. If you put text in name and comment it shows you a message Please enter a valid e-mail address : email, email field is set to NULL which creats an invalid slq record but allows validation code to run.

I know it's caused by the php at the top, I want it to prevent record but do nothing.

Code: Select all

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Guest book, NOT NULL response.

Post by califdon »

By defining a field in a table as NOT NULL, you are forcing MySQL to consider that an ERROR, and it will report such errors. The solution is to not define them that way, but do your data validation in your application code. If you want to notify the user in some kinder, gentler way, do it in Javascript before posting the form to the script to process further. To do it properly, you should again validate the data in the processing script before interacting with the database.
JW09
Forum Newbie
Posts: 14
Joined: Mon Feb 09, 2009 10:30 pm

Re: Guest book, NOT NULL response.

Post by JW09 »

Thanks, yea I want to do it with php, not keen on js popups. Here's the code which inserts record ..

Code: Select all

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "gb")) {
  $insertSQL = sprintf("INSERT INTO guest_book (name, email, `comment`) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['sign'], "text"));
 
  mysql_select_db($database_xxx, $xxx);
  $Result1 = mysql_query($insertSQL, $xxx) or die(mysql_error());
}
Here's the hidden field ..

Code: Select all

<input type="hidden" name="MM_insert" value="gb" />
This is working fine and if I set structures to allow NULL my other validation code runs but I get an invalid record. I think it needs something before the insert code ..

if after posting gb == "" ignore $insertSQL I just don't know how to write it. I don't want the code to die because there's code later on which deals with validation, need it to skip a step.
JW09
Forum Newbie
Posts: 14
Joined: Mon Feb 09, 2009 10:30 pm

Re: Guest book, NOT NULL response.

Post by JW09 »

Well I think you're right js is only way to halt php. I tried moving the php about but it inserts no matter what. I guess there's nothing really to stop it. Anyway have looked at js and got a popup working but I really dispise them. I could create my own but I'd prefer to have it like my php validation which loads a simple bit of text into a table cell. So how would I make the js write something to a cell and how would I alter the cell to make it recognise the function?

The other way I thought of was to make js load an htm page in a target, I can then create an iframe in a cell and have the message in a simple htm page.

EDIT : Something like this might work ..

Code: Select all

<script type="text/javascript" language="JavaScript">
<!--
function validate(check) // function that checks if the Title field is empty
{
if(check.name.value=="")
{
[b]self.frames['framename'].location.href = 'page.htm'; [/b]
return(false);
}
else {return(true);}
}  
//-->
</script>
JW09
Forum Newbie
Posts: 14
Joined: Mon Feb 09, 2009 10:30 pm

Re: Guest book, NOT NULL response.

Post by JW09 »

This is possibly a no no but how would I add email validation to this js function ..

Code: Select all

 
function validate(check) // function that checks if the Title field is empty
{
if(check.name.value=="")
{
self.frames['required'].location.href = 'gb_nr.htm' 
return(false);
}
if(check.comment.value=="")
{
self.frames['required'].location.href = 'gb_nr.htm' 
return(false);
}
else {return(true);}
}
Have tried loads of different versions but doens't get triggered. Must be because I'm trying them in a function?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Guest book, NOT NULL response.

Post by califdon »

I'm sorry I don't have time to study your whole script, but let me just say that using javascript doesn't mean you have to use alerts, that's only one option. You can change the color of the border of an <input> to red, for example, or make a <div> visible that displays an error message, or insert specific words into an empty <div>, using the .innerhtml property... There are lots of things you can do in javascript, you just have to decide what you'd like to do.
JW09
Forum Newbie
Posts: 14
Joined: Mon Feb 09, 2009 10:30 pm

Re: Guest book, NOT NULL response.

Post by JW09 »

Hey, I read up on innerhtml it sounds OK except doesn't work in IE6 and besides I can't get it to work. I use this ..

Code: Select all

function validate(check)
{
if(check.name.value=="")
{
document.getElementById('name').innerHTML = 'Message';
return(false);
}
And in body ..

Code: Select all

<div id='name'></div>

but nothing happens when I press submit, in form is onsubmit="return validate(this)". This is xhtml file. iFrames method works and is only a 1kb file but I wonder why this doesn't work. Also don't know why email validation doesn't work, could perhaps be a function structure issue. I will find me a js forum and find out.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Guest book, NOT NULL response.

Post by califdon »

I suspect you're not giving it the object reference that you think you are, when you use onSubmit='validate(this);' Try calling the validate() from the onBlur event of the specific <input> element (name, in this case).
JW09
Forum Newbie
Posts: 14
Joined: Mon Feb 09, 2009 10:30 pm

Re: Guest book, NOT NULL response.

Post by JW09 »

Well I want name and comment required, email isn't required but it must be valid. I quite like my iframe approach especially after having read stuff about onblur and innerhtml issues with certain browsers. It's all working except for valid email. I've tried about four different things, learned an onsubmit in form can include several functions but none of the functions I've tried work. This looks fine to me, the second function works but with an invalid email it submits form.

Code: Select all

<script type="text/javascript" language="JavaScript">
function checkMail(email){
      var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      if (filter.test(email)) {
      return true;
      }
      else {
      self.frames['required'].location.href = 'gb_er.htm' 
      return false;
      }
      }
</script>
 
<script type="text/javascript" language="JavaScript">
function validate(check) // function that checks if the Title field is empty
{
if(check.name.value==""){
self.frames['required'].location.href = 'gb_nr.htm' 
return(false);
}
if(check.comment.value==""){
self.frames['required'].location.href = 'gb_cr.htm' 
return(false);
}
else {return(true);}
}
</script>
In form ..

Code: Select all

onsubmit="return validate(this); return checkMail(this)"
JW09
Forum Newbie
Posts: 14
Joined: Mon Feb 09, 2009 10:30 pm

Re: Guest book, NOT NULL response.

Post by JW09 »

Reason is first return cancel any function after first one. Works if using this ..

Code: Select all

onsubmit="return (validate(this) && checkEmail(this))"
Thanks again, have learned quite a lot here.
Post Reply