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
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Mon Aug 02, 2004 8:14 pm
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.
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Mon Aug 02, 2004 8:35 pm
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());
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Mon Aug 02, 2004 8:51 pm
The query is: DELETE FROM employee WHERE username = ''
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Mon Aug 02, 2004 9:03 pm
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.
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Mon Aug 02, 2004 9:08 pm
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.
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Mon Aug 02, 2004 9:52 pm
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 » Mon Aug 02, 2004 11:27 pm
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.
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Tue Aug 03, 2004 12:42 am
Always forgot about the hidden field
TQ.. Problem solved..