Jump to content

Delete function not working


Recommended Posts

Hi all, I have a table...

 

ID          user_id          interest

1            20                football

2            20              tennis

3            20              soccer

 

Im trying to write a function that when the user is logged in, his 3 interests are shown, he then has the option to delete an interest only I cant quite get it to work, can anybody see where im going wrong? Im still a novice with PHP so my code may appear very odd...

 

<?php 
    if(isset($_POST['int1'])) { 
        $interest = $_POST['int1']; 
        $qResult= mysql_query("DELETE " . mysql_real_escape_string($interest) . "FROM user_interests WHERE user_id = " . mysql_real_escape_string($usersClass->userID()));
    }
    elseif(isset($_POST['int2'])) {
        $interest = $_POST['int2']; 
        $qResult= mysql_query("DELETE " . mysql_real_escape_string($interest) . "FROM user_interests WHERE user_id = " . mysql_real_escape_string($usersClass->userID()));
    } 
    elseif(isset($_POST['int3'])) {
        $interest = $_POST['int3']; 
        $qResult= mysql_query("DELETE " . mysql_real_escape_string($interest) . "FROM user_interests WHERE user_id = " . mysql_real_escape_string($usersClass->userID()));
    }
    
    print $interest1 . "<form method='post' action='#'><input type='hidden' value='".$interest1."' name='int1' id='int1'/><input type='submit' value='delete' /></form><br />";
    print $interest2 . "<form method='post' action='#'><input type='hidden' value='".$interest2."' name='int2' id='int2'/><input type='submit' value='delete' /></form><br />";
    print $interest3 . "<form method='post' action='#'><input type='hidden' value='".$interest3."' name='int3' id='int3'/><input type='submit' value='delete' /></form><br />";
    ?>[/code[

Link to comment
https://forums.phpfreaks.com/topic/241369-delete-function-not-working/
Share on other sites

I'vr added or die() which now gives me this

 

Could not delete data: Unknown column 'tenni' in 'where clause'

 

The 'tenni' is a value though, I weant to delete the row that contains the value 'tenni' in the interests column

 

Ive finally found the problem, Firstly I used 'or die()' to see where I was going wrong (I SHOUDL ALWAYS DO THIS!)

 

I then received the error 'unknown column in where clause' which led me to add '' around my var, the final query is...

 

 $qResult= mysql_query("DELETE from user_interests WHERE interest = '" . mysql_real_escape_string($interest) . "' and user_id = " . $usersClass->userID());
    if(! $qResult )
        {
          die('Could not delete data: ' . mysql_error());
        }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.