简体   繁体   中英

Why is my PHP code not deleting a row in database?

The code below should be able to delete a row from table car having ProNumber='$searchTerm' and ProCode ='$searchTerm1' but it is not.Any help will be much appreciated .

<?php
$searchTerm = trim($_GET['keyname']);
$searchTerm1 = trim($_GET['codename']);
//create a database connection
mysql_connect("localhost","root","") or die("Error:".mysql_error());
mysql_select_db("products");
//Delete the record
$updateQuery = "DELETE FROM car WHERE ProNumber = '$searchTerm' AND ProCode ='$searchTerm1'";
mysql_query($updateQuery) or die("Error: ".mysql_error());
?>

note that the table and columns are created and filled with the corespondent data.
Update :
The following part of the answer to this question is what solved it
Mysql vs Mysqli Functions

mysql_* functions are deprecated in PHP 5.5.0 you should use the mysqli_* functions to make sure your scripts works on newer PHP Versions.

http://php.net/manual/en/book.mysqli.php

Column Types

please make sure that your column types are correct. if you wrap your data into quotes like this '1234' mysql will interpret the data as a string , write it without quotes and it will be interpreted as an integer .

WHERE column = 1234 on integer columns WHERE column = '1234' on varchar columns

SQL Injections

please use before pass variables directly into sql statements teh php mysqli_real_escape_string function, to prevent SQL Injections

http://php.net/manual/en/mysqli.real-escape-string.php

Mysql vs Mysqli Functions

mysql_* functions are deprecated in PHP 5.5.0 you should use the mysqli_* functions to make sure your scripts works on newer PHP Versions.

http://php.net/manual/en/book.mysqli.php

Logical Error

Make sure data you want to delete is present in your Mysql Database

Error is in your database connection , you cannot delete records until your database connection is not set

//create a database connection
mysql_connect("localhost","root","") or die("Error:".mysql_error());
mysql_select_db("products");

Change above code to this 

$connection = mysql_connect("localhost","root","") or die("Error:".mysql_error());
mysql_select_db("products",$connection);

Just a guess but one problem you might have is that you have single quotes around $searchTerm . Your searching for a number but your passing a string.

Possible fix (Notice the single quotes are missing from $searchTerm ):

$updateQuery = "DELETE FROM car WHERE ProNumber = $searchTerm AND ProCode = '$searchTerm1'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM