title: how to create mysql table using html form from php
body: <p>I am trying to create a signup page where if user inserts required details then by clicking on sign up button it should insert details in selected table and create a new table in database too my html code is:</p>
  
  <pre><code>  &lt;form method="POST" action="signuplink.php" class="form-inline justify-content-center"&gt;
  &lt;div class="col-md-6 justify-content-center form-group"&gt;
      &lt;label class="sr-only"&gt;first_name&lt;/label&gt;
      &lt;input type="text" name="first_name" class="form-control" placeholder="First Name"&gt;
    &lt;/div&gt;
  &lt;div class="col-md-6 justify-content-center form-group"&gt;
      &lt;label class="sr-only"&gt;last_name&lt;/label&gt;
      &lt;input type="text" name="last_name" class="form-control" placeholder="Last Name"&gt;
  &lt;/div&gt;
  &lt;div class="col-md-6 justify-content-center form-group"&gt;
      &lt;label class="sr-only"&gt;phonenumber&lt;/label&gt;
      &lt;input type="text" name="phonenumber" class="form-control" placeholder="Contact Number"&gt;
  &lt;/div&gt;
  &lt;div class="col-md-6 justify-content-center form-group"&gt;
      &lt;label class="sr-only"&gt;username&lt;/label&gt;
      &lt;input type="text" name="username" class="form-control" placeholder="Username"&gt;
  &lt;/div&gt;
  &lt;div class="col-md-6 justify-content-center form-group"&gt;
      &lt;label class="sr-only"&gt;email &lt;/label&gt;
      &lt;input type="text" name="email" class="form-control" placeholder="username@domain.com"&gt;
  &lt;/div&gt;
  &lt;div class="col-md-6 justify-content-center form-group"&gt;
      &lt;label class="sr-only"&gt;password &lt;/label&gt;
      &lt;input type="password" name="password" class="form-control" placeholder="select password"&gt;
  &lt;/div&gt;
  &lt;button type="submit" name="submit" class="btn btn-success "&gt;Signup&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>php code is as below where i redirected the form kindly help to create new table automatically when user fills up the form successfully :</p>
  
  <pre><code>&lt;?php
  
  if (isset($_POST['submit'])) {
  
      include 'connection.php';
  $x = mysqli_real_escape_string ($connect, $_POST['email']);
  $firstname= mysqli_real_escape_string ($connect, $_POST['first_name']);
  $lastname= mysqli_real_escape_string ($connect, $_POST['last_name']);
  $username= mysqli_real_escape_string ($connect, $_POST['username']);
  $phonenumber= mysqli_real_escape_string ($connect, $_POST['phonenumber']);
  $y= mysqli_real_escape_string ($connect, $_POST['password']);
  
  //Error handlers
  //Check for empty fields
  if (empty($x) || empty($firstname) || empty($lastname) || empty($username) || empty($phonenumber) || empty($y))
  {
      header("Location: signup.php?signup=empty");
      exit();
  }
      else
  {
  //Check if input characters are valid
  if (!preg_match("/^[a-zA-Z]*$/", $firstname) || !preg_match("/^[a-zA-Z]*$/", $lastname))
  {
      header("Location: signup.php?signup=invalid");
      exit();
  }
  else
  {
  //Check if email is valid
  if (!filter_var($x, FILTER_VALIDATE_EMAIL))
  {
  header("Location: signup.php?signup=email");
  exit();
  }
  else
  {
  $_SESSION['first_name']=$firstname;
  $sql = "SELECT * FROM users WHERE username='$username' or email='$x'";
  $result = mysqli_query($connect,$sql);
  $resultCheck = mysqli_num_rows($result);
  
  if ($resultCheck &gt; 0) {
  header("Location: signup.php?signup=usertaken");
  exit();
  } else {
  //Hashing the password
  $hashedPwd = password_hash($y, PASSWORD_DEFAULT);
  //Insert the user into the database
  }
  $sql = "INSERT INTO users (ID, username, phonenumber, email,first_name, last_name, password) VALUES ('','$username', '$phonenumber','$x','$firstname','$lastname','$hashedPwd');";
  mysqli_query($connect, $sql);
  header("Location: signup.php?signup=successfully_logged_in");
  exit();
  }
  if($connect-&gt;query($sql)===TRUE)
  {
  $sql = "CREATE TABLE $username (
  ID int,
  LastName varchar(255),
  FirstName varchar(255),
  Address varchar(255),
  City varchar(255)
  )";
  if ($connect-&gt;query($sql) === TRUE) {
  echo "Table MyGuests created successfully";
  } else {
  echo "Error creating table: " . $connect-&gt;error;
  }
  }
  }
  }
  }
  
  
  else {
  header("Location: signup.php");
  exit();
  }
  ?&gt;
  </code></pre>