Question

I am working on an basic crud API. So far i have a working get function, but want to delete the current users. MY tables are as follows.

CREATE TABLE IF NOT EXISTS `users` 
(
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_fullname` varchar(25) NOT NULL,
`user_email` varchar(50) NOT NULL,
`user_password` varchar(50) NOT NULL,
`user_status` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

This is the delete code, how can i delete a user by specific id?

} elseif ($this->get_request_method() == "DELETE"){


            $result = $this->db->query("DELETE * From users");


            $result->close();


            // Send the response to the response() function (lives in the parent class) the 200 is the HTTP status code that's returned
            $this->response(json_encode("Deleted", JSON_PRETTY_PRINT), 200);

        } else {

            /*
             * THE METHOD IS NOT ALLOWED
             */

            $this->response(json_encode("Method Not Allowed"), 405);

        }

No correct solution

OTHER TIPS

You could send the user_id in the URL string then delete that specific record

http://url.com.script.php?user_id=123

// get the user ID and cast to an integer
$user_id = (int) $_GET['user_id'];

// run the query
$result = $this->db->query("DELETE FROM users WHERE user_id = $user_id");

of course, you'd want to sanitize the $user_id against SQL injection in your query, just in case...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top