-1

这就是 Siva,我这里讨论的是动态创建 Mysql 表,字段名称为 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?将是字段的名称,它可能是文本框、收音机、复选框等,以及元素的名称以及它可能有也可能没有特殊字符的名称.....

这是我尝试过的:

<?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");
}
?>
4

1 回答 1

3

你这样做的方法是生成一个CREATE TABLE查询,基于迭代$_POST.

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

MySQL 转义字符是`.

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

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

只需转义您的字段名称:

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

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

于 2013-01-09T12:49:58.723 回答