How to create table at mysql through form html with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
soo
Forum Newbie
Posts: 2
Joined: Wed Apr 02, 2014 8:17 pm

How to create table at mysql through form html with php

Post by soo »

I hope anybody help me,I want to create table to MySQL through form and when I'm enter name of table and name of field and his type in form and click to button add field,the form create table under it that have the name of table and field name and his type and pin the name of table and add another field and his type and click to button that add another field to table and so on ,after I finished entering all fields and their types press at another button to add the name of table and their fields to MySQL with php I used that code for create table under form but I don't know how to take data from table html and use php to create table to database ?!

Code: Select all

<?php require 'connect.php';?>
<html>
<head>
<title>Order</title>
<script type="text/javascript">


function updateForm() {

var tablename = document.getElementById("tablen").value;


document.getElementById("demo").innerHTML=tablename;


    var fieldname = document.getElementById("fieldn").value;

    var fieldtype = document.getElementById("fieldt").value;


    var table=document.getElementById("results");
    var row=table.insertRow(-1);
    var cell1=row.insertCell(0);
    var cell2=row.insertCell(1);


    cell1.innerHTML=fieldname;
    cell2.innerHTML=fieldtype;        



   }


  </script>
 </head>
 <body>
 <form name="order" method="post" id="frm1">
 <table>
    <tr>
        <td>
            <label for="tablename">Table Name </label>
        </td>
        <td>
            <input id="tablen" name="tablename" title="Please enter only alphabetic characters" type="text" size="28" />
        </td>
        <td></td>
    </tr>
    <tr>
        <td>
            <label for="fieldname">Field Name</label>
        </td>
        <td>
            <input id="fieldn" name="fieldname" title="Enter item quantity" width="196px" />
        </td>
    </tr>
    <tr>
        <td>
           <label for="fieldtype">Field Type</label>

        </td>
        <td>
            <select name="fieldtype" id="fieldt" required="required">

<option value="">Select</option>
<option value="int(11) NOT NULL AUTO_INCREMENT">int(11)</option>
<option value="decimal NOT NULL">decimal</option>
<option value="varchar(50) NOT NULL">varchar(50)</option>
<option value="text NOT NULL">text</option>
<option value="char NOT NULL">char</option>
<option value="longtext NOT NULL">longtext</option>
<option value="year NOT NULL">year</option>
<option value="date NOT NULL">date</option>
<option value="time NOT NULL">time</option>
<option value="binary NOT NULL">binary</option>
<option value="float NOT NULL">float</option>
</select>
        </td>

    </tr>
</table>
<input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset" />
<button type="button" onClick="updateForm();"/>Add To Table</button>
</form>
<br>



 <form  method="post" action="insert.php">
 <table id="results" border="2" width="360">
 <thead>
 <tr>
    <th scope="col" width="120">Table Name</th>
    <th scope="col" width="120">
 <p id="demo"></p>
 </th>
 </tr>
 <tr>

    <th scope="col" width="120" id="fieldname1">Field Name</th>
    <th scope="col" width="120" id="fieldtype1">Field Type</th>

 </tr>
 </thead>
 <input type="submit" name="submit" value="Save Mode"  />
 </table>
 </form>


 </body>
 </html>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to create table at mysql through form html with php

Post by Christopher »

You just need to create a query using the SQL command CREATE. Check the documentation for the exact syntax. And you need to connect to the database with a user that has create privileges.
(#10850)
soo
Forum Newbie
Posts: 2
Joined: Wed Apr 02, 2014 8:17 pm

Re: How to create table at mysql through form html with php

Post by soo »

yeah ,I know that I need to create a query using command CREATE but how I take the name of table , fields and their types from table html that stored them at it and use php and mysql query to execute that ?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to create table at mysql through form html with php

Post by Christopher »

Check the CREATE syntax: http://dev.mysql.com/doc/refman/5.1/en/ ... table.html

In PHP you would build the query like:

Code: Select all

$table = 'somename';
$sql = "CREATE TABLE $somename ( ...";
(#10850)
Post Reply