将数据插入具有2个不同表的一个表中

时间:2018-08-26 12:11:03

标签: php sql mysqli

美好的一天!我正在制作评估问卷,并尝试将Question_IDRating_ID插入Question_rating表中,但是Question_IDRating_ID来自以下不同的表:

这是我的问题表: question table

这是我的评分表: rating table

这是我插入这些ID的表: question rating table

这是我的调查表: questionniare form

这是我的表单代码:

<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。谁能给我一些启示?

2 个答案:

答案 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));
        }
    }