2017-06-05 90 views
0

我目前正在为客户端数据库管理制作一个系统。 mySQL中有四个表格用于这个系统,管理员,员工,客户和项目。项目表中有一个来自客户端表的外键,它是clientid。PHP表格无法更新

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

以下是员工更新代码。

<?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小时,我这个接近哭了笑。一直在阅读关于更新表单的所有主题,但仍然没有答案。希望这里的任何人都能帮助我。谢谢。

+4

**危险**:您很容易[SQL注入攻击](http://bobby-tables.com/)**,您需要[防御](http://stackoverflow.com/问题/ 60174/best-way-to-prevent-sql -injection-in-php)自己从。 – Quentin

+1

借调上述,如果可能的话,我会建议使用[PDO Prepared Statements](http://php.net/manual/en/pdo.prepared-statements.php)。 –

+0

您还应该从更新查询集字段中删除'clientid'; – itzmukeshy7

回答

0

您使用的客户端表更新的代码使用此代码:

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 S的关系是$_POST,否则更新后的数据将是相同的之前的数据(因为$row是结果来自查询SELECT * FROM client WHERE clientid='$clientid')。您可以在工作人员表更新代码做是正确的:

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

请注意,你的脚本是在SQL Injection Attack风险。看看Little Bobby Tables发生了什么事。即使是if you are escaping inputs, its not safe!。改为使用prepared parameterized statements