无法更新PHP表单

无法更新PHP表单,php,mysql,Php,Mysql,我目前正在制作一个客户端数据库管理系统。本系统mySQL中有四个表,分别是:;管理员、员工、客户和项目。项目表有一个来自客户机表的外键,即clientid 现在,我已经为所有这些表制作了表单,以便用户可以将数据输入其中。奇怪的是,唯一可以成功更新的表单是员工表单。客户和项目表格都无法更新。它返回为成功,但数据不会更改 以下是员工更新代码 <?php include 'database.php'; $staffid = $_GET['staffid'];

我目前正在制作一个客户端数据库管理系统。本系统mySQL中有四个表,分别是:;管理员、员工、客户和项目。项目表有一个来自客户机表的外键,即clientid

现在,我已经为所有这些表制作了表单,以便用户可以将数据输入其中。奇怪的是,唯一可以成功更新的表单是员工表单。客户和项目表格都无法更新。它返回为成功,但数据不会更改

以下是员工更新代码

    <?php 
    include 'database.php';

    $staffid = $_GET['staffid'];
    $sql = "SELECT * FROM staff WHERE staffid='$staffid'";
    $result = mysqli_query($conn,$sql);

    while ($row=mysqli_fetch_array($result)){
      $staffname = $row['staffname'];
      $staffemail = $row['staffemail'];
      $staffphone = $row['staffphone'];
    }

   if(isset($_POST['submit'])){
     $staffname = $_POST['staffname'];
     $staffemail = $_POST['staffemail'];
     $staffphone = $_POST['staffphone'];

   $sql = "UPDATE staff SET   

    staffname='$staffname',staffemail='$staffemail',staffphone='$staffphone'  WHERE staffid='$staffid'";

    $result = mysqli_query($conn,$sql);

    if($result){
        echo "<table><td><tr><h4>Record has been updated successfully!<br></tr></td></h4></table>";
    }
    else {
        echo "<h4>Record has <b>NOT</b> been updated successfully<br></h4>";
    }
}
?>


<form action="" method="post">
<table class ="table1">
<tr>
<td>Staff Name:</td> <td><input type="text" name="staffname" size="50" value="<?php echo $staffname;?>"></td>
</tr>

<tr>
<td>Staff Email:</td> <td><input type="text" name="staffemail" size="50" value="<?php echo $staffemail;?>"></td>
</tr>

<tr>
<td>Staff Phone No:</td> <td><input type="text" name="staffphone" size="50" value="<?php echo $staffphone;?>"></td>
</tr>

<td><input type="submit" value="Update" name="submit"> <input type="button" value="View" name="view" onclick='location.href="viewstaff.php"'></td>
</table>
</form>
现在是客户机表的更新代码

<?php 
include 'database.php';

$clientid = $_GET['clientid'];
$sql = "SELECT * FROM client WHERE clientid='$clientid'";
$result = mysqli_query($conn,$sql) or die ("Error in query: $query. ".mysqli_error());

while ($row=mysqli_fetch_array($result)){
    $clientid = $row['clientid'];
    $clientname = $row['clientname'];
    $clientno = $row['clientno'];
    $clientemail = $row['clientemail'];
    $clientadd = $row['clientadd'];
}

if(isset($_POST['submit'])){
    $clientid = $row['clientid'];
    $clientname = $row['clientname'];
    $clientno = $row['clientno'];
    $clientemail = $row['clientemail'];
    $clientadd = $row['clientadd'];

    $sql = "UPDATE client SET clientid='$clientid',clientname='$clientname',clientno='$clientno',clientemail='$clientemail',clientadd='$clientadd' WHERE clientid='$clientid'";

    $result = mysqli_query($conn,$sql) or die ("Error in query: $query. ".mysqli_error());

    if($result){
        echo "<table><td><tr><h4>Record has been updated successfully!<br></tr></td></h4></table>";
    }
    else {
        echo "<h4>Record has <b>NOT</b> been updated successfully<br></h4>";
    }
}
?>


<form action="" method="post">
<table class ="table1">
<tr>
<td>Client ID:</td> <td><input type="text" name="clientid" size="50" value="<?php echo $clientid;?>"></td>
</tr>

<tr>
<td>Client Name:</td> <td><input type="text" name="clientname" size="50" value="<?php echo $clientname;?>"></td>
</tr>

<tr>
<td>Client Phone No.:</td> <td><input type="text" name="clientno" size="50" value="<?php echo $clientno;?>"></td>
</tr>

<tr>
<td>Client Email:</td> <td><input type="text" name="clientemail" size="50" value="<?php echo $clientemail;?>"></td>
</tr>

<tr>
<td>Client Address:</td> <td><input type="text" name="clientadd" size="50" value="<?php echo $clientadd;?>"></td>
</tr>

<td><input type="submit" value="Update" name="submit"> <input type="button" value="View" name="view" onclick='location.href="viewclient.php"'></td>
</table>
</form>

也许我很愚蠢,但是我已经试着解决这个问题3个小时了,我几乎要哭了,哈哈。我一直在阅读所有关于更新表单的帖子,但是仍然没有答案。希望这里的任何人都能帮助我。谢谢。

用于客户端表更新的代码使用以下代码:

if(isset($_POST['submit'])){
    $clientid = $row['clientid'];       // $row should be $_POST
    $clientname = $row['clientname'];   // $row should be $_POST
    $clientno = $row['clientno'];       // $row should be $_POST
    $clientemail = $row['clientemail']; // $row should be $_POST
    $clientadd = $row['clientadd'];     // $row should be $_POST
但是这些$row应该是$\u POST,否则更新的数据将与以前的数据相同,因为$row是查询SELECT*from client的结果,其中clientid='$clientid'。您可以在staff表更新代码中正确执行此操作:

 if(isset($_POST['submit'])){
     $staffname = $_POST['staffname'];
     $staffemail = $_POST['staffemail'];
     $staffphone = $_POST['staffphone'];

请注意,您的脚本有被删除的风险。看看发生了什么事。即使改用。

危险:您很容易受到您需要从中获取的信息的攻击。如果可能,我建议您使用上述信息。您还应该从更新查询集字段中删除clientid;如果你不想使用PDO,那么你至少应该在mysqli中使用预先准备好的语句,并尝试使用面向对象的方式,而不是这种功能性的方式。好的,谢谢大家的建议!我将研究PDO并准备好声明。