如何使功能从db中删除数据

时间:2015-11-20 10:00:24

标签: php function sql-delete

任何人都可以告诉我,如何在PHP中创建这个查询的功能,这就是为什么每当我需要它时我只需要调用一个函数来删除记录。我是编码新手。请建议最简单的代码来为此编写函数。

<?php
if (isset($_GET['delete']))
{
    $query= "DELETE FROM student WHERE id='".$_GET['delete']."'";
    global $db;

   $row = $db->exec($query);
}

$query= "SELECT * FROM student";

global $db;
$statement = $db->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$result = $result;
?>

1 个答案:

答案 0 :(得分:0)

试试这个:     

if (isset($_GET['delete']))
{
    deleteStudent($_GET['delete']);
}

$query= "SELECT * FROM student";

global $db;
$statement = $db->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$result = $result;

function deleteStudent($id) {
    global $db;
    $query= "DELETE FROM student WHERE id='".$id."'";
    $row = $db->exec($query);
    return $row; // expects it return the succes/failure flags properly.
} ?>