所以我知道这是一个流行的问题,并且已经有很多答案。然而,我的挑战略有不同,这已经困扰了我好几天了。我有一个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="answerswerTable" 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="answerswerText"></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) ;
}
?>
所以在我的数据库中,我有以下结构:
表单表
-form_id(PK, AI) -tile -date
问题表
-question_id(PK, AI) -description -form_id(FK) references form_id in form table
答案表
-answer_id(PK, AI) -answer_text -question_id(FK) references question_id from questions
小部件(存储从表单(单选、文本区域等)中选择的不同小部件/输入类型。
-widget_id(PK,AI) -widget_type -question_id(FK) references question_id from questions
每个问题可以有多个答案,但只能有一个小部件类型。每个答案只能有一个问题,小部件也可以。因此,单个question_id可能会在答案表中多次出现。这就是我卡住的地方:
- 正确创建表单以实现此目的。这些问题和所有可能的答案都是动态的。因此,您可以决定随时添加问题和答案输入字段。
- 我可以动态添加更多问题和更多答案,但似乎无法弄清楚如何将每个答案链接到它的问题。
- 我在编写正确的查询时也遇到了挑战,该查询会将question_id插入问题表以及引用它的所有相关外键中。
真的很感激一些指示,这样我就可以朝着正确的方向前进。真的很抱歉这个长问题。谢谢
使用 mysqli_insert_id()
为您的插入查询生成 PK
参考手册 : http://us3.php.net/mysqli_insert_id
last_insert_id()
不是推荐的方法,因为它在同时插入的情况下无法提供正确的 ID。
有好消息,也有坏消息。
坏消息是,您无法以 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 . ")"
问题是最好一次插入问题的所有答案,然后继续下一个问题。
如果他们要向现有答案添加额外的答案,则需要从某个地方获取question_id,以便上述脚本起作用。