#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    66
    Rep Power
    8

    Error messgae handling not working


    Hi everyone,
    I'm having a problem getting my error messages to work, they are not showing the error messages when the field is empty. Any help would be great thanks.

    PHP Code:
    <?php
    $error_message
    ="";
    require(
    "common.php");
    if(empty(
    $_SESSION['user']))
    {
    header("Location: login.php");
    die(
    "Redirecting to login.php");
    }
    if(!empty(
    $_POST))
    {
    //ERROR MESSAGE NOT WORKING
    if(!empty($_POST['firstname']))
    {
    $error_message="Please enter message.";
    }
    else
    {
    if(!empty(
    $_POST['lastname']))
    {
    $error_message="Please enter message.";
    }
    else
    {

    //////////////////////

    $query_params = array(
    ':firstname' => $_POST['firstname'],
    ':lastname' => $_POST['lastname'],
    ':address' => $_POST['address'],
    ':city' => $_POST['city'],
    ':zipcode' => $_POST['zipcode'],
    ':country' => $_POST['country'],
    ':user_id' => $_SESSION['user']['id'],
    );
    $query "
    UPDATE users
    SET
    firstname = :firstname,
    lastname = :lastname,
    address = :address,
    city = :city,
    zipcode = :zipcode,
    country = :country
    WHERE
    id = :user_id
    "
    ;
    try
    {
    $stmt $db->prepare($query);
    $result $stmt->execute($query_params);
    }
     catch(
    PDOException $ex)
     {
     die(
    "Failed to run query: " $ex->getMessage());
    }
       
    $_SESSION['user']['firstname'] = $_POST['firstname'];
       
    $_SESSION['user']['lastname'] = $_POST['lastname'];
       
    $_SESSION['user']['address'] = $_POST['address'];
       
    $_SESSION['user']['city'] = $_POST['city'];
       
    $_SESSION['user']['zipcode'] = $_POST['zipcode'];
       
    $_SESSION['user']['country'] = $_POST['country'];
      
    header("Location: p.php");
      die(
    "Redirecting to p.php");
     }
    }
    ?>
    <form action="p.php" method="post">

    <?php echo htmlentities($error_message);?>
    <input type="text" name="firstname" value="<?php echo htmlentities($_SESSION['user']['firstname'], ENT_QUOTES'UTF-8'); ?>" />
    <input type="text" name="lastname" value="<?php echo htmlentities($_SESSION['user']['lastname'], ENT_QUOTES'UTF-8'); ?>" />
    <input type="text" name="address" value="<?php echo htmlentities($_SESSION['user']['address'], ENT_QUOTES'UTF-8'); ?>" />
    <input type="text" name="city" value="<?php echo htmlentities($_SESSION['user']['city'], ENT_QUOTES'UTF-8'); ?>" />
    <input type="text" name="zipcode" value="<?php echo htmlentities($_SESSION['user']['zipcode'], ENT_QUOTES'UTF-8'); ?>" />
    <input type="text" name="country" value="<?php echo htmlentities($_SESSION['user']['country'], ENT_QUOTES'UTF-8'); ?>" />
    <input type="submit" value="Update Account" />
    </form>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,971
    Rep Power
    375
    one hint "echo"
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    66
    Rep Power
    8
    Hi,
    thanks for the reply i know about the echo but im trying to get the custom error messaging code i have in there to work.

    PHP Code:
    $error_message="";
    //ERROR MESSAGE NOT WORKING
    if(!empty($_POST['firstname']))
    {
    echo "Please enter message.";
    }
    else
    {
    if(!empty($_POST['lastname']))
    {
    $error_message="Please enter message.";
    }
    else
    {

    //////////////////////

    <?php echo htmlentities($error_message);?>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,383
    Rep Power
    594
    What is not working?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    66
    Rep Power
    8
    Originally Posted by gw1500se
    What is not working?
    when i leave a text field blank its not showing the error message, and it will also add the blank text field to the database thanks.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    shouldn't the !empty() be an empty()?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    66
    Rep Power
    8
    Originally Posted by Jacques1
    Hi,

    shouldn't the !empty() be an empty()?
    That fixed my problem thanks a lot, and thanks everyone for the help.

IMN logo majestic logo threadwatch logo seochat tools logo