使用php删除mysql中的记录

时间:2013-09-22 01:55:33

标签: php mysql record sql-delete

我无法想出这个。我环顾四周,在这里和其他地方,他们似乎都在一个非常圆的方式解决这个问题。我已经创建了一个删除成员的页面。用户键入用户名,单击选择并从数据库中检索特定记录。然后,用户可以单击删除按钮并删除用户。我遇到的问题是删除部分不起作用。这是html:

<?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.';
  }

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

我不知道参数的位置是什么。

param&#39;不存在,所以,你不需要绑定它们。