James30
James30

Reputation: 285

PHP using DELETE FROM to remove row in mysql

Below is the code I have been trying to use to delete a row from my mysql database. I have been searching through the forum and I have yet find the issue. I know my connection is working properly as I have been able to use the INSERT INTO command with no problems. I'm sure it's pretty obvious that I am new to this, looking for simple solution.

<?php

$_POST[title];
$jimmy = $_POST[title];
echo $jimmy;    ## this echo is here to make sure I was getting form data sent over      correctly
include("dogs.inc");
$cxn = mysqli_connect($host,$user,$passwd,$dbname)
         or die("Couldn't connect to server");

$sql = "DELETE FROM ashly3 WHERE title = '$jimmy' " ;
mysqli_query( $cxn, $sql );

include("ashly.php");


?>

Upvotes: 0

Views: 230

Answers (5)

Hans Henrik
Hans Henrik

Reputation: 63

i think your error is $_POST[title] should be $_POST['title'] , anyway try this code.

<?php
error_reporting(E_ALL);
//$_POST[title];
$jimmy = $_POST['title'];
echo $jimmy;    ## this echo is here to make sure I was getting form data sent over      correctly
include("dogs.inc");
$cxn = mysqli_connect($host,$user,$passwd,$dbname)
         or die("Couldn't connect to server");

$sql = 'DELETE FROM `ashly3` WHERE title = \''.mysqli_real_escape_string($cxn,$jimmy).'\'';

$retval=mysqli_query( $cxn, $sql );
echo "retval:";var_dump($retval);
if($retval===false)
{
echo "mysqli_error:";var_dump(mysqli_error($cxn));
}
include("ashly.php");


?>

Upvotes: 1

mewm
mewm

Reputation: 1277

You should really consider using PDO for databases.
Anyways, for your solution, to identify the error: Try this:
mysqli_query( $cxn, $sql )or die(mysqli_error());

Also, please use mysqli_real_escape_string() around your user input :)

Upvotes: 1

Ruan Mendes
Ruan Mendes

Reputation: 92294

$jimmy = $_POST[title];

Should be

$jimmy = $_POST['title']; 

But that's not the reason it doesn't work, PHP should fix that error for you with a warning.

Also, inserting text from the $_POST without escaping is going to cause you trouble with SQL injection. Imagine if the user inserts hello' OR 1 into the title text field. Use http://phptutorial.info/?mysqli.real-escape-string

Upvotes: 1

Harsha Reddy
Harsha Reddy

Reputation: 99

One way could be this, it is working for me

<?php
require '../access/dbaccess.php';
$dtlname=  $_POST['remove_dtlname'];
mysql_query("delete from `sample_tbl` where `dtl_name`='$dtlname'");
?>

Where dbaccess.php is

<?php
$link = mysql_connect('<your server>', '<username>', '<password>');
        if (!$link)
             die('Could not connect: ' . mysql_error());
mysql_select_db('btp');
?>

Upvotes: 0

Ocracoke
Ocracoke

Reputation: 1768

Try breaking out the variable out of the $SQL line:

$sql = "DELETE FROM ashly3 WHERE title = '".$jimmy."' " ;

Upvotes: -1

Related Questions