[SOLVED] delete user query

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
User avatar
g3ckO
Forum Contributor
Posts: 117
Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:

[SOLVED] delete user query

Post by g3ckO »

What is wrong with this code.

Code: Select all

<?php
<?

include("database.php");
include("login.php");

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', 'db1');


    $u_username = $_POST['u_username'];


?>
	<form action = "" method = "post">
	<center>Are You sure you want to delete <? echo $u_username; ?> record from database?
	<br><br>
	<input type = "submit" value = "DELETE" name = "del">
	</form>
	<form action = "viewuser.php" method = "post">
	<input type = "submit" value = "CANCEL" name = "can">
	</form>

<?
if($_POST['del'])
{	 	

    global $conn;

    $query = "DELETE FROM employee WHERE username = '$u_username'";

	mysql_query($query, $conn);
?>	
	<font color="#671247" size="3" face="Verdana, Arial, Helvetica, sans-serif">
	<br><br><br><center><h3>RECORD SUCCESSFULLY REMOVED.<h3></center>	
	<form action="viewuser.php" method="post">
   	<center><input type="submit" value="BACK" name="back"></center></form>

<?}?>
	
?>
When I click the DELETE button, I get the message RECORD SUCCESSFULLY REMOVED but the data is still in the database.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Debug it:

Code: Select all

$query = "DELETE FROM employee WHERE username = '$u_username'";
echo 'The query is: '.$query.'<br />';
mysql_query($query, $conn) or die(mysql_error());
User avatar
g3ckO
Forum Contributor
Posts: 117
Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:

Post by g3ckO »

The query is: DELETE FROM employee WHERE username = ''
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Then $_POST['u_username']; probably isn't set.
Putting an error_reporting(E_ALL); as the first line of the script should catch things like that.
User avatar
g3ckO
Forum Contributor
Posts: 117
Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:

Post by g3ckO »

I can print the value in this line: Are You sure you want to delete <? echo $u_username; ?> record from database?
so how can I past the value '$u_username' to the query.
User avatar
g3ckO
Forum Contributor
Posts: 117
Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:

Post by g3ckO »

what should I put here: if($_POST['del'])
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You have $u_username = $_POST['u_username']; when you first show the form - as $_POST['del'] isnt set at this point, but when you click the button to submit the form to say you do want to delete it you dont pass u_username as a field, you just need to add it as a hidden field.
User avatar
g3ckO
Forum Contributor
Posts: 117
Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:

Post by g3ckO »

Always forgot about the hidden field :)

TQ.. Problem solved..
Post Reply