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

Step 1) Create a folder in htdocs give the name as a table. open the folder and create a new file save it as db.php and copy the code to 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 the URL. Click on Create new database as give name table to the database and check the connection is successfully established or not.
After checking the connection Create a new file give name as table.php and copy the code of table.php and run the table.php file and pass a name to form which table you want to create and click on submit button after submit button see in PHPMyAdmin the table is created successfully


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>Create Automatic Table in php mysqli by Yourphpguru</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>

If Anyone Need Help or Some Error Occur Join this Group By Clicking on Link.
I Will Help You.

Whatsapp Group:- chat.whatsapp.com

Thank You

PHP-GURU

Leave a Reply

Your email address will not be published. Required fields are marked *