PHP数据库UPDATE函数只修改了第一行?

时间:2015-09-22 09:27:05

标签: php mysql database

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
<?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><a href=\"editcrew.php?id=" . $row['crew_id'] . "\">Edit</a>";

    echo "<td><a href=\"delete.php?id=" . $row['crew_id'] . "\">Delete</a>";
}
?>

editcrew.php

<table>
    <form action="handlecrewedit.php" method="post">
        <tr>
            <td>Crew Name:</td>
            <td><input type="text" name="CrewName" id ="CrewName"required></td>     
        </tr>
        <tr>
            <td>Crew Rank:</td>
            <td><input type="text" name="CrewRank" id="CrewRank" required></td>     
        </tr>
        <tr>
            <td>Start Date:</td>
            <td><input type="text" name="StartDate" id="StartDate" required></td>       
        </tr>
        <tr>
            <td>End Date:</td>
            <td><input type="text" name="EndDate" id="EndDate" required></td>       
        </tr>
        <tr>
            <td>Payroll No:</td>
            <td><input type="text" name="PayrollNo" id="PayrollNo" required></td>       
        </tr>
        <tr>
            <td>Employee No:</td>
            <td><input type="text" name="EmployeeNo" id="EmployeeNo" required></td>     
        </tr>
        <tr>
            <td>Watching Keeping:</td>
            <td><input type="text" name="WatchKeeping" id="WatchKeeping" required></td>     
        </tr>
        <tr>
            <td>Active:</td>
            <td><input type="text" name="Active" id="Active" required></td>     
        </tr>
        <tr>
            <td><input type="submit" value="Submit" ></td>


        </tr>
    </form>
</table>

handlecrewedit.php

<?php

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"];


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);
    header('Location: crewlisting.php');
    mysqli_close($con);
}




mysqli_close($con);
?>

这是我修改条目的另一个问题。我不太确定我是否可以简单地复制并粘贴我的编辑代码的删除代码,但这里有一些粗略的表格。与删除功能不同,通过选择编辑功能,它会将用户定向到表单页面,并要求他们填写更新的数据。

3 个答案:

答案 0 :(得分:1)

错误

  1. 此处?>
  2. 缺少结束标记(<form action="<?php echo $_SERVER['PHP_SELF']; ?" method="post">
  3. $queryStr = "SELECT * " ."FROM crewlist";应为$queryStr = "SELECT * FROM crewlist"
  4. <br> echo "<tr>.<th>" . $row["crew_name"] . "<br></br>" . "</th>";上没有结束标记$result = mysqli_query($con, $queryStr);
  5. 您不知道为此while Cz传递了多少数据行计数验证
  6. if (!mysqli_connect_errno($con))循环内没有使用检查。 System/download
  7. 如果您只是更新字段,则将更新查询置于错误的地方

答案 1 :(得分:1)

使用以下代码替换 editcrew.php handlecrewedit.php 文件代码。

<强> editcrew.php

<table>
    <form action="handlecrewedit.php" method="post">
        <input type="hidden" name="crew_id" value="<?php echo $_GET['id']; ?>" />
        <tr>
            <td>Crew Name:</td>
            <td><input type="text" name="CrewName" id ="CrewName"required></td>     
        </tr>
        <tr>
            <td>Crew Rank:</td>
            <td><input type="text" name="CrewRank" id="CrewRank" required></td>     
        </tr>
        <tr>
            <td>Start Date:</td>
            <td><input type="text" name="StartDate" id="StartDate" required></td>       
        </tr>
        <tr>
            <td>End Date:</td>
            <td><input type="text" name="EndDate" id="EndDate" required></td>       
        </tr>
        <tr>
            <td>Payroll No:</td>
            <td><input type="text" name="PayrollNo" id="PayrollNo" required></td>       
        </tr>
        <tr>
            <td>Employee No:</td>
            <td><input type="text" name="EmployeeNo" id="EmployeeNo" required></td>     
        </tr>
        <tr>
            <td>Watching Keeping:</td>
            <td><input type="text" name="WatchKeeping" id="WatchKeeping" required></td>     
        </tr>
        <tr>
            <td>Active:</td>
            <td><input type="text" name="Active" id="Active" required></td>     
        </tr>
        <tr>
            <td><input type="submit" value="Submit" ></td>


        </tr>
    </form>
</table>

<强> handlecrewedit.php

<?php
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"];

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 = " . $crew_id . "";
    mysqli_query($con, $sqlQueryStr);
}
header('Location: crewlisting.php');
mysqli_close($con);
?>

答案 2 :(得分:0)

我认为建议的答案错过了一点。 它只更新第一行,因为你使用重定向头退出while循环。

URL

将这两行放在while循环之外,它应该更新每一行。

你的while循环将是:

header('Location: crewlisting.php');
mysqli_close($con);