Create Automatic Table (Dynamically) in PHP using mysqli

How To Create Table Dynamically using Php Step By Step :-

Step 1)

Create folder in htdocs give name as a table. open folder and create a new file save as db.php and copy the code the db.php code.

db.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "table";

$conn = new mysqli($servername, $username, $password, $dbname);

if($conn){

}else{
    echo "Connection Failed";
}
?>

 

 Step 2)  

Open a browser type http://localhost/phpmyadmin/   in url.Click on Create new database as give name table to databaseand check connetion is successfully established or not.

 

Step 3) 

Create a new file give name as table.php and copy the code of 

table.php

 <?php
include "db.php";

if(isset($_POST['submit'])){

$table_name=mysqli_real_escape_string($conn,$_POST['table_name']);

$result = mysqli_query($conn,"SHOW TABLES LIKE '".$table_name."'");
if($result->num_rows == 1) {

        echo '<script language="javascript">';
        echo 'alert("Table exists, Please try again")';
        echo '</script>';
}
else {
  
    $table5 = "CREATE TABLE $table_name ( id INT(250) UNSIGNED AUTO_INCREMENT PRIMARY KEY,emp_name VARCHAR(200), salary VARCHAR(200),status tinyint(1) DEFAULT '1', date datetime)";
    $res5=mysqli_query($conn,$table5);

        echo '<script language="javascript">';
        echo 'alert("Table Successfully Created")';
        echo '</script>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>home page</title>
</head>
<body>

    <form action="" method="post" style="margin-top: 50px;margin-left: 70px;">
        <input type="text" name="table_name"><br/><br/>
        <input type="submit" name="submit" value="submit">
    </form>

</body>
</html>

   

Output Screens:-


create-table-automatically-php
Output Screen


create-table-automatically-php



create-table-automatically-php