2013-09-22 130 views
-1

我無法弄清楚這一點。我環顧四周,在這裏和其他地方,他們似乎都圍繞着這個方向來解決這個問題。我創建了一個刪除成員的頁面。用戶鍵入用戶名,點擊select並從數據庫中檢索特定記錄。用戶然後可以單擊刪除按鈕並刪除用戶。我遇到的問題是刪除部分不起作用。這裏是html:使用php刪除mysql中的記錄

<?php 
require_once('../include/officer_session_timeout_db.inc.php'); 
if (isset($_POST['viewmember'])) { 
$username = trim($_POST['username']); 
require_once('../include/viewmember.inc.php'); 
} 
?> 

上面的部分工作正常。這只是刪除部分不起作用。

<?php 
if (isset($_POST['delete'])) { 
    require_once('../include/deletemember.inc.php'); 
} 
?> 



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
</head> 

<body> 



<div id="MainContentBox"> 




    <Div id="MainContentTitle">DELETE A MEMBER!!!!<br> 


      <div id="MainContentText"> 



      <form id="viewmember" method="post" action=""> 

User Name: <input type="text" name="username" id="username"> 
<input name="viewmember" type="submit" id="viewmember" value="Search"> 

</form> 

<?php echo $firstname;?> <?php echo $lastname;?>   

      <fieldset style="width:500px"> 
<legend>Address</legend> 
<?php echo $address;?><br> 
<?php echo $city;?><br> 
<?php echo $state;?> , <?php echo $zip;?> 

</fieldset> 
<fieldset style="width:500px"> 
<legend>Contact Information</legend> 
E-Mail:<a href="mailto:<?php echo $email;?>"><?php echo $email;?></a> <br> 
Home Phone: <?php echo $homephone;?><br> 
Cell Phone: <?php echo $cellphone;?><br> 
</fieldset> 

<?php 
if (isset($success)) { 
echo "<p>$success</p>"; 
} elseif (isset($errors) && !empty($errors)) { 
echo '<ul>'; 
foreach ($errors as $error) { 
echo "<li>$error</li>"; 
} 
echo '</ul>'; 
} 
?> 

<form id="delete" method="post" action=""> 

<input name="delete" type="submit" id="delete" value="DELETE MEMBER!!!!"> 
</form> 
     </div> 
      </div> 

    </body> 

,這裏是爲deletemember.inc.php文件的代碼: '?'

<?php 
require_once('connection.inc.php'); 
    $conn = dbConnect('write'); 
    // prepare SQL statement 
$sql = "DELETE FROM members WHERE username='$username'"; 
    $stmt = $conn->stmt_init(); 
    $stmt = $conn->prepare($sql); 
    // bind parameters and insert the details into the database 
    $stmt->bind_param('s', $username); 
    $stmt->execute(); 
if ($stmt->affected_rows == 1) { 
    $success = "Your information has been updated."; 
    } else { 
    $errors[] = 'Sorry, there was a problem with the database.'; 
    } 

回答

1

使用標記要綁定的參數。試試這個:

$sql = "DELETE FROM members WHERE username=?"; 
$stmt = $conn->stmt_init(); 
$stmt = $conn->prepare($sql); 
// bind parameters and insert the details into the database 
$stmt->bind_param('s', $username); 

的mysqli需要知道查詢你想要的參數綁定,所以你標記了?每個替換。查看bind_param文檔以獲取更多信息和示例:http://php.net/manual/en/mysqli-stmt.bind-param.php

+0

感謝您的信息。我用「?」試過了像上面那樣,我已經通過刪除所有的$ stmt代碼來嘗試它。我已經通過使用實際數據而不是變量來嘗試它,並且它永遠不會工作。它始終返回我的錯誤,即數據庫出現問題。 – Type540

+0

你可以使用'?'佔位符更新你的問題與修改後的代碼,並描述你得到什麼錯誤消息? –

+0

這不是給我一個錯誤。我認爲發生了什麼,是我將多個事物分配給同一個變量。我重寫了它,所以成員並沒有真正刪除。相反,它會更改登錄名稱。例如,「刪除」時的'用戶名'變成'username_deleted'由於成員來去,將它們添加回去會很痛苦。新用戶名對於用戶是未知的,據他們所知,他們不存在。在這樣做的過程中,我基本上重命名了一些變量,並將「DELETE」更改爲「UPDATE」,現在它可以工作。再次感謝您試圖幫助這個可憐的新手出來:) – Type540

1

我看不到參數的位置在哪裏。

參數'不存在,所以,你不需要綁定它們。