【问题标题】:single html form insert into multiple mysql tables with one to many relationship单个html表单插入到具有一对多关系的多个mysql表中
【发布时间】:2013-12-01 07:33:23
【问题描述】:

所以我知道这是一个受欢迎的问题,并且已经有很多回复。但是,我的挑战略有不同,这几天一直困扰着我。我有一个 html 表单,用于将数据存储到多个我的数据库中的表。(现在使用 phpMyAdmin)。有点不同的是,这种形式允许用户创建调查问题,以及该问题的所有可能答案以及每个问题的首选输入类型并将它们存储在mysql db.让我给你看我的html代码:

 <!DOCTYPE html>


<html>
    <head>
        <title> Create a new Survey </title>
        <script type="text/javascript" src="script.js"></script>
    </head>
        <body style= "background: #D1D0CE">

        <h1 style= "text-align: center; font-family: Garamond; color: brown">      Create a new survey</h1><br />

            <p style="text-align:center"><img src = "logo.png"/></p>               

            <form action="insert.php"  method="post">

            <fieldset class="row1">

            <table id="formTable" class="form" >
                <tbody>
                <p style="font-family:Garamond; color:brown">
                <strong>Form Information</strong>
                </p> 
                <p>(Please enter your form details below.)</p>

                <tr>
                    <td> Form Name  :  </td><br /><br />
                    <td> <input type="text" size="12" maxlength="40"  name="formName"/></td>

                </tr>
                <tr>
                    <td>Date : </td><br />
                    <td> <input type="date" size="12" maxlength="40"  name="formDate"/></td>
                </tr>
                </tbody>

            </table>
            <fieldset class="row2">
            <table id="questionTable" class="form" >

                <p style= "font-family: Garamond; color: brown">
                <strong>Question Details</strong>
                </p> 

                <p> 
                 <input type="button" value="Add Question" onClick="addRow('questionTable')" /> 
                <input type="button" value="Remove Question" onClick="deleteRow('questionTable')" /> 
                </p>

                    <tr>
                        <p>
                        <td><input type="checkbox" name="chk[]"/></td>
                        <td>
                            <label>Question : </label>
                            <input type="text" size = "16" maxlength= "50"  name="description"/>
                        </td>

                        <td>
                            <label>Input Type :</label>
                            <select id="widgetType" name="widgetType" required="required">
                                <option>....</option>
                                <option>radio button</option>
                                <option>check box</option>
                                <option>Edit Text</option>
                                <option>Spinner</option>
                                <option>Rating bar</option>

                            </select>
                        </td>
                        </p>        
                    </tr>       
            </table>

            <fieldset class="row3">
                <table id="answerTable" class="form">

                    <p style= "font-family: Garamond; color: brown">
                <strong>Answer Details</strong>
                    </p> 

                    <p> 
                 <input type="button" value="Add Answer" onClick="addAnswer('answerTable')" /> 
                <input type="button" value="Remove Answer" onClick="deleteAnswer('answerTable')" /> 
                        <p>(Click to add more answer options.)</p>
                    </p>
                <tr>
                    <p>
                        <td> <input type="checkbox" name="chk[]"/></td>
                        <td><label>Answer : </label>  
                            <input type="text" size="12" maxlength="40"  name="answerText"></td>

                        <td>
                            <label>Match to Question :</label>
                            <select id="QuestionNumber" name="question" required="required">
                                <option>....</option>
                                <option>Question 1</option>
                                <option>Question 2</option>
                                <option>Question 3</option>
                                <option>Question 4</option>
                                <option>Question 5</option>
                                <option>Question 6</option>
                                <option>Question 7</option>
                                <option>Question 8</option>
                                <option>Question 9</option>
                                <option>Question 10</option>

                            </select>


                        </td>   
                        </p>
                    </tr>

                </table>



            <fieldset class="row4">
            <input type="submit" name="submit" value="Submit">
            </form>

        </body>



</html>   

还有php部分:

 <?php

    if(isset($_POST['submit'])){

        $host = "localhost"; // host of MySQL server
        $user = "root"; // MySQL user
        $pwd = ""; // MySQL user's password
        $db  = "webservice";  // database name

        // Create connection
        $con = mysqli_connect($host, $user, $pwd , $db);

        // Check connection
        if(mysqli_connect_errno($con)) {
            die("Failed to connect to MySQL: " . mysqli_connect_error());
        }

        $sql = "INSERT INTO form (title,date) VALUES ('".$_POST['formName']."','".$_POST['formDate']."')";


        mysqli_query($con , $sql);





        mysqli_close($con) ;
    }   
?> 

