【发布时间】: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) ;
}
?>
所以在我的数据库中,我有以下结构:
-
表格
-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 插入问题表以及引用它的所有相关外键。
我真的很感激一些指示,这样我就可以朝着正确的方向前进。真的很抱歉这个冗长的问题。谢谢
【问题讨论】:
标签: php mysql sql database forms