2017-04-04 22 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);」但記錄仍然顯示在數據庫中。 –