Deleting Row from users table PHP MYSQL

Dannad

I would like to delete a row from my users table when the user clicks a button, the user needs to be logged in do delete their own account.

I have echo'd the $user_id which shows '4', which is the correct id for the logged in user, so user_id = $user_id

This is the page that I have which holds the button which I want to delete the users row in the database

<?php
include_once 'dbconfig.php';
if(!$user->is_loggedin())
{
 $user->redirect('index.php');
}
$user_id = $_SESSION['user_session'];

     if(isset($_POST['leave'])){
    $stmt = $DB_con->prepare("DELETE FROM users WHERE user_id = $user_id ");
         $stmt->execute();
    }
$stmt = $DB_con->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css"  />
<link rel="stylesheet" href="style.css" type="text/css"  />
<title>Welcome - <?php print($userRow['user_email']); ?></title>
</head>

<body>

<div class="header">

    <div class="right">
     <label><a href="logout.php?logout=true"><i class="glyphicon glyphicon-log-out"></i> logout</a></label>
    </div>
</div>
<div class="content">

Welcome  <?php print($userRow['user_name']); ?> <br>
<?php print($userRow['team_name']);?><br>
Rank <?php print($userRow['user_rank']); ?> <br> 
<a href="players.php">Players</a>
<a href="teams.php">Teams</a>

<form action='teams.php' method='post'>
<input type='submit' name='leave' value='Delete Profile'/> </form>

<?php echo $user_id?>

</div>
</body>
</html>
jameshwart lopez

I think your problem is your form action(teams.php) which will receive the post data.Your delete code is on the same file and logically $_POST['leave'] will never be set in this page.

Just try to remove your teams.php in your forms action attribute.

<form action='' method='post'>
<input type='submit' name='leave' value='Delete Profile'/> </form>

or in your teams.php file add your delete code

//Make sure you have started the session before using it
$user_id = $_SESSION['user_session'];

if(isset($_POST['leave'])){
    $stmt = $DB_con->prepare("DELETE FROM users WHERE user_id = $user_id ");
         $stmt->execute();
}

Another piece of advise is use parameterize query. Example:

if(isset($_POST['leave'])){
  $stmt = $DB_con->prepare("DELETE FROM users WHERE user_id = ? ");
         $stmt-> bindParam(1,$user_id);
         $stmt->execute();

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"On Delete Cascade" if deleting a row from the child table

From Dev

MySQL update every single row from a 3 million row table

From Dev

Deleting a Row from JQuery Datatables

From Dev

php count and add each values from MySQL table row

From Dev

Preventing users from deleting records in one table that have related records in another table

From Dev

Deleting csv file vales from mysql table using single query

From Dev

Deleting rows from 3 tables in MySQL table

From Dev

MySQL - Latest row by set of users

From Dev

Safely deleting a row from a matrix

From Dev

Deleting row using PHP Mysql

From Dev

PHP button delete row from table not working

From Dev

MySql - Deleting a row

From Dev

Delete selected row from table in mysql

From Dev

Check for empty row from mysql in PHP

From Dev

PHP MySQL Join Users Table to Multiple Columns & Rows

From Dev

Android Deleting Row from SQLITE

From Dev

how to delete the specified row from html table and also in mysql table using php

From Dev

MySQL IN only deleting single row, should delete multiple from variable - PHP

From Dev

Deleting Row from Gridview with SelectedIndexChanged

From Dev

Deleting a row with Php & MySQL

From Dev

Deleting row in table with Jquery in mvc project

From Dev

In Angularjs, how to update table data after deleting a row from it

From Dev

PHP - DataTables Post username from table row for a mysql_query?

From Dev

UITableView deleting row reduced table row height

From Dev

Deleting a row based on a float column in Mysql/PHP?

From Dev

Deleting a ROW from MySQL database using MySQLi

From Dev

Deleting more than 1 entry from a checkbox php and mysql

From Dev

php/pdo/mysql accessing selected row from a select * in html table

From Dev

Multiple insert in MySQL table from PHP table

Related Related

  1. 1

    "On Delete Cascade" if deleting a row from the child table

  2. 2

    MySQL update every single row from a 3 million row table

  3. 3

    Deleting a Row from JQuery Datatables

  4. 4

    php count and add each values from MySQL table row

  5. 5

    Preventing users from deleting records in one table that have related records in another table

  6. 6

    Deleting csv file vales from mysql table using single query

  7. 7

    Deleting rows from 3 tables in MySQL table

  8. 8

    MySQL - Latest row by set of users

  9. 9

    Safely deleting a row from a matrix

  10. 10

    Deleting row using PHP Mysql

  11. 11

    PHP button delete row from table not working

  12. 12

    MySql - Deleting a row

  13. 13

    Delete selected row from table in mysql

  14. 14

    Check for empty row from mysql in PHP

  15. 15

    PHP MySQL Join Users Table to Multiple Columns & Rows

  16. 16

    Android Deleting Row from SQLITE

  17. 17

    how to delete the specified row from html table and also in mysql table using php

  18. 18

    MySQL IN only deleting single row, should delete multiple from variable - PHP

  19. 19

    Deleting Row from Gridview with SelectedIndexChanged

  20. 20

    Deleting a row with Php & MySQL

  21. 21

    Deleting row in table with Jquery in mvc project

  22. 22

    In Angularjs, how to update table data after deleting a row from it

  23. 23

    PHP - DataTables Post username from table row for a mysql_query?

  24. 24

    UITableView deleting row reduced table row height

  25. 25

    Deleting a row based on a float column in Mysql/PHP?

  26. 26

    Deleting a ROW from MySQL database using MySQLi

  27. 27

    Deleting more than 1 entry from a checkbox php and mysql

  28. 28

    php/pdo/mysql accessing selected row from a select * in html table

  29. 29

    Multiple insert in MySQL table from PHP table

HotTag

Archive