我有一个表单,其中有八个输入字段。在表单内部,我将八个输入字段分成两组。每当我按下提交按钮时,我都会在SQL数据库中插入8个新行。
我如何制作它,以便每当我从表单向SQL数据库中插入四个新行时,会为每四行创建一个ID(qid)?例如,第一组四个输入字段的id为1,第二组的id为2,等等。
我的数据库
TID QID 援助(主键,自动增量) 回答 点
表格
<form action="process.php" method="post" class="copy" id="formid" name="form1" enctype="multipart/form-data">
Question 1
<br>
Answer 1<input type="text" name="answer1"/> <select name="point1"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select><br>
Answer 2<input type="text" name="answer2"/> <select name="point2"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select><br>
Answer 3<input type="text" name="answer3"/> <select name="point3"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select><br>
Answer 4<input type="text" name="answer4"/> <select name="point4"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select>
Question 2
<br>
Answer 1<input type="text" name="answer1"/> <select name="point1"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select><br>
Answer 2<input type="text" name="answer2"/> <select name="point2"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select><br>
Answer 3 3<input type="text" name="answer3"/> <select name="point3"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select><br>
Answer 4<input type="text" name="answer4"/> <select name="point4"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select><br>
</form>
Process.php
if (isset($_POST['submit'])){
$answer1=$_POST['answer1'];
$answer2=$_POST['answer2'];
$answer3=$_POST['answer3'];
$answer4=$_POST['answer4'];
$point1=$_POST['point1'];
$point2=$_POST['point2'];
$point3=$_POST['point3'];
$point4=$_POST['point4'];
$answer11=$_POST['answer11'];
$answer12=$_POST['answer12'];
$answer13=$_POST['answer13'];
$answer14=$_POST['answer14'];
$point11=$_POST['point11'];
$point12=$_POST['point12'];
$point13=$_POST['point13'];
$point14=$_POST['point14'];
$sql1= "INSERT INTO question (answer, point) VALUES ('$answer1', '$point1')";
$result=$connect->query($sql1);
$sql2= "INSERT INTO question (answer, point) VALUES ('$answer2', '$point2')";
$result=$connect->query($sql2);
$sql3= "INSERT INTO question (answer, point) VALUES ('$answer3', '$point3')";
$result=$connect->query($sql3);
$sql4= "INSERT INTO question (answer, point) VALUES ('$answer4', '$point4')";
$result=$connect->query($sql4);
$sql11= "INSERT INTO question (answer, point) VALUES ('$answer11', '$point11')";
$result=$connect->query($sql11);
$sql12= "INSERT INTO question (answer, point) VALUES ('$answer12', '$point12')";
$result=$connect->query($sql2);
$sql13= "INSERT INTO question (answer, point) VALUES ('$answer13', '$point13')";
$result=$connect->query($sql3);
$sql14= "INSERT INTO question (answer, point) VALUES ('$answer14', '$point14')";
$result=$connect->query($sql4);
答案 0 :(得分:1)
在插入第一个组之前,请先执行查询,例如:
`select max(qid) from question`
您可以将结果存储为变量$qid
。
$qid ++; //plus one before insert to your database.
$sql1= "INSERT INTO question (answer, point, tid) VALUES ('$answer1', '$point1', '$qid')"; //your first group.
$sql2= "INSERT INTO question (answer, point, tid) VALUES ('$answer1', '$point1', '$qid')"; //your first group.
....//do them all.
$qid++; // plus one before insert the second group
$sql11= "INSERT INTO question (answer, point, tid) VALUES ('$answer1', '$point1', '$qid')"; //your second group.
$sql12= "INSERT INTO question (answer, point, tid) VALUES ('$answer1', '$point1', '$qid')"; //your second group.
....//do them all