Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

mysql - PHP database UPDATE function only modified the first row?

<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);
?>

This is another issue on my modifying entries. I'm not quite sure if I can simply copy and paste my delete code for my edit code, but here is some rough gauge of my table. Unlike the delete function, by selecting the edit function, it directs the user to the form page and it requires them to fill in the updated data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Replace editcrew.php and handlecrewedit.php file code with below code.

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);
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...