插入数据到表格,使用一个循环从另一个表格获取数据。

huangapple go评论71阅读模式
英文:

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

问题

我尝试从循环中获取CompanyIDJobListingID,以便在点击申请按钮后将数据传递到另一个名为"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?

&lt;?php
include (&quot;Jobseeker_session.php&quot;);
require_once(&#39;connect.php&#39;);
?&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width,initial-scale=1.0&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Job Seeker Website&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
&lt;link href=&#39;https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css&#39; rel=&#39;stylesheet&#39;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;sidebar&quot;&gt;
&lt;div class=&quot;logo-details&quot;&gt;
&lt;i class=&#39;#&#39; &gt;&lt;/i&gt;
&lt;span class=&quot;logo_name&quot;&gt;Job Seekeer&lt;/span&gt;
&lt;/div&gt;
&lt;ul class=&quot;nav-links&quot;&gt;
&lt;li&gt;
&lt;a href=&quot;JobSeekerProfile.php&quot; &gt;
&lt;i class=&#39;bx bxs-user-detail&#39; &gt;&lt;/i&gt;				
&lt;span class=&quot;links_name&quot;&gt;Profile&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;JobSeekerAboutUs.php&quot; &gt;
&lt;i class=&#39;bx bxs-user-account&#39; &gt;&lt;/i&gt;				
&lt;span class=&quot;links_name&quot;&gt;About Us&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;JobSeekerJobListing.php&quot;&gt;
&lt;i class=&#39;bx bx-list-ul&#39; &gt;&lt;/i&gt;				  
&lt;span class=&quot;links_name&quot;&gt;Job Listing&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;JobSeekerCompanyListing.php&quot;&gt;
&lt;i class=&#39;bx bx-list-ul&#39; &gt;&lt;/i&gt;				  
&lt;span class=&quot;links_name&quot;&gt;Company Listing&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;JobSeekerFavourite.php&quot;&gt;
&lt;i class=&#39;bx bxs-bookmark&#39; &gt;&lt;/i&gt;
&lt;span class=&quot;links_name&quot;&gt;Favourite&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;JobSeekerApplication.php&quot;&gt;
&lt;i class=&#39;bx bxs-business&#39;&gt;&lt;/i&gt;
&lt;span class=&quot;links_name&quot;&gt;Application&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;JobSeekerReportHistory.php&quot;&gt;
&lt;i class=&#39;bx bxs-time&#39;&gt;&lt;/i&gt;
&lt;span class=&quot;links_name&quot;&gt;History Report&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;JobSeekerMessage.php&quot;&gt;
&lt;i class=&#39;bx bxs-chat&#39; &gt;&lt;/i&gt;
&lt;span class=&quot;links_name&quot;&gt;Message&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;JobSeekerResume.php&quot;&gt;
&lt;i class=&#39;bx bx-paperclip&#39;&gt;&lt;/i&gt;
&lt;span class=&quot;links_name&quot;&gt;Resume&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;log_out&quot;&gt;
&lt;a href=&quot;#&quot;&gt;
&lt;i class=&#39;bx bx-log-out&#39;&gt;&lt;/i&gt;
&lt;span class=&quot;links_name&quot;&gt;Log out&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;nav&gt;
&lt;div class=&quot;sidebar-button&quot;&gt;
&lt;i class=&#39;bx bx-menu sidebarBtn&#39;&gt;&lt;/i&gt;
&lt;span class=&quot;dashboard&quot;&gt;Dashboard&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;search-box&quot;&gt;
&lt;input type=&quot;text&quot; placeholder=&quot;Search...&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;profile-details&quot;&gt;
&lt;span class=&quot;admin_name&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;section class=&quot;home-section&quot;&gt;
&lt;div class=&quot;home-content&quot;&gt;
&lt;div class=&quot;all-boxes&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Job Listing&lt;/div&gt;
&lt;div class=&quot;applicant-boxes&quot;&gt;
&lt;form method=&quot;post&quot;&gt;
&lt;div class=&quot;listing-boxes&quot;&gt;
&lt;div class=&quot;left&quot;&gt;
&lt;?php
$sql = &quot;select * from joblisting&quot;;
$result = mysqli_query($connect,$sql);
while($row = mysqli_fetch_assoc($result))
{
$eee = $row[&#39;JobListingID&#39;];
$new = $row[&#39;CompanyID&#39;]; 
$sql2 = &quot;select * from company_info WHERE CompanyID = &#39;$new&#39;&quot;;
$result2 = mysqli_query($connect,$sql2);
$row2 = mysqli_fetch_assoc($result2);
if(is_array($row2)){ 
?&gt;
&lt;div class=&quot;job-card&quot;&gt;
&lt;div class=&quot;img&quot;&gt;
&lt;img src=&quot;&lt;?php echo $row2[&quot;CompanyLogo&quot;]; ?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;h2 &gt;&lt;a href=&quot;JobSeekerJobDescription.php?jobid=&lt;?php echo $eee;?&gt;&quot;&gt;&lt;?php echo $row[&quot;JobTitle&quot;]; ?&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;b&gt;Company:&lt;/b&gt; &lt;?php echo $row2[&quot;CompanyName&quot;]; ?&gt;&lt;?php echo $new;?&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Location:&lt;/b&gt; &lt;?php echo $row[&quot;JobLocation&quot;]; ?&gt;&lt;?php echo $eee;?&gt;&lt;?php echo $new;?&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Salary:&lt;/b&gt; &lt;?php echo $row[&quot;JobSalary&quot;]; ?&gt;&lt;?php echo $eee;?&gt;&lt;/p&gt;
&lt;div class=&quot;action&quot;&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; name=&quot;apply&quot;&gt;Apply&lt;?php echo $eee;?&gt;&lt;?php echo $new;?&gt;&lt;/button&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; name=&quot;favourite&quot;&gt;Add To Favourite&lt;?php echo $eee;?&gt;&lt;?php echo $new;?&gt;&lt;/button&gt;
&lt;a href=&quot;JobSeekerReport.php?joblistingid=&lt;?php echo $eee;?&gt;&quot;&gt;Report Job&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;?php
}
}
?&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/section&gt;		  
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?php
if(isset($_POST[&#39;apply&#39;]))
{date_default_timezone_set(&quot;Asia/Kuala_Lumpur&quot;);
$date=date(&#39;d/m/Y H:i:s&#39;);
$query = &quot;INSERT INTO application(DateApplied,ApplicationStatus,Job_SeekerID,CompanyID,JobListingID)
VALUES(&#39;$date&#39;,&#39;In Progress&#39;,&#39;$jobseekerid&#39;,&#39;$new&#39;,&#39;$eee&#39;)&quot;;
$result = mysqli_query($connect,$query);
}
?&gt;			
&lt;script&gt;
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
&lt;/script&gt;

答案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传递参数,您可以在每个项目周围放置表单标签,并将idlistid放入隐藏输入中,以便在单击项目按钮时提交一个表单。实际上,您会在页面上有多少个项目就有多少个表单。

但是,由于每个项目都有更多的操作(申请和收藏),您还可以考虑通过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:

&lt;?php
$path = rtrim(parse_url($_SERVER[&#39;REQUEST_URI&#39;], PHP_URL_PATH), &#39;/&#39;);
parse_str(parse_url($_SERVER[&#39;REQUEST_URI&#39;], PHP_URL_QUERY), $params);
var_dump($params); // Kint debugger
?&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php for ($i = 1; $i &lt;= 2; $i++) : ?&gt;
&lt;h2&gt;Listing &lt;?= $i ?&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;?php for ($j = 1; $j &lt;= 3; $j++) : $id = uniqid(); ?&gt;
&lt;li&gt;
&lt;p&gt;Job &lt;?= $j ?&gt;&lt;/p&gt;
&lt;a href=&quot;&lt;?= $path ?&gt;?action=apply&amp;id=&lt;?= $id ?&gt;&amp;listid=&lt;?= $i ?&gt;&quot;&gt;Apply&lt;/a&gt; | &lt;a href=&quot;&lt;?= $path ?&gt;?action=favourite&amp;id=&lt;?= $id ?&gt;&amp;listid=&lt;?= $i ?&gt;&quot;&gt;Favourite&lt;/a&gt;
&lt;/li&gt;
&lt;?php endfor; ?&gt;
&lt;/ul&gt;
&lt;?php endfor; ?&gt;
&lt;/body&gt;
&lt;/html&gt;

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.

huangapple
  • 本文由 发表于 2023年6月29日 08:05:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定