通过PHP动态创建Mysql表?

时间:2013-01-09 12:45:25

标签: php jquery mysql

这是Siva,我在这里讨论动态创建Mysql表的字段名称为The HTML。

您可以在此处查看示例HTML代码....

    <div id='container'>
            <h1 id="form-name" style="background-color: rgb(255, 255, 255); box-shadow: none; border: none; margin: 8px 15px;">New Form</h1>
            <form action="saveData.php" method="post" id="preview_form" novalidate="novalidate">
        <div class="row" style="display: block;">
    <label class="field" for="how_to_use_it?">How To Use It?</label><span class="radioButton" data="" id="how_to_use_it?">
    <label class="option" for="how_to_use_it?_option_1">
    <input class="radio" type="radio" name="how_to_use_it?" id="dialog_box_how_to_use_it?_option_1" value="Option 1" data="{&quot;validate&quot;:{&quot;required&quot;:false,&quot;messages&quot;:{}}}">Option 1</label><label class="option" for="how_to_use_it?_option_2">
    <input class="radio" type="radio" name="how_to_use_it?" id="how_to_use_it?_option_2" value="Option 2">Option 2</label>
<label class="option" for="how_to_use_it?_option_3"><input class="radio" type="radio" name="how_to_use_it?" id="how_to_use_it?_option_3" value="Option 3">Option 3</label></span></div>
<input type="submit" class="button blue" value="Submit" id="submit-form"><input type='hidden' id='tname' name='tname' value='surveyForm_6' />
</form>
</div> <!--container-->

这是我需要的输出表:

id(pk) how_to_use_it?
1      Option1

这里how_to_use_it?这将是该字段的名称,它可能是一个文本框,广播,复选框等,以及该元素的名称,它可能或可能没有特殊的charectors .....

这就是我所尝试的:

<?php 
require_once"../session.php";
$selFname =$dbHandle->execQuery("SELECT file_name,max(survey_form_id) as id FROM master_survey_forms WHERE survey_form_id=(SELECT max(survey_form_id) FROM master_survey_forms)");
$fetFname =$dbHandle->fetchObjectQuery($selFname);
$last_saved_on =date("Y-m-d H:i:s");
if($fetFname->file_name != '')
{
    $tmpName =explode("_",$fetFname->file_name);
    $tname =$tmpName[0].'_'.($fetFname->id);
}
else
{
    $tname ='surveyForm_1';
}
if (isset($_POST)) {
    $values="'$fetFname->id' ,'$userId','$last_saved_on',";

    foreach ($_POST as $key => $val) {
        $sqlin .= " ".strtolower($key)." VARCHAR(255) , ";
        if($key != 'tname')
        {
            $fldNamein .=strtolower($key).',';
        }
        $tmpVals="";
        if(is_array($val)){

            if(!empty($val) && $key != 'tname') {
                foreach($val as $vls) {
                    if($key != 'tname')
                    {
                        $tmpVals .=$vls.',';
                    }
                }
                $tmpVals =substr($tmpVals,0,strlen($tmpVals)-1);
            }
            else
            {
                 $tmpVals ="";
            }
        }
        else
        {
            if($key != 'tname')
            {
                $tmpVals =$val;
            }
        }
        if($key != 'tname')
        {
            $values .=" '$tmpVals'".' ,';
        }
        $tsname =$_POST['tname'];
    }
    if($tsname != '')
    {
        $tname =$tsname;
    }
    else
    {
        $tname =$tname;
    }
    echo $sqlin.'sdf';
    $fldName='INSERT INTO '.$tname.'(survey_form_id ,submitted_by,submitted_on, '.$fldNamein;
    $sql = 'CREATE TABLE IF NOT EXISTS ';
    $sql .= $tname.'('.$tname.'_id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY('.$tname.'_id), survey_form_id  varchar(255) ,submitted_by  varchar(15) ,submitted_on  timestamp , '.$sqlin;
    $sql = preg_replace('/, $/', '', $sql);
    $sql .= ')';
    echo $sql;
    if($tsname == '')
    {
        $dbHandle->execQuery($sql);
    }
    else
    {
        $insVal =substr($fldName,0,strlen($fldName)-1).') VALUES ('.substr($values,0,strlen($values)-1).' )';
        $dbHandle->execQuery($insVal);
    }
    //header("Location:index.php");
}
?>

1 个答案:

答案 0 :(得分:3)

您执行此操作的方法是生成CREATE TABLE查询,基于迭代$_POST中的键。

请注意,如果您这样做,则需要转义列名称(因为您有空格,特殊字符等)。

MySQL转义字符为`

编辑:根据您的代码,我猜这是您的问题,如上所述:

$sqlin .= " ".strtolower($key)." VARCHAR(255) , ";
// ..
$fldNamein .=strtolower($key).',';

简单地逃避您的字段名称:

$sqlin .= " `".strtolower($key)."` VARCHAR(255) , ";
// ..
$fldNamein .= '`' . strtolower($key) . '`,';

(如果这不起作用,请提供$sql的值以及MySQL_error()的任何结果