所以在我的数据库中,我有以下结构:

  1. 表格

    -form_id(PK, AI)
    
    -tile
    
    -date
    
  2. 问题表

    -question_id(PK, AI)
    
    -description
    
    -form_id(FK) references form_id in form table
    
  3. 答案表

    -answer_id(PK, AI)
    
    -answer_text
    
    -question_id(FK) references question_id from questions
    
  4. 小部件(存储从表单(收音机、文本区域等)中选择的不同小部件/输入类型。)

    -widget_id(PK,AI)
    
    -widget_type
    
    -question_id(FK) references question_id from questions
    

    每个问题可以有多个答案,但只有一种小部件类型。每个答案只能有一个问题,小部件也可以。因此,一个 question_id 可能会在答案表中出现多次。这就是我卡住的地方:

    • 正确创建表单以实现此目的。问题和所有可能的答案都是动态的。因此您可以决定随时添加问题和输入字段以获取答案。
    • 我可以动态添加更多问题和更多答案,但似乎无法弄清楚如何将每个答案与其问题相关联。
    • 我在编写正确的查询时也遇到了挑战,该查询会将 question_id 插入问题表以及引用它的所有相关外键。

    我真的很感激一些指示,这样我就可以朝着正确的方向前进。真的很抱歉这个冗长的问题。谢谢

【问题讨论】:

    标签: php mysql sql database forms


    【解决方案1】:

    有好消息也有坏消息。

    坏消息是,你不能在 100% sql 中插入多个表。

    好消息是,您可以分几步完成。

    插入核心记录后,可以再做一次sql查询

    $getid = mysql_query("SELECT last_insert_id()");
    $getid = mysql_fetch_array($getid);
    $id = $getid[0];
    

    然后您可以将此结果的返回值用于所有将来的插入,例如:

    "INSERT into answers (answer_text, question_id) values ('Some text', " . $id . ")"
    

    问题是最好一次插入问题的所有答案,然后继续下一个问题。

    如果他们要为现有的 from 添加额外的答案,您将需要从某个地方获取 question_id,以便上述脚本可以工作。

    【讨论】:

    • 嗨@Joel Small。非常感谢您的快速回复。您介意稍微解释一下您的代码 sn-p。只是这样我才能理解它的去向。从我能得到的一点点来看,就像我们将主键值存储在$getid 变量。然后我不明白为什么我们:在那里使用数组。如果 5 个答案都属于同一个队列,情况如何?我们如何在查询中处理它?
    • 不可否认,我的回答使用的是mysql函数而不是mysqli,所以实现可能会略有不同。我建议使用 @Mayank 的解决方案。
    • 接下来......我建议使用@Mayank 的解决方案。在我的代码中,mysql_fetch_array() 获取该 mysql 行的值数组,其中第一列位于索引 0。这一行中只有一个值。我将它分配给 $id 只是为了整洁。现在用更详细的解释更新我的答案
    • 那么我是否应该效仿你的例子,而不是 last_insert_id() 我只是用 mysqli_insert_id() 替换它?因为现在,这是我根据您和@Mayank 的建议所做的编辑: $sql = "INSERT INTO form (title,date) VALUES ('".$_POST['formName']."','" .$_POST['formDate']."')"; mysqli_query($con, $sql); $sql1 = "INSERT INTO questions (description, form_id) VALUES ('".$_POST['description']."', mysqli_insert_id())";这只插入到表单表中,我在问题表中一无所获。mysqli_insert_id()中的描述和form_id都没有
    • 正确,您仍然需要为所有后续表进行单独插入,但您可以这样做,因为您现在拥有核心表“表单”中的主键
    【解决方案2】:

    使用 mysqli_insert_id() 为您的插入查询生成 PK
    请参阅手册:http://us3.php.net/mysqli_insert_id

    last_insert_id() 不是推荐的方法,因为在同时插入的情况下它无法提供正确的 id....

    【讨论】:

    • 谢谢@Mayank 的那个指针。我还检查了其他线程,我明白你的意思是避免last_insert_id()。不过,一个简单的问题,mysqli_insert_id() 不会不一致它涉及一个有多个答案的问题?因为从您发布的链接中,它返回插入的最后一个 id 的值。那么如果下一个答案仍然引用前一个 id 怎么办?我该如何修改我的查询来满足这个要求?
    • mysqli_insert_id() 只会返回当前插入的 PK 而不会返回最后一个插入 id,因此当您为问题插入新答案时,您将获得插入的答案元组的 PK... ..要正确映射它,您必须根据您的架构将mysqli_query 中已回答的问题的PK 作为FK 传递......
    • 谢谢@Mayank。您的两个答案都非常有帮助。不幸的是,我无法标记其中任何一个,因为我的声誉至少 15
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 2011-03-14
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    相关资源
    最近更新 更多