Search code examples
phpmysqlsql-delete

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");


?>

Solution

  • $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