Delete script does not want to work

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1591 Views - Last Post: 06 February 2011 - 06:08 PM Rate Topic: -----

#1 mdmartiny  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 28-May 10

Delete script does not want to work

Posted 06 February 2011 - 01:54 AM

Hello Everyone.

I am trying to figure out why this delete script does not want to work. I click on the delete user and all it does it take me back to the page to pick a person to delete from the database.

When I run the sql in the database it deletes the user with no problem

This is the show_user_del.php page. This is the verification page to make sure you are deleting the right person
<?php

if (!$_POST[id]) {
	header ("LOCATION: pick_user.php");
	exit;
}

require('../includes/auth_user.php');

//build and issue query
$sql = "SELECT * FROM $table WHERE id = '$_POST[id]'";
$result = mysql_query($sql, $connection) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
	$id = $row['id'];
	$f_name = $row['f_name'];
	$l_name = $row['l_name'];
	$username = $row['username'];
	$password = $row['password'];
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Classified Ad</title>
</head>
<body>
<h2><em>Delete user from Database</em></h2>
<h3>User being deleted <?php echo "$f_name $l_name"; ?></h3>
<form method="POST" action="do_delete_user.php">
<input type="hidden" name="id" value="<?php echo "$_POST[id]"; ?>" />
<input type="hidden" name="f_name" value="<?php echo "$f_name"; ?>" />
<input type="hidden" name="l_name" value="<?php echo "$l_name"; ?>" />
<p> <strong>Name:</strong> <?php echo "$f_name $l_name"; ?>
   </p>
   <p> <strong>Username:</strong> <?php echo "$username"; ?>
   </p>
   <p> <strong>Password:</strong> <?php echo "$password"; ?>
   </p>
   <p>
      <input type="submit" name="submit" id="name" value="Delete User" />
   </p>
</form>
<p><a href="../admin_menu.php">Return to Administration Menu</a></p>
</body>
</html>



Here is the do_delete_user.php page. Obviously the page that actually does the deleting. As I mentioned before all it does is reroute back to the pick_user.php page. without removing the person from the database
<?php

if (!$_POST[id]) {
	header ("LOCATION: pick_user.php");
	exit;
}
require('../includes/auth_user.php');

$sql = "DELETE FROM $table WHERE id='$_POST[id]'";
$result = mysql_query($sql, $connection) or die(mysql_error());
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<h1>User has been removed</h1>
<?php echo "$sql"; ?><?php echo "$result"; ?>
<h2><em>User <?php echo "$_POST[f_name] $_POST[l_name]"; ?> has been deleted
      from the <?php echo "$table"; ?> table</em></h2>
<p><a href="pick_user.php">Delete another person</a></p>
<p><a href="../admin_menu.php">Administration Menu</a></p>
</body>
</html>


This post has been edited by mdmartiny: 06 February 2011 - 01:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Delete script does not want to work

#2 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 922
  • View blog
  • Posts: 3,195
  • Joined: 19-January 10

Re: Delete script does not want to work

Posted 06 February 2011 - 02:09 AM

Are you connected to the database? You need a connection to run a query. Are there any errors?

Oh wait,

change $_POST[id] to $_POST['id'] on line 3.
Was This Post Helpful? 1
  • +
  • -

#3 mdmartiny  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 28-May 10

Re: Delete script does not want to work

Posted 06 February 2011 - 02:22 AM

View Postcreativecoding, on 06 February 2011 - 09:09 AM, said:

Are you connected to the database? You need a connection to run a query. Are there any errors?

Oh wait,

change $_POST[id] to $_POST['id'] on line 3.



The connection to the database is in the include files...

That suggestion did not work
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: Delete script does not want to work

Posted 06 February 2011 - 04:35 AM

If I had to guess, your problem is here:

$sql = "DELETE FROM $table WHERE id='$_POST[id]'";


Try this instead:

$sql = "DELETE FROM $table WHERE id='" . $_POST['id'] . "'";

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: Delete script does not want to work

Posted 06 February 2011 - 05:59 AM

*
POPULAR

Again, it comes down to this. Please...LEARN THIS AND USE IT whenever you're running a query you build from variables:

$sql = "DELETE FROM $table WHERE id='$_POST[id]'";
$result = mysql_query($sql, $connection) or die("Query $sql failed: " . mysql_error());



When you're in the development stages, PRINT OUT THE QUERY ON FAILURE!!! This is BASIC DEBUGGING! Learn to do this if you want to develop software. When you move to deploy this to the real world, then you use error logging to log the error for troubleshooting, rather than dying and printing the error so the user gets a glimpse into your database structure.

Also, you're wide open for SQL injection.

Consider using PDO and prepared statements, as Dormilich so often suggests. There's a tutorial in our tutorials section on this.
Was This Post Helpful? 5
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3394
  • View blog
  • Posts: 9,599
  • Joined: 08-June 10

Re: Delete script does not want to work

Posted 06 February 2011 - 07:23 AM

I couldn’t help giving rep++ for this, Jack. ;)
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Delete script does not want to work

Posted 06 February 2011 - 09:07 AM

In addition to the problems previously mentioned, I have these questions for you:
  • Why are you using a while loop?
  • How does moving a value from the post variable to another variable help you? :crazy:
  • Why are you mixing php and html? You break out of php 8 times in only 49 lines of code. That's almost once every 6 lines! Do you expect to be able to read your own code when it's a couple of hundred lines long and you haven't seen it in six months? :eek:

