I want the user to be able to remove their account from the database, this will be done by the click of a button in my php forum, see code below.
<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>
I already have the database connection setup
<?php
$connection = mysql_connect('localhost', 'root', 'Oliver');
mysql_select_db('users');
$query = "SELECT * FROM username WHERE username='$login_session'";
$result = mysql_query($query);
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'];
}
mysql_close();
?>
The database row will be identified by the php variable UserNameID which is from the database
$UserNameID = $row['UserNameID'];
If you need any more info please just drop me a response, i'm quite new to coding so cooperation will be needed :D Thanks ever so much
Update Instead of disliking please tell me what i'm doing wrong, that would be hugely appreciated
Update
This is the session code
<?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
}
?>
This just logs the user in if details are match in the database and starts a session to unlock other pages
I think this will move you bit closer to your goal.
<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();
?>
Edited According to OP request
<?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();
?>