As a beginner in PHP, I was trying to learn how PHP runs along with MySQL. So I was practicing how to insert data into a database using a form. So far this is what I did.

error_reporting(E_ALL ^ E_WARNING); 

define( "TITLE", "GET &amp; POST" );

if( isset( $_POST["post_submit"] ) ) {
    // build a function that validates data
    function validateFormData( $formData ) {
        $formData = trim( stripslashes( htmlspecialchars( $formData ) ) );
        return $formData;
    // check to see if inputs are empty
    // create variables with form data
    // wrap the data with our function
    if( !$_POST["post_name"] ) {
        $nameError = "Please enter your name <br>";
    } else {
        $name = validateFormData( $_POST["post_name"] );
    if( !$_POST["post_email"] ) {
        $emailError = "Please enter your email <br>";
    } else {
        $email = validateFormData( $_POST["post_email"] );


    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title><?php echo TITLE; ?></title>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>

    <div class="container">
        <h1><?php echo TITLE; ?></h1>
        <h4>Submitted via $_GET</h4>
        <form action="form_get.php" method="get">
            <input type="text" placeholder="Name" name="name">
            <input type="text" placeholder="Email" name="email">
            <input type="submit" name="form_submit">
        <h4>Submitted via $_POST</h4>
        <form action="form_post.php" method="post">
            <input type="text" placeholder="Name" name="post_name">
            <input type="text" placeholder="Email" name="post_email">
            <input type="submit" name="post_submit">


        <h4>Submitted to current page</h4>
        <p class="text-danger">* Required fields</p>
        <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
            <small class="text-danger">* <?php echo $nameError; ?></small>
            <input type="text" placeholder="Name" name="post_name"><br><br>
            <small class="text-danger">* <?php echo $emailError; ?></small>
            <input type="text" placeholder="Email" name="post_email"><br><br>
            <input type="submit" name="post_submit">
            if( isset($_POST["post_submit"]) ) {
                echo "<h4>Your info</h4>";
                echo "$name <br> $email <br>";

    <!-- jQuery -->
    <script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
    <!-- Bootstrap JS -->
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

When I wasn't using error_reporting(E_ALL ^ E_WARNING) it kept showing this warning message [ Warning: Undefined variable $nameError in C:\xampp\htdocs\mysql\insert.php on line 84 ].

Can anyone help me why it's showing that there is an undefined variable? Cause I think I defined this variable at the top portion of PHP code.

And is it a good practice to use the error_reporting() function to suppress the warning message from showing?

  • Programming questions are off-topic here and should be asked on Stack Overflow. Before you ask there you need to make your example minimal. You need to pare it down to just a small section of no more than 10 lines that demonstrates the problem. Mar 20, 2021 at 9:43
  • You can supress errors by prefixing the command with "@" - so maybe "@echo "$name <br> $email <br>"; would work generically. I expect though that defining $name before its used might be a better way = eg at the top of your code add a line like <?php $name="" ?>
    – davidgo
    Mar 20, 2021 at 21:03
  • @davidgo suppressing errors is a bad idea...there is an error, it needs to be sorted.
    – Steve
    Mar 20, 2021 at 21:29
  • @Steve Agreed. It can be useful as a short term hack to find the appropriate bit of code that is causing grief, and can be useful for a beginner because it can get some crap out the way to focus on the basic concepts - especially if PHP is the first language being programmed in. (I struggle with my sister - a high school programming teacher - about all the bad practices they run with - like no validation, no error checks etc - but her goals in teaching the basics are different to my expectations of what even a semi-competent programmer would do)...
    – davidgo
    Mar 20, 2021 at 22:18
  • Hence why I added "a better way" at the end of my post.
    – davidgo
    Mar 20, 2021 at 22:18

1 Answer 1


I can't recreate the warning on a test page I made using your code so I can't test the solution.

Note that what you are seeing is only a warning. Unlike a fatal error, it won't break your code, though you may have an unpredictable result for the value of $nameError

In your code, you have a line

if( !$_POST["post_name"] ) { $nameError = "Please enter your name <br>"; ...

But what if post_name is set? Then there is no value in $nameError and that is what is throwing the error when you echo $nameError;

There are a few different ways to solve this.

  1. add the line $nameError = ""; just before the if() statement - that declares it.
  2. In the else() do the same thing; $nameError = "";
  3. where you echo it, instead have <?php if( isset($nameError) ){echo $nameError;} ?> so it only will try to display if it is declared and not null

You would only use one of these options

You may have to do the same thing for $emailError

And it is OK to hide errors on a production site if you are also logging them, but during development you should definitely have them enabled otherwise you are going to tear your hair out wondering why something is behaving strangely...trust me on this :P

Not the answer you're looking for? Browse other questions tagged or ask your own question.