phpmyadmin 如何在PHP中将一个用户从一个包含所有用户的表中插入到MySQL的表中

jvidinwx  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(166)

我有一个表,其中包含数据库中users表中的所有注册用户。现在,我有一个接受按钮,单击该按钮后,我想将该特定用户添加到名为accepted_applicants.的新表中
但是当我尝试这样做时,它会将所有用户添加到数据库中。我如何才能只添加我单击的特定行呢?
我做错了什么?
下面是我的代码:

//users query 
$query = $conn->query("SELECT users.id AS userid, users.status AS status, users.name AS username, users.dob AS dob, users.gender AS g, users.country AS country, users.addedDate AS created_date, users.categoryId AS catID ,
     category.name AS awardname, country.id AS countryID, country.country_name AS countryName FROM users LEFT JOIN category ON users.categoryId = category.id LEFT JOIN country ON country.id = users.country;");

<table class="table table-head-fixed text-nowrap">
                  <thead>
                    <tr>
                      <th>ID</th>
                      <th>Name</th>
                      <th>Gender</th>
                      <th>Date of Birth</th>
                      <th>Country</th>
                      <th>Award Category</th>
                      <th>Date Created</th>
                      <th>Application Status</th>
                      <th>Answers</th>
                      <th>Accept</th>
                      <th>Reject</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php

                    while ($row = $query->fetch()) {

                      $userid = $row['userid'];
                      $username = $row['username'];
                      $gender = $row['g'];
                      $dob = $row['dob'];
                      $date = $row['created_date'];
                      $status = $row['status'];
                      $countryName = $row['countryName'];
                      $awardCatName = $row['awardname'];

                      if(isset($_POST['accept'])){
                        $insertq = $conn->prepare("INSERT INTO accepted_applicants (user_id,name,gender,dob,country,award_cat,status) VALUES (?,?,?,?,?,?,?)");
                        $insertq->bindValue(1,$userid);
                        $insertq->bindValue(2,$username);
                        $insertq->bindValue(3,$gender);
                        $insertq->bindValue(4,$dob);
                        $insertq->bindValue(5,$countryName);
                        $insertq->bindValue(6,$awardCatName);
                        $insertq->bindValue(7,$status);
                        $insertq->execute();
                      }

                    ?>
                    <tr>
                      <td><?php echo $row['userid'] ?></td>
                      <td><?php echo $row['username'] ?></td>
                      <td><?php echo $row['g'] ?></td>
                      <td><?php echo $row['dob'] ?></td>    
                      <td><?php echo $countryName ?></td>
                      <td><?php echo $awardCatName ?></td>
                      <td><?php echo $row['created_date'] ?></td>
                      <td><?php if($row['status'] == 1){
                          echo "Submitted";
                        }else{
                          echo "Not Submitted";
                        } ?></td>
                      <td><button>Answers</button></td>
                      <td><input type="submit" name="accept" value="Accept"/></td>
                      <td><input type="submit" name="reject"/></td>

                    </tr>

                    <?php
                    }
                    ?>

                  </tbody>
                </table>
                  </form>
yfwxisqw

yfwxisqw1#

我是这样解的:
我在if语句中添加了一个额外的条件,以检查userid是否等于Accept输入字段的值。

<form method="post" action="applicant_approval.php" >
                  <tbody>
                    <?php

                    while ($row = $query->fetch()) {

                     // $userid = $row['userid'];
                      $username = $row['username'];
                      $gender = $row['g'];
                      $dob = $row['dob'];
                      $date = $row['created_date'];
                      $status = $row['status'];
                      $countryName = $row['countryName'];
                      $awardCatName = $row['awardname'];

                      if(isset($_POST['accept']) && $_POST['accept']==$row['userid']){
                        $insertq = $conn->prepare("INSERT INTO accepted_applicants (user_id,name,gender,dob,country,award_cat,status) VALUES (?,?,?,?,?,?,?)" );
                        $insertq->bindValue(1,$row['userid']);
                        $insertq->bindValue(2,$username);
                        $insertq->bindValue(3,$gender);
                        $insertq->bindValue(4,$dob);
                        $insertq->bindValue(5,$countryName);
                        $insertq->bindValue(6,$awardCatName);
                        $insertq->bindValue(7,$status);
                        $insertq->execute();
                      }

                    ?>

                    <tr>
                      <td><?php echo $row['userid'] ?></td>
                      <td><?php echo $row['username'] ?></td>
                      <td><?php echo $row['g'] ?></td>
                      <td><?php echo $row['dob'] ?></td>    
                      <td><?php echo $countryName ?></td>
                      <td><?php echo $awardCatName ?></td>
                      <td><?php echo $row['created_date'] ?></td>
                      <td><?php if($row['status'] == 1){
                          echo "Submitted";
                        }else{
                          echo "Not Submitted";
                        } ?></td>
                      <td><button>Answers</button></td>
                      <td><input type="submit" name="accept" value="<?php echo $row['userid']; ?>"/></td>
                      <td><input type="submit" name="reject"/></td>

                    </tr>

                    <?php
                    }
                    ?>

                  </tbody>
                </table>
                  </form>

相关问题