美好的一天!我正在制作评估问卷,并尝试将Question_ID
和Rating_ID
插入Question_rating表中,但是Question_ID
和Rating_ID
来自以下不同的表:
这是我的表单代码:
<form method="POST">
<table class="table table-bordered stick-top">
<thead class="text-center" id="data">
<tr>
<th></th>
<th>Questions</th>
<th id="try">Rating</th>
</tr>
</thead>
<tbody class="body_data">
<?php
$queryQuestion = "SELECT * FROM question";
$resultQuestion = $conn -> query($queryQuestion);
?>
<?php while ($row2 = $resultQuestion -> fetch_object()): ?>
<tr>
<td><?php echo $row2 -> Question_ID ?></td>
<td><?php echo $row2 -> Question_Description ?></td>
<td>
<?php
$q1 = "SELECT * FROM rating ORDER BY Rating_ID ASC";
$r1 = $conn -> query($q1);
while($row = $r1 -> fetch_object()){
echo "<input id='radio' type='radio' name='" . $row2 -> Question_ID . "' value='" . $row -> Rating_ID . "'>" . $row -> Rating_ID;
}
?>
</td>
</tr>
<?php endwhile; ?>
<tr>
<td></td> <td><button type="submit" name="submit" class="btn btn-primary">Submit Evaluation</button></td>
<td></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
这是我的插入代码:
<?php
if (isset($_POST['submit'])) {
$q3 = "insert into question_rating (null, question_id, rating_id)
select null, a.question_id, b.rating_id
from question a
cross join rating b ";
$conn -> query($q3);
}
?>
我仍在学习编程SQL。谁能给我一些启示?
答案 0 :(得分:1)
您可以使用插入选择和交叉连接选择来处理单个查询
insert into question_rating (question_id, question_rating_id)
select a.question_id, b.rating_id
from question a
cross join rating b
如果使用null,则需要3列进行插入,并需要3列进行选择,例如:
$q3 = "insert into question_rating (null, question_id, rating_id)
select null, a.question_id, b.rating_id
from question a
cross join rating b
答案 1 :(得分:0)
解决了!
这是我的代码:
if (isset($_POST['submit'])) {
$sql=mysqli_query($conn,"select * from question")or die ("erorr");
while(list($Question_ID,$Question_Description)=mysqli_fetch_array($sql))
{
$evl_val=$_POST['radio'.$Question_ID];
mysqli_query($conn,"INSERT INTO `question_rating`(`Question_Rating_ID`,`Question_ID`,`Rating_ID`) VALUES (null, '$Question_ID','$evl_val')") or die (mysqli_error($connection));
}
}