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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.