zero-perfoliate
zero-perfoliate

Author Topic: error checking  (Read 187 times)

Offline fife

  • New PHP Members
  • Posts: 1
  • Karma: +0/-0
error checking
« on: July 27, 2010, 12:30:42 PM »
hey, new to this php and its amazingly difficult to get your head around.  I've just wrote my first INSERT which works great but I've been trying to add error checking which does not allow empty field.  I will post my code here.  for some reason my error checking is not working but i can't see my mistake.  Can anyone help?  Also is it possible to add multiple checks on one field with only one if statement?  i.e check there is something there while making sure its over so many characters?  I know I'm really asking two questions here.  If anyone could answer just the first question that would be great thank you

Code: [Select]

<?php include('database name here');
session_start(); 
$validation_id strval(time());

if(isset(
$_POST['submit'])) {

if(
$_POST['first_name'] == "") {
$message "Please enter a first name";
$success 0;
}
else if($_POST['last_name'] == "") {
$message "Please enter a surname";
$success 0;
}
else if($_POST['DOB'] == "") {
$message "Please enter your birthday in the format dd/mm/yyy";
$success 0;
}
else if($_POST['email'] == "") {
$message "Please enter the user's email";
$success 0;
}
else if($_POST['username'] == "") {
$message "Please enter a username";
$success 0;
}
else if($_POST['password'] == "") {
$message "Please enter a password";
$success 0;
}


 
$first_name mysql_real_escape_string($_POST['first_name']);
  
$last_name mysql_real_escape_string($_POST['last_name']);
   
$DOB mysql_real_escape_string($_POST['DOB']);
    
$sex mysql_real_escape_string($_POST['sex']);
 $email mysql_real_escape_string($_POST['email']);
  $username mysql_real_escape_string($_POST['username']);
   $password mysql_real_escape_string($_POST['password']); 
   $agree mysql_real_escape_string($_POST['agreed']);
    $creation_date mysql_real_escape_string($_POST['creation_date']);
$user_type mysql_real_escape_string($_POST['member_type']);
 $access_level mysql_real_escape_string($_POST['access_level']);
  $validation mysql_real_escape_string($_POST['validation_id']);
     $club_user mysql_real_escape_string($_POST['user_type']);
  
$insert_member"INSERT INTO Members (`first_name`,`last_name`,`DOB`,`sex`,`email`,`username`,`password`,`agree`,`creation_date`,`usertype`,`access_level`,`validationID`) 
VALUES 
('"
.$first_name."','".$last_name."','".$DOB."','".$sex."','".$email."','".$username."','".$password."','".$agree."','".$creation_date."','".$user_type."','".$access_level."', '".$validation."')";

$insert_member_nowmysql_query($insert_member) or die(mysql_error());
}

Offline TinMonkey

  • PHP Workers
  • **
  • Posts: 5
  • Karma: +0/-0
Re: error checking
« Reply #1 on: August 07, 2010, 03:27:26 AM »
you need to put each check in its own if statement. once and else if loop finds a match it stops executing the rest of the statements. Also, you need to write the errors to an array in case you have multiple errors on a form.
Code: [Select]
<?php include('database name here');
session_start(); 
$validation_id strval(time());

if (isset(
$_POST['submit'])) {

// Create empty array to hold errors
$errors = array();

if ($_POST['first_name'] == "") {
// append new entry into the array
$errors[] = "Please enter a first name";
}
if ($_POST['last_name'] == "") {
$errors[] = "Please enter a surname";
}
if ($_POST['DOB'] == "") {
$errors[] = "Please enter your birthday in the format dd/mm/yyy";
}
if ($_POST['email'] == "") {
$errors[] = "Please enter the user's email";
}
if ($_POST['username'] == "") {
$errors[] = "Please enter a username";
}
if ($_POST['password'] == "") {
$errors[] = "Please enter a password";
}

// check if we added any errors to the array
if ( count($errors) > ) {

// we have errors so we need to echo them back to the user
// start a list to hold them
echo '<ul>';
// echo out each error in the array
foreach ($errors as $error) {
echo '<li class="error">'.$error.'</li>';
}
// close our ul
echo '</ul>';

} else {

// No errors... process the data

$first_name mysql_real_escape_string($_POST['first_name']);
$last_name mysql_real_escape_string($_POST['last_name']);
$DOB mysql_real_escape_string($_POST['DOB']);
$sex mysql_real_escape_string($_POST['sex']);
$email mysql_real_escape_string($_POST['email']);
$username mysql_real_escape_string($_POST['username']);
$password mysql_real_escape_string($_POST['password']); 
$agree mysql_real_escape_string($_POST['agreed']);
$creation_date mysql_real_escape_string($_POST['creation_date']);
$user_type mysql_real_escape_string($_POST['member_type']);
$access_level mysql_real_escape_string($_POST['access_level']);
$validation mysql_real_escape_string($_POST['validation_id']);
$club_user mysql_real_escape_string($_POST['user_type']);
  
$insert_member"INSERT INTO Members (`first_name`,`last_name`,`DOB`,`sex`,`email`,`username`,`password`,`agree`,`creation_date`,`usertype`,`access_level`,`validationID`) 
VALUES 
('"
.$first_name."','".$last_name."','".$DOB."','".$sex."','".$email."','".$username."','".$password."','".$agree."','".$creation_date."','".$user_type."','".$access_level."', '".$validation."')";

$insert_member_nowmysql_query($insert_member) or die(mysql_error());

}
}


Also you can add multiple arguments to an if statement by using either || or && (or, and)

Code: [Select]
If ( ($a == $b) || ($b ==$c) ) { then do this }
If ( ($a == $b) && ($b ==$c) ) { then do this }

Hope this all made sense... good luck!