在同一个字段数据库php中存储不同的值

时间:2014-09-15 02:28:53

标签: php mysql sql

我有这两张桌子,

考试表

ID, title, creator 

exam_settings表

id //(simply for managing rows and edits/updates)
"1", "2", "3" // etc

quiz_id
"55912" // or so on

type
"question", "answer"

ref_number
"1", "2" // This is basically Question 1, Question 2, Answer 1 etc.

value
"What is ...?" or "21" // The question or the answer

现在我在表格中存储问题和答案时遇到问题,我使用了这个html数组问题 <input type="text" name="quiztxtBox[]" > and <input type="text" name="answer[]" >
得到输入。但我想知道如何使用这2个数组执行sql并将它们存储在同一个字段中。这是我的SQL的样子。

$quiztxtBox = $_POST["quiztxtBox"]; //array
$answer = $_POST["answer"]; //array

mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("elearning") or die(mysql_error()) ;

mysql_query("INSERT INTO exam_list (exam_name, creator) VALUES ('$files', 'admin')");

$lastid = mysql_query("SELECT ID FROM exam_list ORDER BY ID DESC LIMIT 0 , 1");
$id = implode($lastid);

mysql_query("INSERT INTO exam_setting (exam_id, type, ref_number, value) VALUES ('$id', 'othervalues')");

因此,我首先将考试名称和创建者存储在考试问题和答案之后,这就是我失去的部分。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以通过这种方式将输入文本的值读取为数组。

<form name="" action="array.php" method="post">
<input type="text"  name="quiztxtBox[]" >
<input type="text"  name="answer[]" >
</form>

在array.php文件中

//get the array
$quiztxtBox=$_POST['quiztxtBox'];
$answer=$_POST['answer'];
//loop through the array
foreach($quiztxtBox as $key=>$quiztxtBox){

 echo 'Question'.$quiztxtBox.'Answer'.$answer[$key];
 //for storing in the same field.
 //perform your sql query with this
$finalstring=$quiztxtBox.','.$answer[$key];


}