尝试在表单中插入所有信息以在数据库中更新

时间:2014-12-29 11:13:32

标签: php html mysql

谢谢你看看这个。我一直坚持这个问题,基本上我正在为我的学校项目做一个招聘机构网站。我正在执行的功能,我可以查看申请任何工作的所有候选人,我可以从下拉框中选择是否"批准","拒绝",或保持为&# 34;未决"它应该将数据库中的表更新为我选择的选项,它将反映在候选人的页面上。然而,使用我现在正在使用的代码,它能够显示我需要的所有信息来自页面上的不同表格,但是当我尝试提交详细信息时,它仅适用于应用的最后一个人而不是其他人。

这是表格:

<form method="post" action="doEditStatus.php">

<div align ="center">

<table border='1' width ="500">

<tr>

 <td> <b> ID </b></td>
            <td> <b> Candidate name </b></td>
            <td> <b> Job ID </b></td>
            <td> <b> Job title </b></td>
            <td> <b> Company </b></td>
            <td> <b> Shortlist status </b></td>
 </tr>

while ($row = mysqli_fetch_array($result)) {
    $jobid = $row['Job_id'];
    $canid = $row['Candidate_id'];
 ?>  
 <tr>

<td><?php echo $canid; ?></td>

<input type="hidden" name="can_id" value=<?php echo $canid ?>>
<input type="hidden" name="job_id" value=<?php echo $jobid ?>>
<?php

$query2 = "SELECT * FROM candidate WHERE Candidate_id =$canid";
$result2 = mysqli_query($link, $query2) or die(mysqli_error($link));

while ($row2 = mysqli_fetch_array($result2)) {
    $canname = $row2['First_name']." ".$row2['Last_name'];
?>

<td><?php echo  $canname; ?></td>


<?php
}

$query3 = "SELECT * FROM jobs WHERE Job_id =$jobid";
$result3 = mysqli_query($link, $query3) or die(mysqli_error($link));

while ($row3 = mysqli_fetch_array($result3)) {

    $jobname = $row3['Job_title'];
    $comid = $row3['Company_id'];
    ?>

    <td><?php echo  $jobid; ?></td>
    <td><?php echo  $jobname; ?></td>        
    <?php
}

$query4 = "SELECT * FROM company WHERE Company_id =$comid";
$result4 = mysqli_query($link, $query4) or die(mysqli_error($link));

while ($row4 = mysqli_fetch_array($result4)) {

    $comname = $row4['Company_name'];
    ?>

    <td><?php echo  $comname; ?></td>

    <?php
    }

?>

<td>
     <select id="id_status" name="shortlist_status">
     <option value="0">Pending...</option>
     <option value="1">Shortlist</option>
     <option value="2">Denied</option>
     </select>

     </td>

 </tr>

<?php

}

?>

</table>

</div>

<input type="submit" value="Submit"/>


</form>

这是dosubmit页面:

<?php
include "dbFunctions.php";

session_start();

$candidate_id = $_POST['can_id'];
$job_id = $_POST['job_id'];
$status = $_POST['shortlist_status'];


$insertQuery = "UPDATE application SET Shortlist_status = '$status' WHERE Candidate_id = $candidate_id AND Job_id = $job_id";

$inserted = mysqli_query($link, $insertQuery) or die(mysqli_error($link));

if($inserted)
{
    $message = 'Profile edited successfully <br><a href="testing.php">Home</a>';;
    echo $candidate_id;
    echo $status;
}
else
{
    $message = "Profile edited failed";
}
echo $message;

?>

1 个答案:

答案 0 :(得分:0)

将所有输入名称以[]结尾。然后,PHP将在$_POST中为每个数组创建一个数组。 E.g。

<input type="hidden" name="can_id[]" value=<?php echo $canid ?>>
<input type="hidden" name="job_id[]" value=<?php echo $jobid ?>>

然后您的PHP可以:

$insertQuery = "UPDATE application SET Shortlist_status = ? WHERE Candidate_id = ? AND Job_id = ?";
$insertStmt = mysqli_prepare($link, $insertQuery);
mysqli_stmt_bind_param($insertStmt, "iii", $status, $candidate_id, $job_id);
foreach ($_POST['can_id'] as $i => $candidate_id) {
    $job_id = $_POST['job_id'][$i];
    $status = $_POST['shortlist_status'][$i];
    $inserted = mysqli_execute($insertStmt) or die(mysqli_error($insertStmt));
    if ($inserted) {
        ...
    } else {
        ...
    }
}