title: PHP-Form validation and insertion using MySql
body: <p>I'm using this code to validate my my html form and I now need to add the form data into a table in mysql. How do I proceed I know the basics of creating a connection and sql databases but since I've already used the form's submit button i don't know how to get the data to a place where I can insert it again</p>
  
  <pre><code>&lt;?php
  // define variables and initialize with empty values
  $nameErr = $passErr = $emailErr =$cpassErr="";
  $name = $pass = $cpass = $email = "";
  
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["username"])) {
          $nameErr = "Enter Username";
      }
      else {
          $name = $_POST["username"];
      }
  
      if (empty($_POST["password"])) {
          $passErr = "Enter password";
      }
      else {
          $pass = $_POST["password"];
      }
         if (empty($_POST["cpassword"])) {
          $cpassErr = "Retype password";
      }
      else {
          $cpass= $_POST["cpassword"];
      }
  
      if (empty($_POST["email"]))  {
          $emailErr = "Enter email";
      }
      else {
          $email = $_POST["email"];
      }
  
  
  }
  ?&gt;
  
  &lt;html&gt;
      &lt;head&gt;
      &lt;style&gt;
       .error {
      color: #FF0000;
          } 
      &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
     &lt;form method="POST" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt;
      &lt;table border="0" cellspacing="20"&gt;
        &lt;tbody&gt;
              &lt;tr&gt;
                  &lt;td&gt;Username:&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="username" accept="" value="&lt;?php echo htmlspecialchars($name);?&gt;"&gt;
                      &lt;span class="error"&gt;&lt;?php echo $nameErr;?&gt;&lt;/span&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Password:&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="password" accept="" value="&lt;?php echo htmlspecialchars($pass);?&gt;"&gt;
                      &lt;span class="error"&gt;&lt;?php echo $passErr;?&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Confirm Password:&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="cpassword" accept=""value="&lt;?php echo htmlspecialchars($cpass);?&gt;"&gt;
                      &lt;span class="error"&gt;&lt;?php echo $cpassErr;?&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Email:&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="email" accept="" value="&lt;?php echo htmlspecialchars($email);?&gt;"&gt;
                      &lt;span class="error"&gt;&lt;?php echo $emailErr;?&gt;&lt;/span&gt;&lt;/td&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/tbody&gt;
      &lt;/table&gt;
         &lt;input type="submit" name="submit" value="Submit"&gt;
     &lt;/form&gt;
      &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Code for the connection</p>
  
  <pre><code>&lt;?php
  $host="localhost";
    $username="root";
    $password="root";
    $db_name="LSDB";
  
      $con=mysqli_connect("$host","$username","$password","$db_name");
  // Check connection
  if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    var_dump($_POST);
  
    $u=$_POST['username'];
    $p=$_POST['password'];
    $e=$_POST['email'];
    $ph=$_POST['phone'];
  
    $sql="INSERT INTO register (username,password,email,phone)
       VALUES 
       ('$u','$p','$e','$ph')";
  
    if (!mysqli_query($con,$sql))
    {
    die('Error: ' . mysqli_error($con));
    }
  mysqli_close($con);
  ?&gt;
  </code></pre>