如何在MySQL中运行时创建表并将其保存到数据库中

时间:2022-08-09 00:51:20

I created a form which creates table with entered number of rows and columns on form. I want to create that entered fields table into database and save its values into database Table. Guide me what I should use.

我创建了一个表单,用于在表单上创建输入行数和列数的表。我想将输入的字段表创建到数据库中,并将其值保存到数据库表中。指导我应该使用什么。

My code:

我的代码:

 <?php
    global $Host;
    global $Username;
    global $password;
    global $database;
    function getConnection()
    {
        $Host = "localhost";
        $Username = "root";
        $password = "";
        $database = "labdata";

        $oMysqli = new mysqli($Host,$Username,$password,$database);
        return($oMysqli);
    }
    ?>
    <html>
    <head>
    <title>aa</title>
    </head>
    <body>
        <form name="report Creation" method="post">
                            <label for='Table'>Define Table</label>

                            <label for='column'>Row</label>
                            <input type="text" name="column"></input>

                            <label for='rows'>Column</label>
                            <input type="text" name="rows"></input>

            <input type="submit" name="submit" value="submit">
    </form>
    </body>
    </html>     
    <?php
      function displayData($column,$rows)             
        {       
          echo "<table border='1' align='center'>";           
          for($i = 0;$i<$_POST['column'];$i++)       
            { 
                echo "<tr>".$i."</tr>";                              
                for($j = 0; $j <$_POST['rows'];$j++)         
                {
                 echo "<td>" ."<input type=\"text\" name='column_$i[$j]'>"."</td>"; 
                }
            }
            echo "</table>";
        echo "<form>";
        echo "<input type=\"submit\" name=\"ok\" value=\"ok\">";
        echo "</form>";
         //function displaydata($column = NULL) {if($id == NULL) Event::run('system.52');}
      }

    if(isset($_POST['submit']))displaydata();// Show data INSIDE form
    {
    $query = "CREATE TABLE Cars('$columns[$j]')";
    $oMysqli = getConnection();
    $oMysqli->query($query);
    $Insert = "INSERT INTO Cars(Id,column[$j]) VALUES($Id,$column)";
    $oMysqli = getConnection();
    $oMysqli->query($Insert);

    if(isset($_POST['ok']))
     {
      $plength = count($_POST['column']);
      for($j=0;$j<$plength;$j++)
      {

        $b = $_POST['column'][$j];
        $pa = array('column' => $b['column']);
        foreach($pa as $l => $m)
        {
            $pa[$l] = mysql_real_escape_string($m);
        }
                            $columns = $pa['column'];
                            $columns = $_POST['column'][$j];                        

                        for($n=0;$n<$columns;$n++)
                        {
                                $x= $_POST['column'][$j];
                                $ab = array('column' => $c['column']);
                                foreach($t as $u => $n)
                                {
                                 $ab[$u] = mysql_real_escape_string($n);                             
                                }
                                $columns = $ab['column'];
                                $columns = $_POST['column'][$j];
                        }

        }
    }
        if($result = $mysqli->query($Insert))
            {
             $k=0;
             $column = explode(",",$_POST['columns']);
                        foreach($column as $c)
                    {
                      echo "<td><b>$c</b></td>\r\n";
                    }
                echo "</tr>";
            }
        }
    ?>

2 个解决方案

#1


2  

To answer you question: creating a table is executing a SQL statement against the server.

回答你的问题:创建一个表正在对服务器执行一条SQL语句。

You should rethink your strategy, creating a table based on user input is bad design.

你应该重新考虑你的策略,根据用户输入创建一个表是糟糕的设计。

#2


1  

Use the 'CREATE TABLE' syntax in your query.

在查询中使用“CREATE TABLE”语法。

All info about it: http://dev.mysql.com/doc/refman/5.1/en/create-table.html

所有关于它的信息:http://dev.mysql.com/doc/refman/5.1/en/create-table.html

#1


2  

To answer you question: creating a table is executing a SQL statement against the server.

回答你的问题:创建一个表正在对服务器执行一条SQL语句。

You should rethink your strategy, creating a table based on user input is bad design.

你应该重新考虑你的策略,根据用户输入创建一个表是糟糕的设计。

#2


1  

Use the 'CREATE TABLE' syntax in your query.

在查询中使用“CREATE TABLE”语法。

All info about it: http://dev.mysql.com/doc/refman/5.1/en/create-table.html

所有关于它的信息:http://dev.mysql.com/doc/refman/5.1/en/create-table.html