使用MySQLi从MySQL数据库中删除ROW

时间:2016-01-28 12:48:33

标签: php mysql mysqli

我有一个带有是/否按钮的delete.php文件,调用deleteRecord.php从数据库中删除所选行。

问题似乎是我没有将ProjectID的变量传递给 deleteRecord 文件。

有人可以告诉我有什么问题吗?

delete.php

<?php 
error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true);
require('includes/conn.inc.php');
require('includes/functions.inc.php');
$sProjectID = safeInt($_GET['ProjectID']);
$stmt = $mysqli->prepare("SELECT ProjectID, ProjectName, ProjectImage, LanguageUsed, ApplicationUsed, Description FROM Projects WHERE ProjectID = ?");
$stmt->bind_param('i', $sProjectID);
$stmt->execute(); 
$stmt->bind_result($ProjectID, $ProjectName, $ProjectImage, $LanguageUsed, $ApplicationUsed, $Description);
$stmt->fetch();
$stmt->close();
?>
<!DOCTYPE HTML>
<input name="ProjectID" type="hidden" value="<?php echo
$ProjectID; ?>">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Delete <?php echo $ProjectName; ?></title>
<link href="styles/cms.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
<header>
<h1>Delete <?php echo $ProjectName; ?></h1>
<?php
require('includes/nav.inc.php');
?><p>Hello </p><?php echo "$sProjectID" ?>
</header>

<form name="form1" method="get" action="process/deleteRecord.php">
<p>Are you sure you wish to delete <?php echo $ProjectName; ?>?</p>
   <p>
    <input type="submit" name="del" id="del" value="Delete">
  </p>
    </form>

<form name="form2" method="" action="listall.php" id="saveForm">
    <input type="submit" name="save" id="save" value="Save">
</form>

<?php
require('includes/footer.inc.php');
?>

</div>
    </body>
    </html>

deleteRecord.php

<?php
error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true);
require('../includes/conn.inc.php');
require('../includes/functions.inc.php');
// sanitize user variables
$sProjectID = safeInt($_POST['ProjectID']);
// prepare SQL
$stmt = $mysqli->prepare("DELETE FROM Projects WHERE ProjectID = ?");
$stmt->bind_param('i', $sProjectID);
$stmt->execute();
$stmt->close();
//header("Location: ../index.php");
// redirect browser
exit;
// make sure no other code executed
?>

2 个答案:

答案 0 :(得分:5)

您需要在表单中编写隐藏字段,并将方法更改为POST

<form name="form1" method="post" action="process/deleteRecord.php">
    <p>Are you sure you wish to delete <?php echo $ProjectName; ?>?</p>
    <p>
        <input name="ProjectID" type="hidden" value="<?php echo
$ProjectID;
?>">
        <input type="submit" name="del" id="del" value="Delete">
    </p>
</form>

根据下面的@VolkerK评论,将输入元素放在表单元素“内”而不是<html>标记之前。

答案 1 :(得分:0)

这使用GET方法做同样的事情:

删除delete.php:

<form name="form1" method="get" action="process/deleteRecord.php?ProjectID=<?= $ProjectID ?>">
    <p>Are you sure you wish to delete <?php echo $ProjectName; ?>?</p>
    <p>
        <input type="submit" name="del" id="del" value="Delete">
    </p>
</form>

deleteRecord.php中的更改:

$sProjectID = safeInt($_GET['ProjectID']);