Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently having trouble deleted a photo from my database after a user uploads it.

This is my index file.

case "delete":
delete((int)$_POST['IdPhoto']);
break;

This is my API file (Trying to delete photo with specific ID for testing):

function delete($IdPhoto) {

$result = query("DELETE from photos WHERE IdPhoto='28'");
if (!$result['error']) {
    // if no error occured, print out the JSON data of the 
    // fetched photo data
    print json_encode($result);
} else {
    //there was an error, print out to the iPhone app
    errorJson('Photo stream is broken');
}

}

That works perfectly. How would I do it for any ID though or a specific one that I would grab through my iOS application?

The application grabs IdPhoto, ID, Title automatically when it loads the image. I just need the proper query to determine it by the specific ID.

If it helps, this is how I load images:

    // load the last 50 photos from the "photos" table, also join the "login" so that you can fetch the 
    // usernames of the photos' authors
    $result = query("SELECT IdPhoto, title, l.IdUser, username FROM photos p JOIN login l ON (l.IdUser = p.IdUser) ORDER BY IdPhoto DESC LIMIT 50");
share|improve this question
You are vulnerable to SQL injection attacks. – Marc B Jul 19 at 14:41
DELETE FROM photos WHERE PhotoData = '$photoData' AND Title='$title' PD:insert_id don't return you the Id of DELETE... you need select before the id ;) (Almost, is better delete with id, than from title / photodata ;)) – Eleazan Jul 19 at 14:41
Casting to (int) is not an alternative to proper SQL escaping. – tadman Jul 19 at 15:23
If our answers have helped you, you need to accept one of them, and if possible, upvote the others. This allows others to help you in the future. – Kneel-Before-ZOD Jul 20 at 17:38

4 Answers

up vote 1 down vote accepted

First, your delete statement is wrong.
This

 
DELETE photos(PhotoData,Title) WHERE $photoData, $title
 

should be

 
   DELETE from photos WHERE PhotoData='$title';
 

When you delete from a table, the name of the column(s) that determine what row to delete is the only one needed to be specified.

Secondly, since it seems that the column you are using for the deletion (PhotoData) is a string, you need to surround your value ($title) with a single quotes '$title'.

If you are still having problems with the deletion, it is possible that the value in the database and in $title do not match character by character. This can happen if there are spaces between the values in either case. You can either use trim() to remove any extra space before running the sql statement OR run a query like this:

 
  DELETE from photos WHERE PhotoData like '%$title%';
 

This will remove all rows having the same characters as the $title

Hope this helps.

Update

Since you want to pass in the column's and row id, your function should have 2 parameters.

 
function delete($IdPhoto, $row_id) {

   $result = query("DELETE from photos WHERE IdPhoto=$row_id");
   if (!$result['error']) {
    // if no error occured, print out the JSON data of the 
    // fetched photo data
    print json_encode($result);
   } else {
    //there was an error, print out to the iPhone app
    errorJson('Photo stream is broken');
   }

}
 

So, just pass in 2 parameters and you should be fine.

share|improve this answer
Can you see my edits to tell me what my case statement should be as well? – asg Jul 19 at 14:57
Your delete function declaration accepts 3 parameters, but you only supplied it one in that case statement. – Kneel-Before-ZOD Jul 19 at 15:06
It could be anything as long as it matches the number of parameters specified ($id, $photoData, $title) . – Kneel-Before-ZOD Jul 19 at 15:10
See edits......please :) – asg Jul 19 at 15:21
That should work; remove the single quotes though. Integers don't need them; only strings do. – Kneel-Before-ZOD Jul 19 at 15:22
show 2 more comments

your are missing a from clause in your deleting query add from into query

$result = query("DELETE from photos(PhotoData,Title) WHERE $photoData, $title");

try this one.

share|improve this answer
What should be my case request in my index file? – asg Jul 19 at 14:55
are you getting proper $_POST['IdPhoto'] in index file? – Azam Alvi Jul 19 at 16:31
you cannot directly call a function with it's name in codeigniter you need to add $this so change line in index file with this $this->delete((int)$_POST['IdPhoto']); – Azam Alvi Jul 19 at 16:33

i think you shoud leran how to delete data by using sql

$result = query("DELETE from photos where title=$title");
share|improve this answer
I tried that too and it did not work. – asg Jul 19 at 14:40
then you can echo that sql to show if the sql is what you want – Jason Yang Jul 19 at 14:43

your delete statement is incorrect. Use the following statement. You don't specify the columns for a delete query because your actually deleting a row.

DELETE FROM photos WHERE photoData = $variable
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.