Jump to content

Delete Database Entry Code... Help!


mat3000000

Recommended Posts

No errors come up when I run this but it just doesn't delete the entry.

 

 

<?
$rec = $_POST['stock'];
if (!$rec){ 
die ('Please eneter a stock number');
}

$link = mysql_connect("xxxx","xxx","xxx");
mysql_select_db('wadkin', $link) or die( "Unable to select database");

mysql_query("DELETE FROM 'wadkin'.'stocklist' WHERE 'stocklist'.'Stock Number' = $rec")
or die ('Query failed');

mysql_close($link);

?>

 

Can someone please correct,

 

(btw I have also tried... "DELETE FROM stocklist WHERE 'Stock Number' = $rec")

Link to comment
https://forums.phpfreaks.com/topic/217384-delete-database-entry-code-help/
Share on other sites

You're using quotes around the table and field names. It isn't really necessary in this case, but if you feel like enclosing them for a query in MySQL, use backticks instead. `table`. `field`

<?php

error_reporting(E_ALL);

$link = mysql_connect("xxxx","xxx","xxx");
mysql_select_db('wadkin', $link) or die( "Unable to select database");

//'Catch' the $_POST value being submitted
if (isset($_POST['stock']) && !empty($_POST['stock'])){

$rec = strip_tags(mysql_real_escape_string($_POST['stock']));//sanitise first

//issue the query
$res = mysql_query("DELETE FROM `wadkin`.`stocklist` WHERE `stocklist`.`Stock Number` = ".$rec." ") or die ('Query failed'.mysql_error());

//Check that the query was successful
if($res){
	echo "record deleted!";
	exit;
}
else{
	echo "Uh-oh, something went wrong!";
	exit;
}

}
else{
echo "Please enter a valid stock number!";
exit;
}
?>

 

There are better ways, but this will work fine for what you are trying to achieve.

 

If the 'stock' value isn't numerical, you will need to quote ('".$rec."') that value in the sql.

 

Rw

I am now given an error:

 

Warning: mysql_query() expects parameter 1 to be string, resource given in C:\wamp\www\admin\delete.php on line 10

 

Line 10 is:

$res = mysql_query($link, "DELETE FROM 'stocklist' WHERE 'Stock Number'=".$rec."")

 

???????

 

Please can someone correct.

Thanks

Keep forgetting back-sticks not quotes! :S oops!

 

You'll all be glad to know I got this...

 

Unknown column 'S8637' in 'where clause'

 

What does this mean in english please?

If it means it can't find this entry in the table under `Stock Number` then something has gone wrong, I know that this number is in my table????

Archived

This topic is now archived and is closed to further replies.

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