我无法使用php从mysql中删除记录

时间:2017-04-04 00:15:14

标签: php mysql sql

我一直在创建一个人员目录web应用程序来学习和测试我学到的东西,但我有问题从我的mysql数据库中删除上一页的staff_id = staff_id的记录,它会重新记录并填充字段但是当我选择是时,只是不删除记录。

任何帮助或指导都会很棒: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>

3 个答案:

答案 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

答案 1 :(得分:0)

将表单方法从post更改为get

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

答案 2 :(得分:0)

难道这很容易吗?

变化:

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

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

如果staff_id是一个整数,那将会有效。