-3

Error!

The following error(s) occurred: Notice: Undefined variable: errors in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\scripts\ladies_paradise\checkout.php on line 75

Please try again. ` Untitled Document

<body>
<?php

$page_title='ORDER FORM';

if(isset($_POST['submitted'])){
   $errors= array();
   $price=($_POST['price']);;
   $quantity=($_POST['quantity']);
   $total = $quantity * $price;
   $total = number_format ($total, 2);

// Validate the name and combat Magic Quotes, if necessary.
if (empty ($_POST['product'])) { 
   $errors[]='You forgot to enter product.';
}else{
    $pr=trim($_POST['product']);
}

 // Validate the price.
if (empty ($_POST['price'])) { 
     $errors[] ='You forgot to enter price.';
} else {
     $p=trim($_POST['price']);
} 

 // Validate the quantity.
if (empty ($_POST['quantity'])) { 
     $errors[] ='You forgot to enter quantity.';
} else {
     $q=trim($_POST['quantity']);
} 





if(empty($errors)){
require_once('checkout_connect.php');    

     $query="INSERT INTO customers(product,price,quantity)VALUES('$pr','$p','$q')";
     $result=@mysql_query($query);//Run the query.
     if($result){

       echo 'You are purchasing <b>', $c. '</b>. 
          Total price is <b>$', $total, '</b>.';
} else { // One from element was not filled out properly.
     echo  '<p><font color="orange">Please go back and fill out the form again.</font></p>';
}

 exit();

     }else{ //If it did not run OK.
       echo '<h1 id="mainhead">System Error</h1>
       <p class="error">You could not registered due to system error.We apologize for any inconvenience.</p>';//Public message.
       echo '<p>'.mysql_error().'<br/><br/>'.$query.'</p>'; //Debugging message.


        exit();
     }

     mysql_close(); //Close the database connection.

  }else{ //Report the errors.

    echo '<h1 id="mainhead">Error!</h1>
    <p class="error">The following error(s) occurred:<br />';
    if (is_array($errors)) {
    foreach ($errors as $msg){ //Print each error.
       echo " - $msg<br />\n";
    }
    }
    echo '</p><p>Please try again.</p><p><br /></p>';
     }//End of if (empty($errors)) IF.
 ?>

<h2>ORDER FORM:</h2>
<form action="checkout.php" method="post">
   <p>Product: <input type="number" name="code_item" size="15" maxlength="15" value"<?php if (isset($_POST['product'])) echo $_POST['product']; ?>" /></p>
   <p>Price: <input type="number" name="price" size="15" maxlength="30" value"<?php if (isset($_POST['price'])) echo    $_POST['price']; ?>" /></p>
   <p>Quantity: <input type="number" name="quantity" size="30" maxlength="50" value"<?php if (isset($_POST['quantity'])) echo $_POST['quantity']; ?>" /></p>
 <p><input type="submit" name="submit" value="Submit" /></p>
   <input type="hidden" name="submitted" value="TRUE" />

</form>
</body>
</html>``

1 Answers1

0

Define it at the top of the document.

<?php

    $errors = false;

Alternatively, check if it is set using a ternary where assignment is necessary.

echo isset($errors) ? $errors: '';

Where '' can be replaced with whatever assignment correlates to the requirements.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • `s/ternary/conditional operator/`. And does `$errors;` on its own really function as a declaration? – Lightness Races in Orbit Dec 08 '13 at 02:40
  • @LightnessRacesinOrbit In any version of PHP above 4, it does function as it's down declaration *edit* AFAIK. – Ohgodwhy Dec 08 '13 at 02:40
  • @Ohgodwhy: [Nope, doesn't seem so](http://ideone.com/i6Gxpf), at least not in PHP 5.4.4. – Lightness Races in Orbit Dec 08 '13 at 02:43
  • @user3078998 That's because it's not an array. Your initial conditional statement fails due to the fact that the value `$_POST['submitted']` is not set. Due to this fact, errors are never turned to an array. Debug the variable, `echo '
    ', print_r($errors), '
    ';`.
    – Ohgodwhy Dec 08 '13 at 02:44
  • @LightnessRacesinOrbit Fair enough, could've sworn empty declarations work, oh well, I'll just remove it. – Ohgodwhy Dec 08 '13 at 02:45
  • It's an empty-ish statement but there's no declaration there whatsoever. – Lightness Races in Orbit Dec 08 '13 at 02:45
  • Error! The following error(s) occurred: Please try again. }else{ //Report the errors. echo '

    Error!

    The following error(s) occurred:
    '; if (is_array($errors)) { foreach ($errors as $msg){ //Print each error. echo " - $msg
    \n"; } } echo '

    Please try again.


    '; }//End of if (empty($errors)) IF. ?>
    – Newbie PHP Dec 08 '13 at 02:46
  • thank you for the help Ohgodwhy and Lightness Races in Orbit. – Newbie PHP Dec 08 '13 at 03:03
  • the line 75 has gone. but now it appears line 63 : echo '

    '.mysql_error().'

    '.$query.'

    '; this is my coding : echo '

    System Error

    You could not registered due to system error.We apologize for any inconvenience.

    '; echo '

    '.mysql_error().'

    '.$query.'

    ';
    – Newbie PHP Dec 08 '13 at 03:11