Deleting a user in php, but the name dosent delete in db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
masoman
Forum Newbie
Posts: 1
Joined: Sat Jan 01, 2011 5:39 pm

Deleting a user in php, but the name dosent delete in db

Post by masoman »

Hi,

I am trying to delete a user from a database list viewed in a php script.
For example when i click on the delete button the user should be deleted but it stil shows in the database. Also it says User deleted but it really didnt get deleted
here is my code
Thanks

<?php
$page_title = 'Delete a User';

include('header.html');

echo '<h1>Delete a User</h1>';


if((isset($_GET['id'])) && (is_numeric($_GET['id'])))
{
//from view_users
$id = $_GET['id'];
}

else if((isset($_POST['id'])) && (is_numeric($_POST['id'])))
{
$id = $_POST['id'];
}


/*
else
{
echo '<p class="error">This page has been accessed in errorrrr. </p>';
include('footer.html');
exit();
}
*/

require_once('mysql_connect.php');



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

if($_POST['sure'] == 'Yes')
{

$q = mysql_query("DELETE FROM users WHERE user_id='$id' ");
$result = $q;


if($result)
{
echo '<p>The user has been deleted. </p>';
}

else
{
echo '<p class = "error">The user could not be deleted. </p>';
echo '<p>' . mysql_error() . '</p>';
}

}//close sure YES



else //this is from if sure, no confirmation of deletion
{
echo '<p>The user has NOT been deleted. </p>';
}


} //close submit



else//coming from if submit else form
{
$q = mysql_query("SELECT CONCAT(last_name, ', ', first_name) FROM users WHERE user_id=$id ");
$result = $q;

//if($result)
if($result)
{
//Get the users info
$row = mysql_fetch_array($result);

echo '<form action = "delete_user.php" method = "post">
<h3>Name: ' . $row[0] . ' </h3>

<p>Are you sure you want to delete this user?<br/>

<input type="radio" name = "sure" value = "Yes" /> Yes
<input type = "radio" name = "sure" value = "No" checked = "checked" />No</p>

<p><input type = "submit" name = "submit" value = "Submit" /> </p>

<input type = "hidden" name = "submit" value = "TRUE" />

<input type = "hidden" name = "id" value = " ' . $id . ' " />
</form>';

}


else//Not a valid user ID
{
echo '<p class = "error">This page has been accessed in error. </p> ';
}



}//end of the main submission conditional

mysql_close($connect);

include('footer.html');




?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Deleting a user in php, but the name dosent delete in db

Post by social_experiment »

Move the code where you set the values of $id after you have checked if the button has been set and see what happens.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Deleting a user in php, but the name dosent delete in db

Post by Darhazer »

var_dump($id);

DELETE which do not found any rows will still return true;
Post Reply