如何使用我的php变量UserNameID删除用户';数据库中的行
我希望用户能够从数据库中删除他们的帐户,这将通过单击我的php论坛中的按钮来完成,请参见下面的代码如何使用我的php变量UserNameID删除用户';数据库中的行,php,html,mysql,Php,Html,Mysql,我希望用户能够从数据库中删除他们的帐户,这将通过单击我的php论坛中的按钮来完成,请参见下面的代码 <div class="btn-group"> <button style="width:200px" type="button" class="btn btn-primary">Change details</button> <button style="width:200px" type="button" class="btn b
<div class="btn-group">
<button style="width:200px" type="button" class="btn btn-primary">Change details</button>
<button style="width:200px" type="button" class="btn btn-primary">Add details</button>
<button onclick="myFunction()" style="width:200px" type="button" class="btn btn-primary">Delete details</button>
</div>
如果你需要更多的信息,请给我一个回复,我对编码很陌生,所以需要合作:D非常感谢
更新
与其不喜欢,不如告诉我我做错了什么,我将不胜感激
更新
这是会话代码
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "Oliver");
// Selecting Database
$db = mysql_select_db("users", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select username from username where username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: home.php'); // Redirecting To Home Page
}
?>
我想这会让你更接近你的目标
<div class="btn-group">
<button style="width:200px" type="button" class="btn btn-primary">Change details</button>
<button style="width:200px" type="button" class="btn btn-primary">Add details</button
<form action="" method="post">
<button type="submit" style="width:200px" class="btn btn-primary">Delete details</button>
<input type="hidden" name="UserName id="UserName" value="<?php echo $login_session;?>" />
</form>
</div>
<?php
$connection = mysql_connect('localhost', 'root', 'Oliver');
mysql_select_db('users');
$query = "SELECT * FROM username WHERE username='$login_session'";
$result = mysql_query($query);
//By the way you don't need loop here
while($row = mysql_fetch_array($result)){
$name = $row['name'];
$username = $row['username'];
$sex = $row['sex'];
$phone = $row['phone'];
$email = $row['email'];
$dob = $row['dob'];
$image = $row['imagelink'];
$hobby = $row['hobby'];
$bio = $row['Bio'];
$Level = $row['Level'];
$UserNameID = $row['UserNameID'];
}
//Here you delete record from database
if(isset($_POST['UserName'])){
$user = mysql_real_escape_string($_POST['UserName']);
//Strongly suggest that use UserId to delete record not username
$sql = "DELETE FROM username WHERE username='$user' LIMIT 1";
}
mysql_close();
?>
更改详细信息
添加详细信息您的onclick=“myFunction()”
代码在哪里?您面临什么问题???$login\u会话的值是多少???用户ID???然后只需删除配置文件。。。。很简单…谢谢你的回复,我正在努力弄清楚我是如何设置一个按钮来删除数据库中带有UserNameID的行的。我还没有设置@shehary,我不知道在里面放什么为什么要遍历用户的表?您可能还应该确保正在删除的用户是登录的用户。可以构造请求以删除其他用户帐户。还取决于您处理该请求的方式,可能会导致SQL注入。一定要使用预先准备好的语句。好吧,我只是100%的困惑:D我对编码非常陌生,我知道问题出在引导模式上,你想让我创建一个新的php文档,你想让我在其中放什么,我应该保存什么?给我一点时间,我会创建另一个PasteBins直到不起作用,想把我添加到facebook上,它更容易聊天,几乎可以在最后一次尝试添加ob_start()
在您的delete.php文件中进行更改后仍然不工作:/what's you means您能在输入的引导模式中看到用户id吗?是的,我能在delete details按钮旁边看到UserNameID值吗
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "Oliver");
// Selecting Database
$db = mysql_select_db("users", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select username from username where username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: home.php'); // Redirecting To Home Page
}
?>
<div class="btn-group">
<button style="width:200px" type="button" class="btn btn-primary">Change details</button>
<button style="width:200px" type="button" class="btn btn-primary">Add details</button
<form action="" method="post">
<button type="submit" style="width:200px" class="btn btn-primary">Delete details</button>
<input type="hidden" name="UserName id="UserName" value="<?php echo $login_session;?>" />
</form>
</div>
<?php
$connection = mysql_connect('localhost', 'root', 'Oliver');
mysql_select_db('users');
$query = "SELECT * FROM username WHERE username='$login_session'";
$result = mysql_query($query);
//By the way you don't need loop here
while($row = mysql_fetch_array($result)){
$name = $row['name'];
$username = $row['username'];
$sex = $row['sex'];
$phone = $row['phone'];
$email = $row['email'];
$dob = $row['dob'];
$image = $row['imagelink'];
$hobby = $row['hobby'];
$bio = $row['Bio'];
$Level = $row['Level'];
$UserNameID = $row['UserNameID'];
}
//Here you delete record from database
if(isset($_POST['UserName'])){
$user = mysql_real_escape_string($_POST['UserName']);
//Strongly suggest that use UserId to delete record not username
$sql = "DELETE FROM username WHERE username='$user' LIMIT 1";
}
mysql_close();
?>
<?php
$connection = mysql_connect('localhost', 'root', 'Oliver');
mysql_select_db('users');
if(isset($_POST['UserName'])){
$user = mysql_real_escape_string($_POST['UserName']);
$query = "SELECT * FROM username WHERE username='$user'";
$result = mysql_query($query);
$total = mysql_num_rows($result);
$row = mysql_fetch_array($result);
// You dont need to fetch all records just `$UserNameID` enough
$name = $row['name'];
$username = $row['username'];
$sex = $row['sex'];
$phone = $row['phone'];
$email = $row['email'];
$dob = $row['dob'];
$image = $row['imagelink'];
$hobby = $row['hobby'];
$bio = $row['Bio'];
$Level = $row['Level'];
$UserNameID = $row['UserNameID'];
//Here you delete record from database
if($total>0){
$sql = "DELETE FROM username WHERE UserNameID='$UserNameID' LIMIT 1";
}
}
mysql_close();
?>