简体   繁体   English

SQL仅编辑第一个选项

[英]SQL Edits first option only

This is the code from our handle page we are trying to edit our database in phpmyadmin but can seem to only edit our first entry even though we clicked on other entries' edit button 这是我们处理页面中的代码,我们试图在phpmyadmin中编辑数据库,但是即使单击其他条目的“编辑”按钮,似乎也只能编辑我们的第一个条目

require 'dbfunction.php';
$con = getDbConnect();
$crew_id = $_POST["crew_id"];
$CrewName = $_POST["CrewName"];
$CrewRank = $_POST["CrewRank"];
$StartDate = $_POST["StartDate"];
$EndDate = $_POST["EndDate"];
$PayrollNo = $_POST["PayrollNo"];
$EmployeeNo = $_POST["EmployeeNo"];
$WatchKeeping = $_POST["WatchKeeping"];
$Active = $_POST["Active"];
//$Delete = $_POST["Delete"];

if (!mysqli_connect_errno($con)) {

$queryStr = "SELECT crew_id " .
        "FROM crewlist";
}
$result = mysqli_query($con, $queryStr);
while ($row = mysqli_fetch_array($result)) {
if (!mysqli_connect_errno($con)) {
    $sqlQueryStr = "UPDATE crewlist SET crew_name = '$CrewName', crew_rank = '$CrewRank', start_date = '$StartDate' "
            . ", end_date = '$EndDate', payroll_no = '$PayrollNo'"
            . ", employee_no = '$EmployeeNo', watchkeeping = '$WatchKeeping', active = '$Active' WHERE crew_id = " . $row['crew_id'];

    mysqli_query($con, $sqlQueryStr);

    mysqli_close($con);
} else {
    break;
}
header('Location: crewlisting.php');
}

?>

This code only edits the first entry of our database and the rest remains stagnant. 此代码仅编辑数据库的第一项,其余部分保持停滞。 This is the code from our edit page 这是我们编辑页面中的代码

<?php
            if (!mysqli_connect_errno($con)) {

                $queryStr = "SELECT * " .
                        "FROM crewlist";
            }
            $result = mysqli_query($con, $queryStr);
            while ($row = mysqli_fetch_array($result)) {

                echo "<tr>.<th>" . $row["crew_name"] . "<br></br>" . "</th>";
                echo "<th>" . $row["crew_rank"] . "</th>";
                echo "<th>" . $row["start_date"] . "</th>";
                echo "<th>" . $row["end_date"] . "</th>";
                echo "<th>" . $row["watchkeeping"] . "</th>";
                echo "<th>" . $row["active"] . "</th>";
                //echo "<td><form action=\"editcrew.php\" method=\"post\"><a href='editcrew.php?del={$row['crew_id']}'>edit</a></form></td>";
                echo "<td><a href=\"editcrew.php?id=" . $row['crew_id'] . "\">Edit</a>";
               echo "<td><form action=\"delete.php\" method=\"post\"><a href='crewlisting.php?del={$row['crew_id']}'>delete</a></form></td>";
            }
            ?>

You are closing your mysqli connection in the first iteration of your loop. 您将在循环的第一次迭代中关闭mysqli连接。 Move the mysqli_close($con); 移动mysqli_close($con); behind the loop. 在循环后面。

Edit: And you also redirect to crewlist.php in the loop. 编辑:并且您还重定向到循环中的crewlist.php。 If you reformat your indentation of your code, this will get obvious. 如果您重新格式化代码缩进,这将变得显而易见。

Remove mysqli_close($con) from loop: 从循环中删除mysqli_close($con)

while ($row = mysqli_fetch_array($result)) {
    $sqlQueryStr = "UPDATE crewlist SET crew_name = '$CrewName', crew_rank = '$CrewRank', start_date = '$StartDate' "
            . ", end_date = '$EndDate', payroll_no = '$PayrollNo'"
            . ", employee_no = '$EmployeeNo', watchkeeping = '$WatchKeeping', active = '$Active' WHERE crew_id = " . $row['crew_id'];

    mysqli_query($con, $sqlQueryStr);

}
header('Location: crewlisting.php');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM