0

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

thechaoticpanda
  • 396
  • 2
  • 18

3 Answers3

2

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

ins0
  • 3,918
  • 1
  • 20
  • 28
1

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);
Noman
  • 4,088
  • 1
  • 21
  • 36
0

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'";
XjSv
  • 46
  • 8