Connecting Tech Pros Worldwide Forums | Help | Site Map

Delete not working

Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#1: Jun 24 '09
This is a classified ads table which contains all the item's information.

The item's "id" is received on this delete page using
Expand|Select|Wrap|Line Numbers
  1. $id = $_GET["id"];
. It's being sent by
Expand|Select|Wrap|Line Numbers
  1. delete_picture.php?id=$id
A query takes place which grabs the image_name from the database based on the id. Then it unlinks the images in the image/ and image/thumbs folders with this image_name. So the id variable is working, at least to this point.

Then it is suppose to perform this delete query:
Expand|Select|Wrap|Line Numbers
  1. $sql = "DELETE image_name,b_width,b_height,t_width,t_height
  2. FROM ads WHERE id='".$id."'"; 
  3. $result = mysql_query($sql);
But this part is not working. Is there any obvious reason why?

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#2: Jun 24 '09

re: Delete not working


Ya you're delete is wrong.

See Manual: http://dev.mysql.com/doc/refman/5.0/en/delete.html

From what it looks like, you seem to select the image fields of the ads table to hopefully just delete those columns, and not the entire record.

That's now how MySQL works, a record must always have the same number of fields as the table allows although they can contain nothing (empty string, or NULL value)

Remember, Sanitize your inputs if I were to call your delete.php file like so

delete.php?id=1';DELETE FROM ads;

what do you think will happen? SQL Injection.

Good luck,



Dan
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#3: Jun 24 '09

re: Delete not working


Quote:

Originally Posted by DavidPr View Post

This is a classified ads table which contains all the item's information.

The item's "id" is received on this delete page using

Expand|Select|Wrap|Line Numbers
  1. $id = $_GET["id"];
. It's being sent by
Expand|Select|Wrap|Line Numbers
  1. delete_picture.php?id=$id
A query takes place which grabs the image_name from the database based on the id. Then it unlinks the images in the image/ and image/thumbs folders with this image_name. So the id variable is working, at least to this point.

Then it is suppose to perform this delete query:
Expand|Select|Wrap|Line Numbers
  1. $sql = "DELETE image_name,b_width,b_height,t_width,t_height
  2. FROM ads WHERE id='".$id."'"; 
  3. $result = mysql_query($sql);
But this part is not working. Is there any obvious reason why?

Add 'or die(mysql_error());' to your mysql query to see if you a generating any errors.

Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query(...) or die(mysql_error());
  2.  
Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#4: Jun 24 '09

re: Delete not working


How would I sanitize that?
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#5: Jun 24 '09

re: Delete not working


Quote:
original
$sql = "DELETE image_name,b_width,b_height,t_width,t_height
FROM ads WHERE id='".$id."'";
$result = mysql_query($sql);

your delete query is wrong

may be

Expand|Select|Wrap|Line Numbers
  1.  
  2. $sql = "DELETE FROM ads WHERE id='".$id."' ";
  3. $result = mysql_query($sql);
  4.  
  5.  
Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#6: Jun 25 '09

re: Delete not working


I found that UPDATE worked better than DELETE in this instance.
Expand|Select|Wrap|Line Numbers
  1. $name = $_POST['name'];
  2. $address1 = $_POST['address1'];
  3. $address2 = $_POST['address2'];
  4. $phone = $_POST['phone'];
  5. $cell = $_POST['cell'];
  6.  
  7. // variables may have a value or they may be empty
  8.  
  9. query="UPDATE address_book SET
  10. name='$name',
  11. address1='$address1',
  12. address2='$address2',
  13. phone='$phone',
  14. cell='$cell'
  15. WHERE id='$id'";
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#7: Jun 25 '09

re: Delete not working


cool ..that you wanted ..

get some tutorial online
Reply