www.webdeveloper.com
Results 1 to 2 of 2

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

Hybrid View

  1. #1
    Join Date
    Apr 2014
    Posts
    1

    How to create table at mysql through form html with php

    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 ?!
    PHP Code:
    <?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>

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    You're using a file connect.php and sending some form data to insert.php, however, you haven't shown the code for either file. Since your JavaScript is using fields from both forms, I'm not sure whether you want to write data from both forms or just one. If you want to write it from both, it would be much easier to use only 1 <form>.

    Just a couple issues with your HTML first:

    - Any HTML input elements you wish to read data from will need to have a name attribute.
    - You do not need (and shouldn't for this) need a name attribute if the input type is submit or reset.
    - I'm guessing you want placeholder attributes instead of the title attributes in your input elements.
    - You have <label for = "fieldtype" >Field Type</label>, however, there is no other HTML element with a matching ID. I'm guessing you wanted it to be for your <select>, otherwise that label is functionally useless.

    Once you get that sorted out, in your insert.php file (or whichever other file will capture the table data and write to your MySQL), you'll need to do a few things:

    1) Use mysqli or PDO functions (NOT BOTH) to interact with your MySQL database. Don't use mysql functions since they will be depreciated.
    2) Check whether each $_POST variable is set, for example:
    PHP Code:
    if(isset($_POST['tablename'])) {
         
    // do something, possibly verification on the input
    } else {
        
    // data was not entered, do something

    3) If data verification was not yet performed, you need to do it.
    4) Write data to your MySQL table, preferably through prepared statements using mysqli or PDO functions.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles