Amir Mohammad Rivand Amir Mohammad Rivand - 3 months ago 7
MySQL Question

How to making new table in mysql using php when we have add more field button in form

I want to make a school management system in php. In my new lessons page I want to have an "add more field button" to allow users define more lessons. How can I achieve that?

How do I write php code to insert a new SQL line for each new field?

My code is :

<?php

/**
* @author AmirMohammad
* @copyright 2016
*/

include('../ss_engine/config.php');


if(isset($_POST['submit'])) {
$lessons[] = $_POST['lesson'];
$sql="CREATE TABLE IF NOT EXISTS darsha(
`id` INT NOT NULL AUTO_INCREMENT,
`sid` INT(6) NOT NULL,
`firstname` VARCHAR(255) NOT NULL,
`lastname` VARCHAR(255) NOT NULL,
`class` VARCHAR(255) NOT NULL,
$lessons FLOAT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci";
$result = $conn->query($sql);
if($result) {
echo 'success';
} else {
echo 'Error adding data'.$conn->error;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>تعریف دروس</title>
</head>

<body>

<form action="" method="POST">
<table style="border: 1px solid #454545;width: 100%;">
<tr>
<td>lesson 1</td>
<td><input type="text" name="lesson[]" /></td>
</tr>
<tr>
<td>lesson 2</td>
<td><input type="text" name="lesson[]" /></td>
</tr>
<tr>
<td>lesson 3</td>
<td><input type="text" name="lesson[]" /></td>
</tr>
<tr>
<td>lesson 4</td>
<td><input type="text" name="lesson[]" /></td>
</tr>
<tr>
<td>lesson 5</td>
<td><input type="text" name="lesson[]" /></td>
</tr>
<tr>
<td><input type="submit" value="submut" name="submit" /></td>
<td><button>add new field</button></td>
</tr>
</table>
</form>

</body>
</html>

Answer

I have added the javascript to dynamically add the lesson. and you i have changed php code also for dynamically adding the fields.

                   <?php

        include('../ss_engine/config.php');


        if(isset($_POST['submit'])) {
            $lessons[] = $_POST['lesson']; 
            $lesson_string;
            foreach($lessons as $lesson)
            {
              $lesson_string=$lesson.'FLOAT NOT NULL ,';
            }
            $sql='CREATE TABLE IF NOT EXISTS darsha(
                `id` INT NOT NULL AUTO_INCREMENT,
                `sid` INT(6) NOT NULL,
                `firstname` VARCHAR(255) NOT NULL,
                `lastname` VARCHAR(255) NOT NULL,
                `class` VARCHAR(255) NOT NULL,'.
                $lesson_string .'
                PRIMARY KEY ( `id` )
                ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci';        
            $result = $conn->query($sql);
            if($result) {
                echo 'success';
            } else {
    echo 'Error adding data'.$conn->error;
}
        }
        ?>
        <!DOCTYPE HTML>
        <html>
        <head>
        <title>تعریف دروس</title>
        </head>
        <script>
            $('#addNew').click(function() {
                var rowCount = $('#myTable tr').length;
           $('#myTable tbody').append('<tr class="child"><td>lesson '+rowCount + '</td><td>         <input type="text" name="lesson[]" /></td></tr>');
        });
        </script>
        <body>

        <form action="" method="POST">
        <table style="border: 1px solid #454545;width: 100%;" id="myTable">
        <tr>
        <td>lesson 1</td>
        <td><input type="text" name="lesson[]" /></td>
        </tr>
        <tr>
        <td>lesson 2</td>
        <td><input type="text" name="lesson[]" /></td>
        </tr>
        <tr>
        <td>lesson 3</td>
        <td><input type="text" name="lesson[]" /></td>
        </tr>
        <tr>
        <td>lesson 4</td>
        <td><input type="text" name="lesson[]" /></td>
        </tr>
        <tr>
        <td>lesson 5</td>
        <td><input type="text" name="lesson[]" /></td>
        </tr>
        <tr>
        <td><input type="submit" value="submut" name="submit" /></td>
        <td><button id="addNew">add new field</button></td>
        </tr>
        </table>
        </form>

        </body>
        </html>
Comments