Jump to content

MYSQL delete query problem?


eddieblunt

Recommended Posts

Why won't the following code work?

 

I want to ensure that the logged in user can only delete his/her own stories? and not just type in http://example.com/delete.php?story=1 for example

 

or is there a better way of doing this?

 

<?php
include_once('include_fns.php');

$handle = db_connect();

$story = $_REQUEST['story'];
$writer = $_SESSION['userid'];  

$query = "DELETE from stories WHERE id = $story AND writer = $writer";

$result = $handle->query($query);
header('Location: '.$_SERVER['HTTP_REFERER']);
?>

 

Link to comment
Share on other sites

if either of your fields are varchar fields you need to put your values in single quotes. Good practice to just do it anyway. You can also add LIMIT to it so that only one row gets deleted just in case.

 

$query = "DELETE FROM `stories` WHERE `id` = '$story' AND `writer` = '$writer' LIMIT 1";

 

Ray

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.