Was This Post Helpful? 0
  • +
  • -

#8 mdmartiny  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 28-May 10

Re: Delete script does not want to work

Posted 06 February 2011 - 09:54 AM

View PostValek, on 06 February 2011 - 11:35 AM, said:

If I had to guess, your problem is here:

$sql = "DELETE FROM $table WHERE id='$_POST[id]'";


Try this instead:

$sql = "DELETE FROM $table WHERE id='" . $_POST['id'] . "'";


I tried this and it still just redirects me back to the pick user page


View PostJackOfAllTrades, on 06 February 2011 - 12:59 PM, said:

Again, it comes down to this. Please...LEARN THIS AND USE IT whenever you're running a query you build from variables:

$sql = "DELETE FROM $table WHERE id='$_POST[id]'";
$result = mysql_query($sql, $connection) or die("Query $sql failed: " . mysql_error());



I have also added the error coding to that section of the script and it is not showing any error codes. Just redirects back to the pick user page. I am also looking into adding error coding to the pages. So, I will be looking at the tutorial that you have suggested. I just want to look at getting this working.


View PostCTphpnwb, on 06 February 2011 - 04:07 PM, said:

In addition to the problems previously mentioned, I have these questions for you:
  • Why are you using a while loop?
  • How does moving a value from the post variable to another variable help you? :crazy:
  • Why are you mixing php and html? You break out of php 8 times in only 49 lines of code. That's almost once every 6 lines! Do you expect to be able to read your own code when it's a couple of hundred lines long and you haven't seen it in six months? :eek:

I know that the coding is not the best and I plan on making amends to it and making it easier to read once I have it working the way that it is supposed to be working. I am still in the learning stages of PHP.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3394
  • View blog
  • Posts: 9,599
  • Joined: 08-June 10

Re: Delete script does not want to work

Posted 06 February 2011 - 09:57 AM

what value does $_POST['id'] have? try
if (!isset($_POST['id'])) {

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Delete script does not want to work

Posted 06 February 2011 - 10:44 AM

View Postmdmartiny, on 06 February 2011 - 12:54 PM, said:

I know that the coding is not the best and I plan on making amends to it and making it easier to read once I have it working the way that it is supposed to be working. I am still in the learning stages of PHP.

Ah, the beginner's mantra!

Here's the stock response: That will not work. Write clean code from the start or you'll have nothing but problems. The habits you develop early are going to stay with you, and the ones you're developing are not the ones you want. The last thing you need to worry about is whether or not the code works because if you write clean code it will work, and with far less effort than you're setting yourself up to make.
Was This Post Helpful? 0
  • +
  • -

#11 mdmartiny  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 28-May 10

Re: Delete script does not want to work

Posted 06 February 2011 - 11:10 AM

View PostDormilich, on 06 February 2011 - 04:57 PM, said:

what value does $_POST['id'] have? try
if (!isset($_POST['id'])) {


$_POST['id'] is the id of the record from the pick user page..

I tried the isset code and I got the following error
Parse error: syntax error, unexpected $end in /home/content/m/i/k/mikedmartiny/html/CMS/user/do_delete_user.php on line 28
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3394
  • View blog
  • Posts: 9,599
  • Joined: 08-June 10

Re: Delete script does not want to work

Posted 06 February 2011 - 11:11 AM

hm, I expected you to replace line #3 in your original code with that …
Was This Post Helpful? 0
  • +
  • -

#13 mdmartiny  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 28-May 10

Re: Delete script does not want to work

Posted 06 February 2011 - 11:16 AM

View PostCTphpnwb, on 06 February 2011 - 05:44 PM, said:

View Postmdmartiny, on 06 February 2011 - 12:54 PM, said:

I know that the coding is not the best and I plan on making amends to it and making it easier to read once I have it working the way that it is supposed to be working. I am still in the learning stages of PHP.

Ah, the beginner's mantra!

Here's the stock response: That will not work. Write clean code from the start or you'll have nothing but problems. The habits you develop early are going to stay with you, and the ones you're developing are not the ones you want. The last thing you need to worry about is whether or not the code works because if you write clean code it will work, and with far less effort than you're setting yourself up to make.


If I am not supposed to mix html and php together? How am I supposed to write it?

View PostDormilich, on 06 February 2011 - 06:11 PM, said:

hm, I expected you to replace line #3 in your original code with that …

I did replace line three

<?php

if (!isset($_POST['id'])) { {
	header ("LOCATION: pick_user.php");
	exit;
}
require('../includes/auth_user.php');

$sql = "DELETE FROM $table WHERE id='$_POST[id]'";
$result = mysql_query($sql, $connection) or die("Query $sql failed: " . mysql_error());
?>



but it gave me the error on line 28
Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3394
  • View blog
  • Posts: 9,599
  • Joined: 08-June 10

Re: Delete script does not want to work

Posted 06 February 2011 - 11:26 AM

didn’t the double curly braces strike you odd (where you had only one before)?
Was This Post Helpful? 0
  • +
  • -

#15 mdmartiny  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 28-May 10

Re: Delete script does not want to work

Posted 06 February 2011 - 11:32 AM

View PostDormilich, on 06 February 2011 - 06:26 PM, said:

didn’t the double curly braces strike you odd (where you had only one before)?


sorry didn't even notice that..

I have three different things going on right now.. creating my fantasy baseball league, uploading video and working on this

It still just redirects me back to pick user page

This post has been edited by mdmartiny: 06 February 2011 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2