user871258
user871258

Reputation:

Delete From Table Name Stored As Variable?

After the user logs out, it basically deletes the all the data in their table that includes their id code in any of the rows.

$idcode = $_SESSION['idcode'];
$idicao = $_SESSION['idicao'];

if(isset($_POST['logout'])) {

$sql = "DELETE FROM $idicao WHERE idcode=".$idcode."";

mysql_query($sql);

}


session_unset();
session_destroy();

mysql_close($dbid);

header("Location: login.php");

?>

The variables are echoed on the page correctly, and session_start is at the top. The only problem is that the records from the tables are not being deleted.

Upvotes: 0

Views: 6026

Answers (4)

Adam Winter
Adam Winter

Reputation: 1934

$query = 'DELETE FROM ? WHERE idcode = ?';
$stmt = $db->prepare($query);
$stmt->bind_param('ss', $idicao, $idcode);
$stmt->execute();
if (mysqli_connect_errno()) {
                              $obj->error = 'Error: ...your error msg...';
                              echo json_encode($obj);
                              exit;
}

Upvotes: 0

kasper Taeymans
kasper Taeymans

Reputation: 7026

Try this:

$idicao='yourtablename';   
$sql = 'DELETE FROM '.$idicao.' WHERE idcode='.$idcode;

Upvotes: 2

Dennefyren
Dennefyren

Reputation: 344

You are saying that the sql-variable outputs: DELETE FROM Test WHERE idcode=test

Test should be in single quotes like this:

$sql = "DELETE FROM ".$idicao." WHERE idcode='".$idcode."'";

Upvotes: 1

MWard
MWard

Reputation: 135

change this line:
$sql = "DELETE FROM $idicao WHERE idcode=".$idcode."";

to

$sql = "DELETE FROM ".$idicao." WHERE idcode=".$idcode.";

and you should be good to go

Upvotes: 2

Related Questions