2017-04-04 56 views
-1

我一直在创造一个工作人员目录网页aplication学习和测试我学到的东西,但有从我的MySQL数据库所在的staff_id = staff_id从以前的delete一个页面记录问题IM从MySQL删除记录,它会重新记录并填充字段,但是当我选择“是”时不会删除记录。我不能使用PHP

任何帮助或指导做好将是巨大的:d因此按

<?php 
# display all php errors 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

# include dbConnection details 
require '/includes/dbconn.php'; 

# if $staff_id is not empty, GET the id 
if (!empty($_GET['staff_id'])) { 
    $staff_id = $_GET['staff_id']; 
} 

# check if data has been posted 
if (!empty($_POST)) { 
    # modify the variable request from GET to POST 
    $staff_id = $_POST['staff_id']; 

    # delete data from joke table 
    $sql = "DELETE FROM staff WHERE staff_id = :staff_id"; 
    $stmt = $DB_con->prepare($sql); 
    $stmt->bindValue(':staff_id', $staff_id); 
    $stmt->execute(); 

    header("Location: Admin_View_Employee.php"); 
    exit();  
# display current record  
} else { 
    $sql = "SELECT staff_id, forename, surname, job_role, joined, manager_id, extension, mobile, email, background_info, qualifications, achievements, username, password, level, dept_id 
      FROM staff 
      WHERE staff_id = :staff_id"; 
    $stmt = $DB_con->prepare($sql); 
    $stmt->bindValue(':staff_id', $staff_id); 
    $stmt->execute(); 
    $data = $stmt->fetch(); 
    $staff_id = $data['staff_id']; 
    $forename = $data['forename']; 
    $surname = $data['surname']; 
    $job_role = $data['job_role']; 
    $manager_id = $data['manager_id']; 
    $joined = $data['joined']; 
    $extension = $data['extension']; 
    $mobile = $data['mobile']; 
    $email = $data['email']; 
    $background_info = $data['background_info']; 
    $qualifications = $data['qualifications']; 
    $achievements = $data['achievements']; 
    $dept_id = $data['dept_id']; 
} 


    # if data is not found 
    if(!$data) 
    { 
     header("Location: Admin_View_Employee.php"); 
     exit(); 
    } 

?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Update Employee</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" href="css/style.css" type="text/css" /> 
</head> 


     <body> 
    <div> 
    <table width="100%" border="0"> 
    <tbody> 
    <tr> 
    <td width="25%"><img src="images/beacon_logo.png" width="240" height="168" align="left"></td> 
    <td width="50%"><h1 style="text-align: center" border="0">Update Employee</h1></td> 
    <td width="25%">&nbsp;</td> 
    </tr> 
    </tbody> 
    </table> 
</div> 
    <?php include('Admin_Nav.php'); ?> 



      </tbody> 
     </table></p> 
    <div>  
     <form action="Admin_Delete_Employee.php?staff_id=<?php echo $staff_id ?>" method="post"> 
     <input type="hidden" name="id" value="<?php echo $staff_id; ?>"/> 
     <p>Are you sure to delete this record?</p> 


<table width="574"> 
      <tr> 
       <th width="131">Staff ID</th> 
       <td width="84"><?php echo $staff_id;?></td> 
      </tr> 
       <tr> 
       <th>Forename</th> 
       <td><?php echo $forename;?></td>    
      </tr> 
      <tr> 
       <th>Surname</th> 
       <td><?php echo $surname;?></td>    
      </tr> 
      <tr> 
       <th>Job Role</th> 
       <td><?php echo $job_role;?></td> 
      </tr> 
     </tr>    
     </table> 

      <div> 
       <button type="submit">[Yes]</button> 
       <a href="Admin_View_Employee.php">[No]</a> 
      </div> 
     </form> 
    </div> 
</body> 
</html> 
+0

'NAME = “ID”'!='$ _ POST [ 'staff_id']'...或者把它作为'$ _GET'因为你也正在发送这种方式'?staff_id = <?PHP的echo $ staff_id?''你只需要取消其他覆盖。 – chris85

+0

你得到了什么错误信息,你有什么问题的确切代码段? –

回答

0

http://php.net/manual/en/pdo.prepared-statements.php

$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)"); 
$stmt->bindParam(':name', $name); 
$stmt->bindParam(':value', $value); 

使用bindParam

+0

这为什么会有所作为?该变量被覆盖为空。 – chris85

+0

您可以检查此情况下http://stackoverflow.com/questions/1179874/what-is-the-difference-between-bindparam-and-bindvalue – maSTAShuFu

+0

我知道它做什么。不过看看代码。 '$ staff_id = $ _POST ['staff_id'];'和'”/>'。 – chris85

0

查NGE从post表单方法get

<form action="Admin_Delete_Employee.php?staff_id=<?php echo $staff_id ?>" method="get"> 
+0

这已经是一个'GET'。根据你发布的字符串'?staff_id = <?php echo $ staff_id?>'。它覆盖在'delete'中。这是一个错字,应该投票结束。 – chris85

+0

大致问题是,你说什么,但现实是仅仅是'$ staff_id = $ _ POST [“staff_id”];'这将覆盖'GET'值的OP已经发送。如果OP变化的方法其他脚本值将需要的,如果改变'(!空($ _ POST)){'..等等 – chris85

+0

在这种情况下,它应该是'id'不'staff_id'吧? @ chris85 – C2486

0

难道是这容易吗?

变化:

$stmt->bindValue(':staff_id', $staff_id); 

$stmt->bindValue(':staff_id', $staff_id, PDO::PARAM_INT); 

如果staff_id是一个整数,将工作。

+0

这是一个错字,应该投票结束。该变量在'delete'条件中被覆盖。 – chris85

+0

staff_id是一个INTeger,我添加了“,PDO :: PARAM_INT);”但记录仍然显示在数据库中。 –