2016-01-28 59 views
2

我有一個delete.php文件,其中包含yes/no按鈕,用於調用deleteRecord.php從數據庫中刪除選定的行。使用MySQLi刪除MySQL數據庫中的行

問題似乎是我沒有將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 
?> 
+1

吹毛求疵:沒有Mysqli數據庫。 [mysqli](http://docs.php.net/mysqli)只是與MySQL服務器交互的php-apis之一。 – VolkerK

回答

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>標記之前的一個「之內」。

+2

只是爲了說明:將'input'元素放置在''標籤之前的表單元素_instead_中。 – VolkerK

+0

謝謝你。工作過一種享受。 –

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']);