2013-05-10 68 views
0

我想從數據庫中使用php刪除記錄。這應該發生在我點擊一個按鈕時,沒有顯示錯誤,並且查詢出現在屏幕上,但是記錄仍然在數據庫上。記錄不會從數據庫中刪除

phpmyadmin給我使用以下代碼:DELETE FROM'the shop'。'客戶」 WHERE‘客戶’。'客戶ID = 8

<?php 
$host="localhost"; // Host name 
$tbl_name="customer"; // Table name 
$db_user="root"; 
$db_pass=""; 

$connect = mysql_connect("$host", "$db_user", "$db_pass"); 
$db_name="the_shop"; // Database name 
mysql_select_db("$db_name"); 
if (!$connect) 
{ 
die("MySQL could not connect!"); 
} 

if(isset($_GET['submit2'])){ 

$db_username = $_GET['username']; 

$sql4 = "DELETE FROM 'the_shop'.'customer' WHERE 'customer'.'CustomerID' = 8" 
or die('error deleting record'); 
mysql_query($sql4); 
echo $sql4; 
} 
?> 

我知道這將只刪除了一個客戶ID是= 8 我的意思是,一旦這個工作,我會用用戶名和更換客戶ID記錄'8'與相關的變量,將通過表格給出值

任何幫助表示讚賞

回答

1
Use this,It is working. 
<?php 
$host="localhost"; // Host name 
$tbl_name="customer"; // Table name 
$db_user="root"; 
$db_pass=""; 

$connect = mysql_connect("$host", "$db_user", "$db_pass"); 
$db_name="the_shop"; // Database name 
mysql_select_db("$db_name",$connect); 
if (!$connect) 
{ 
die("MySQL could not connect!"); 
    } 

if(isset($_GET['submit2'])){ 

$db_username = $_GET['username']; 

$sql4 = "DELETE FROM `the_shop`.`customer` WHERE `customer`.`CustomerID` = 8"; 

mysql_query($sql4,$connect) or die('error deleting record'); 
echo $sql4; 
} 

?>

+0

我不知道你改變了什麼,所以我很難從你的答案中學習,請讓我知道嗎? 我已經做出了改變,你是正確的,它現在正在工作:) – 2013-05-10 12:12:27

+0

你可以檢查'the_shop'。'customer'和'the_shop'.customer'之間的區別,請檢查你的和rakesh的$ sql4。 即你使用「'」,你應該使用「'」。 – unknownbits 2013-05-15 13:18:00

3

您使用引號來代替反勾

$sql4 = "DELETE FROM `the_shop`.`customer` WHERE `customer`.`CustomerID` = 8"; 

而且(因爲你沒有使用任何保留的關鍵字在這裏,在這種情況下)你不需要背蜱,以及你在錯誤的使用die()

1

您的陳述不正確。您使用引用而不是後面的勾號。但是你可以讓你的陳述更容易。

$sql4 = "DELETE FROM customer WHERE CustomerID = 8"; 
0
  1. 你並不需要指定要在查詢中查詢的數據庫。 這個就足夠了:

    DELETE FROM customer WHERE CustomerID = 8 
    
  2. MySQL擴展已被棄用。這意味着它不再被PHP支持,不應該被使用。改爲mysqlipdo

+0

您正在使用單引號,這是無效的 – 2013-05-10 10:21:12

+0

您是對的,我得到的值和字段混淆在一起。 – 2013-05-10 10:22:21

1
$sql4 = "DELETE FROM `the_shop`.`customer` WHERE `customer`.`CustomerID` = 8" 
mysql_query($sql4);or die('error deleting record'); 
echo $sql4; 
0

你可以使用這個。您無需指定數據庫。

delete from customer where CustomerID = 8