如何从获取另一个表数据的 while 循环中将数据插入表?

How do I insert data to table from a while loop that fetch another table data?

提问人:user16804141 提问时间:6/29/2023 最后编辑:Barmaruser16804141 更新时间:6/29/2023 访问量:40

问:

我试图从 while 循环中获取和获取,因此在单击应用按钮后,它可以将数据传递给另一个名为 application 的表,但不知何故,它只将最新的 from the joblisting 表传递给应用程序我该如何做才能让它传递我需要的数据?CompanyIDJobListingIDJobListingIDCompanyID

<?php
include ("Jobseeker_session.php");
require_once('connect.php');


?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<html>
<head>
    <title>Job Seeker Website</title>
    <link rel="stylesheet" href="style.css">
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
</head>

<body>
    <div class="sidebar">
        <div class="logo-details">
          <i class='#' ></i>
          <span class="logo_name">Job Seekeer</span>
        </div>
          <ul class="nav-links">
            <li>
              <a href="JobSeekerProfile.php" >
              <i class='bx bxs-user-detail' ></i>               
              <span class="links_name">Profile</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerAboutUs.php" >
              <i class='bx bxs-user-account' ></i>              
              <span class="links_name">About Us</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerJobListing.php">
              <i class='bx bx-list-ul' ></i>                  
              <span class="links_name">Job Listing</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerCompanyListing.php">
              <i class='bx bx-list-ul' ></i>                  
              <span class="links_name">Company Listing</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerFavourite.php">
              <i class='bx bxs-bookmark' ></i>
              <span class="links_name">Favourite</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerApplication.php">
              <i class='bx bxs-business'></i>
              <span class="links_name">Application</span>
              </a>
            </li>
           
            <li>
              <a href="JobSeekerReportHistory.php">
              <i class='bx bxs-time'></i>
              <span class="links_name">History Report</span>
              </a>
            </li>

            <li>
              <a href="JobSeekerMessage.php">
              <i class='bx bxs-chat' ></i>
              <span class="links_name">Message</span>
              </a>
            </li>
            <li>
              <a href="JobSeekerResume.php">
              <i class='bx bx-paperclip'></i>
              <span class="links_name">Resume</span>
              </a>
            </li>
    
            <li class="log_out">
              <a href="#">
              <i class='bx bx-log-out'></i>
              <span class="links_name">Log out</span>
              </a>
            </li>
          </ul>
    </div>
    
          <nav>
        
            <div class="sidebar-button">
              <i class='bx bx-menu sidebarBtn'></i>
              <span class="dashboard">Dashboard</span>
            </div>
        
            <div class="search-box">
              <input type="text" placeholder="Search...">
              
            </div>
        
            <div class="profile-details">
              <span class="admin_name"></span>
              
            </div>
          </nav>

          <section class="home-section">
            <div class="home-content">
                <div class="all-boxes">
                    <div class="title">Job Listing</div>
                    
                        <div class="applicant-boxes">
                            <form method="post">
                            <div class="listing-boxes">
                                    <div class="left">
                                        <?php
                                        $sql = "select * from joblisting";
                                        $result = mysqli_query($connect,$sql);
                                        
                                        
                                        while($row = mysqli_fetch_assoc($result))
                                        {
                                        $eee = $row['JobListingID'];
                                        $new = $row['CompanyID']; 
                                        $sql2 = "select * from company_info WHERE CompanyID = '$new'";
                                        $result2 = mysqli_query($connect,$sql2);
                                        $row2 = mysqli_fetch_assoc($result2);
                                        if(is_array($row2)){ 
                                        ?>
                                        <div class="job-card">
                                            <div class="img">
                                                <img src="<?php echo $row2["CompanyLogo"]; ?>">
                                            </div>
                                            <div class="content">
                                                <h2 ><a href="JobSeekerJobDescription.php?jobid=<?php echo $eee;?>"><?php echo $row["JobTitle"]; ?></a></h2>
                                                <p><b>Company:</b> <?php echo $row2["CompanyName"]; ?><?php echo $new;?></p>
                                                <p><b>Location:</b> <?php echo $row["JobLocation"]; ?><?php echo $eee;?><?php echo $new;?></p>
                                                <p><b>Salary:</b> <?php echo $row["JobSalary"]; ?><?php echo $eee;?></p>
                                                <div class="action">
                                                
                                                <button type="submit" class="btn btn-primary" name="apply">Apply<?php echo $eee;?><?php echo $new;?></button>
                                                <button type="submit" class="btn btn-primary" name="favourite">Add To Favourite<?php echo $eee;?><?php echo $new;?></button>
                                                <a href="JobSeekerReport.php?joblistingid=<?php echo $eee;?>">Report Job</a>
                                                
                                            </div>
                                            </div>
                                            
                                        </div>
                                        <?php
                                        
                                                }
                                                
                                        
                                        }
                                        ?>
                                        
                                    </div>
                                
                            </div>
                            </form>
                        </div>
                </div>
                </div>
            </div>
            
          </section>          
            
            </div>
            
</body>
</html>
<?php
                                        
                                    
                                    
    if(isset($_POST['apply']))
    {date_default_timezone_set("Asia/Kuala_Lumpur");
        $date=date('d/m/Y H:i:s');
        $query = "INSERT INTO application(DateApplied,ApplicationStatus,Job_SeekerID,CompanyID,JobListingID)
        VALUES('$date','In Progress','$jobseekerid','$new','$eee')";
        $result = mysqli_query($connect,$query);
        }
?>          
                            





<script>
    if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
</script>
php html 数据库 mysqli

评论

0赞 Barmar 6/29/2023
表单没有任何字段来保存您提交的参数。你期望如何从表格中得到任何东西?<input>
0赞 Barmar 6/29/2023
查询中的变量不是来自表单提交。它们只是您在循环中分配给这些变量的最后一个值。INSERT
0赞 Dharman 7/22/2023
警告:您对 SQL 注入持开放态度,并且应该使用参数化的预准备语句,而不是手动构建查询。它们由 PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,您仍然面临数据损坏的风险逃跑是不够的!

答:

1赞 Jos 6/29/2023 #1

您不需要表单即可执行此操作。最简单的方法是生成带有所需参数的 url。下面是一个示例:

<?php
$path = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
parse_str(parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), $params);

var_dump($params); // Kint debugger
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <?php for ($i = 1; $i <= 2; $i++) : ?>
        <h2>Listing <?= $i ?></h2>
        <ul>
            <?php for ($j = 1; $j <= 3; $j++) : $id = uniqid(); ?>
                <li>
                    <p>Job <?= $j ?></p>
                    <a href="<?= $path ?>?action=apply&id=<?= $id ?>&listid=<?= $i ?>">Apply</a> | <a href="<?= $path ?>?action=favourite&id=<?= $id ?>&listid=<?= $i ?>">Favourite</a>
                </li>
            <?php endfor; ?>
        </ul>
    <?php endfor; ?>
</body>

</html>

演示在这里

通常的做法是获取信息/数据使用 GET 请求,并通过 POST 请求发送用于保存/更改的数据,因此 url 带有参数。

如果您确实想 POST 参数而不是通过 GET,您可以在每个项目周围放置表单标签并将 and 放在隐藏的输入中,以便您在单击项目的按钮时发布一个表单。实际上,页面中的表单数量与项目数量一样多。idlistid

但是,由于每个项目有更多的操作(应用和收藏),您也可以考虑通过带有 fetch 的 javascript 进行 POSTing,并在不刷新的情况下处理收藏夹操作的结果。有效地为您的应用程序创建一种 API。