英文:
How do I insert data to table from a while loop that fetch another table data?
问题
我尝试从循环中获取CompanyID
和JobListingID
,以便在点击申请按钮后将数据传递到另一个名为"application"的表格,但不知何故只传递了最新的JobListingID
和来自joblisting表的CompanyID
。我应该如何做才能传递我需要的数据?
<?php
include("Jobseeker_session.php");
require_once('connect.php');
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<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/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
</head>
<body>
<!-- 省略部分HTML内容 -->
<div class="listing-boxes">
<?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">
<!-- 省略部分HTML内容 -->
<button type="submit" class="btn btn-primary" name="apply">Apply</button>
<button type="submit" class="btn btn-primary" name="favourite">Add To Favourite</button>
<a href="JobSeekerReport.php?joblistingid=<?php echo $eee; ?>">Report Job</a>
</div>
<?php
}
}
?>
</div>
<!-- 省略部分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>
</body>
</html>
请注意,我只提供了PHP代码的翻译,未包括HTML和CSS部分。希望这能帮助你解决问题。如果你需要进一步的帮助,请随时提出。
英文:
I was try to get CompanyID
and JobListingID
from the while loop so after clicking apply button it can pass the data to another table called application, but somehow it only pass the latest JobListingID
with the CompanyID
from the joblisting table to the application How am I suppose to do to make it pass the data that I need?
<?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/boxicons@2.1.4/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>
答案1
得分: 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传递参数,您可以在每个项目周围放置表单标签,并将id
和listid
放入隐藏输入中,以便在单击项目按钮时提交一个表单。实际上,您会在页面上有多少个项目就有多少个表单。
但是,由于每个项目都有更多的操作(申请和收藏),您还可以考虑通过JavaScript和fetch进行POST,并处理不刷新页面的情况下的收藏操作的结果。实际上,为您的应用程序创建了一种API。
英文:
You don't need a form to do this. Easiest would be to generate url's with the parameters needed. Here's an example:
<?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>
Demo here
Common practice is that getting info/data uses GET requests and sending data for saving / mutation is done via POST requests, hence the url's with parameters.
If you do want to POST the parameters rather than via GET, you could put form tags around every item and put the id
and listid
in hidden inputs, so that you post one form on clicking the button for an item. In effect, you would have as many forms in the page as there are items.
But since you have more actions per item (apply and favourite), you could also consider POSTing via javascript with fetch and handling the result for favourite-action without refresh. Effectively creating a sort of API for your application.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论