Page 1 of 1

[SOLVED] delete user query

Posted: Mon Aug 02, 2004 8:14 pm
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.

Posted: Mon Aug 02, 2004 8:35 pm
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());

Posted: Mon Aug 02, 2004 8:51 pm
by g3ckO
The query is: DELETE FROM employee WHERE username = ''

Posted: Mon Aug 02, 2004 9:03 pm
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.

Posted: Mon Aug 02, 2004 9:08 pm
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.

Posted: Mon Aug 02, 2004 9:52 pm
by g3ckO
what should I put here: if($_POST['del'])

Posted: Mon Aug 02, 2004 11:27 pm
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.

Posted: Tue Aug 03, 2004 12:42 am
by g3ckO
Always forgot about the hidden field :)

TQ.. Problem solved..