使用php删除mysql中的记录
我搞不懂这个。我环顾了四周、这里和其他地方,他们似乎都在以一种非常全面的方式处理这项工作。我已经创建了一个用于删除成员的页面。用户输入用户名,单击select,然后从数据库中检索特定记录。然后,用户可以单击删除按钮并删除该用户。我的问题是删除部分不起作用。以下是html:使用php删除mysql中的记录,php,mysql,record,sql-delete,Php,Mysql,Record,Sql Delete,我搞不懂这个。我环顾了四周、这里和其他地方,他们似乎都在以一种非常全面的方式处理这项工作。我已经创建了一个用于删除成员的页面。用户输入用户名,单击select,然后从数据库中检索特定记录。然后,用户可以单击删除按钮并删除该用户。我的问题是删除部分不起作用。以下是html: <?php require_once('../include/officer_session_timeout_db.inc.php'); if (isset($_POST['viewmember'])) { $user
<?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.';
}
使用“?”标记要绑定的参数。试试这个:
$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文档了解更多信息和示例:我看不到参数的位置
参数“s”不存在,因此您不需要绑定它们。谢谢您提供的信息。我用上面的“?”进行了尝试,并删除了所有$stmt代码。我尝试过使用实际数据而不是变量,但它从来都不起作用。它总是返回我的错误,即数据库出现问题。您能否使用?
占位符用修改后的代码更新您的问题,并描述您收到的错误消息?它没有给我错误。我想发生的是,我给同一个变量分配了多个变量。我重新修改了它,这样成员就不会真正被删除。相反,它会更改登录名。例如,“username”当“deleted”变为“username\u deleted”时,由于成员来来去去去,将其重新添加是一件痛苦的事情。新用户名对于用户来说是未知的,据他们所知,它们并不存在。在这个过程中,我基本上重命名了一些变量,并将“DELETE”更改为“UPDATE”,现在就可以使用了。再次感谢你帮助这个可怜的新手:)