Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 17th, 2005, 06:50 PM
Registered User
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validation and Insert problem

Hi,
This is my very first time posting here so I hope I’m following all the rules. If not, go easy as I’m also new to PHP but learning fast.
I wonder if anybody can see why the two codes will not work together.
For example at the moment if I enter details into the Form the info is
passed to the database without the validation kicking in. If I test each
separately i.e. validation without the 'insert record' code and the 'insert
recode' without the validation code, both work perfectly but just placing them
together causes the validation to be ignored.
 I know that I need to make sure the insert code is only called when everything else is fine but I’m at this two days now and just can’t seem to see where I’m going in order to correct this.

Any help is much appreciated.

Thanks
Brian



<?php require_once('Connections/b.php'); ?>
<?php
if ($_POST && array_key_exists('sendCom',$_POST)) {
  $nomessage='';
  $GuestName='';
// Check each field and build errors array if problems found
if (isset($_POST['GuestDetails']) && !empty($_POST['GuestDetails'])) {
  $message=strip_tags($_POST['GuestDetails']);
  }
else {
  $nomessage = 'Message';
  }
if (isset($_POST['GuestName']) && !empty($_POST['GuestName'])) {
  $GuestName=trim($_POST['GuestName']);
  }
else {
  $error['GuestName'] = 'You must give your name';
}
}
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
$theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO tblguestbook (GuestName, GuestLocation,
GuestDetails, GuestWebsite, GuestEmail, GuestDate) VALUES (%s,%s, %s, %s,
%s, CURDATE())",
                       GetSQLValueString($_POST['GuestName'], "text"),
                       GetSQLValueString($_POST['GuestLocation'], "text"),
                       GetSQLValueString($_POST['GuestDetails'], "text"),
                       GetSQLValueString($_POST['GuestWebsite'], "text"),
                      GetSQLValueString($_POST['GuestEmail'], "text"));
  mysql_select_db($database_brian, $brian);
  $Result1 = mysql_query($insertSQL, $brian) or die(mysql_error());
  $insertGoTo = "guestbook.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>

<?php
// Display error message if errors have been found in submission
if (isset($nomessage) || isset($error)) {
?>
Error.
<?php
  }
?>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
GuestName:
              <?PHP
            if(isset($error)) {//Display error essage.Otherwise skip row.
            foreach ($error as $key=> $value){ //Loop through error message,
and display
            echo $value;
            }
            }
            ?>
      <input type="text" name="GuestName" value="" size="32">
    GuestLocation:
      <input type="text" name="GuestLocation" value="" size="32">
    GuestDetails:
              <?php if (isset($nomessage) && !empty($nomessage)) {
                          echo $nomessage; } else {
                          } ?>
   <textarea name="GuestDetails" cols="55" rows="10" id="GuestDetails"
 ><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea>
GuestWebsite:
      <input type="text" name="GuestWebsite" value="" size="32">
    GuestEmail:
      <input type="text" name="GuestEmail" value="" size="32">
      <input name="sendCom" type="submit" id="sendCom" value="Post Message"
/>

               <input name="Reset" type="reset" value="Reset">
<input type="hidden" name="MM_insert" value="form1">
</form>







Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation Control Problem manojbhoyar ASP.NET 1.0 and 1.1 Basics 4 June 20th, 2008 04:05 AM
validation problem Ashleek007 JSP Basics 1 June 18th, 2007 01:16 AM
Validation problem monika.vasvani ASP.NET 1.0 and 1.1 Professional 1 November 25th, 2006 01:44 PM
validation problem Abhinav_jain_mca General .NET 6 August 5th, 2004 02:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.