Jump to content

[SOLVED] How to execute sql code in php


steviez

Recommended Posts

Hi,

 

I need the following code to be executed when a user clicks on the delete button:

 

<?php
if(!$UsErId)

{

$redirect = $SITEURL."login.php";

header("Location:$redirect");

}

  session_start();
  if (isset($_SESSION['$UsErId'])) {
    $sql = "DELETE FROM user WHERE id = '{$_SESSION['$UsErId']}'";
    if (mysql_query($sql)) {
      $redirect = $SITEURL."logout.php";
      header("location:$redirect");
    } else {
      echo "Delete failed";
    }
  }
?>

 

How is this done? The button when pressed will delete a user from the database.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/37642-solved-how-to-execute-sql-code-in-php/
Share on other sites

something like

<?php

if	(isset($_POST['delete']))
{
$delete = $_POST['delete'];

if	($delete == "yes")
	{
	$sql = "DELETE FROM user WHERE id = '{$_SESSION['$UsErId']}'";
	if (mysql_query($sql)) 
		{
	  $redirect = $SITEURL."logout.php";
	  header("location:$redirect");
		} 
	else
		{
		 echo "Delete failed";
		}

	}
else
	{
	echo "You chose not to delete the record";
	}
}

?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input name="delete" type="submit" value="yes" />
<input name="delete" type="submit" value="no" />
</form>

I wrote it quite quickly so there may be some minor errors.

Thank you that worked a treat! i dont suppose anyone could help me with the next problem in this puzzle?

 

When the user is deleted from the database i am wanting all their images, uploads etc to be deleted at the same time. Is this possable and if so how?

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.