PHP: search bar and insert email : <p>I'm running tests on php and MySql for a single page wich contains an input search bar and a newsletter subscrtion. Both of them work, but not at the same time: if i have the search bar and subscription input and I try to get results from the search, i get the php message from the subscrition (email required...). If eliminate the subscrition form, the query search works...</p>

<p>The code: 
        </p>

<pre><code>    &lt;?php

        require('MGconfig.php');

        if (isset($_GET["game"]) &amp;&amp; $_GET["game"]!="") {

          $mysearch = htmlspecialchars($_GET["game"]);
        $result = $connection-&gt;query("select title, genre from games wher    title like '%" . $mysearch ."%' or genre like '%" . $mysearch ."%'");



         }
    ?&gt;
&lt;h2&gt;Search game&lt;/h2&gt;

            &lt;form&gt;
                &lt;input name="game" id="game" class="form-control" type="text" /&gt;
            &lt;/form&gt;

            &lt;table class="table table-hover"&gt;
                &lt;thead&gt;
                    &lt;tr&gt;
                        &lt;th&gt;Title&lt;/th&gt;
                        &lt;th&gt;Genre&lt;/th&gt;
                    &lt;/tr&gt;
                &lt;/thead&gt;
                &lt;?php
      //while($row = mysqli_fetch_assoc($result)){
      foreach ($result as $row) {
        echo "&lt;tr&gt;";
        echo "&lt;td&gt;".$row["title"]. "&lt;/td&gt;&lt;td&gt;".$row["genre"]."&lt;/td&gt;";
        echo "&lt;/tr&gt;";
      }

    ?&gt;
            &lt;/table&gt;


    &lt;/div&gt;

&lt;?php

$error="";
$sucessMessage="";
if (($_POST)){

    if( $_POST["email"]===""){
        $error = "An email is required!";
    }


    if ($error!=""){
        $error = '&lt;div class="error"&gt;'.$error.'&lt;/div&gt;';

    }
    else{

        require("MGconfig.php");


        $email = mysqli_real_escape_string($connection, $_POST["email"]);


        // check if user mail is already on DB
        $result=mysqli_query($connection, "select email from newsletter where email = '".$email."'");

        //if there is a result, inform user that email is already regis

        if (mysqli_num_rows($result)&gt;0){
            $error = '&lt;div class="error"&gt;Email already registred&lt;/div&gt;';
        }else{

        //check id for new registration
        $result=mysqli_query($connection, "select max(id) from newsletter");
        $row=mysqli_fetch_row($result);
        $id = $row[0]+1;

$insert ="insert into newsletter(id, email) values (".$id.",'".$email."')";

        //Execute insert on the DB

    mysqli_query($connection, $insert);

             // Check if there was an error executing the insert
        if (!$result){
            $error ='&lt;div class="error"&gt;Fail to register&lt;/div&gt;'.mysqli_error($connection);

        }else{
            $sucessMessage ='&lt;div class="error"&gt;Thank you for registring!&lt;/div&gt;';

        }
      }  
    }
}


?&gt;
&lt;div class="subscribe"&gt;

                &lt;form method="post"&gt;
                    &lt;label style="font-size:20px"&gt;Subscribe our Newsletter!&lt;/label&gt;
                    &lt;input type="email" id="email" name="email" class="newsletter" placeholder="       enter your email here                 "&gt;
                    &lt;div class="error"&gt;
                        &lt;?php echo $error;?&gt;
                    &lt;/div&gt;
                    &lt;div class="error"&gt;
                        &lt;?php echo $sucessMessage;?&gt;
                    &lt;/div&gt;
                &lt;/form&gt;
            &lt;/div&gt;
</code></pre>
