PDO PhP - 从选择ID中删除查询

时间:2016-01-05 11:43:55

标签: php sql pdo

我正试图这样做,当我点击“x”时它会删除整行。 我已将每条记录链接到我的jobRef但是删除无效。

这是我到目前为止所得到的;

<?php
$status = 'available';
$stmt = $pdo->query('SELECT * FROM jobs WHERE jobStatus = "' . $status . '"');
$results = $stmt->fetchAll();

echo "<table><tr><td>Job Reference</td><td>Description</td>";
foreach ($results as $row) {
  echo "<tr><td>".$row['jobRef']."</td>","<td>".$row['jobDescription']."</td>";
  echo "<td><a href='edit.php?id=".$row['jobRef']."'>Edit</a></td>";
?>

继承我的delete.php

<?php
require 'mysqlcon.php';

?>

<?php

if(isset($_GET['id']))
{
$id=$_GET['id'];
$query1= ("DELETE FROM person WHERE id='$id'");
if($query1)
{
header('location:Vacancies.php');
}
}
?>

1 个答案:

答案 0 :(得分:2)

您只需编写查询,忘记执行它。

$query1= ("DELETE FROM person WHERE id='$id'");

你需要执行它

$pdo->query("DELETE FROM person WHERE id='$id'");

或者更好地使用绑定语句

$sth =$pdo->prepare('DELETE FROM person WHERE id=:id');
    $sth->bindValue(':id', $id, PDO::PARAM_INT);
    $sth->execute();
    $count = $sth->rowCount();
    if($count>0)
    {
        header('location:Vacancies.php');
    }else{
        echo "Error in delete";
    }
相关问题