Deleting a user in php, but the name dosent delete in db
Posted: Sat Jan 01, 2011 5:44 pm
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');
?>
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');
?>