2016-01-01 19 views
2

所以我想要從我的數據庫'條碼'中刪除一行信息,但它沒有發生。我點擊了提交按鈕,但它並沒有刪除我在輸入框中輸入的'itemcode'。幫幫我??商品代碼不是從數據庫中刪除

以下編輯我有一個新的錯誤

Error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

Delete.php

Testing to see if items deleted 
<form action="delete.php" method="post"> 
<tr> 
<td>Item Code: </td> 
<td><input type="text" name="itemcode" autofocus></td> 
<td><input type="submit" value="Delete"></td> 
</tr> 
</table> <br> 

<?php 
require_once('dbconnect.php'); 
$txtitemcode = (!empty($_POST['itemcode']) ? $_POST['itemcode'] : null); 
$result = mysqli_query($con, "SELECT * from barcode order by itemcode"); 
$delete = mysqli_query($con,"DELETE FROM barcode WHERE itemcode='itemcode'"); 
if(!mysqli_query($con, $delete)) 
    { 
     echo('Error:'.mysqli_error($con)); 
    } 
echo "<center><table border=1>"; 
echo"<tr> 
<th>ITEM CODE:</th> 
<th>Company Shipping:</th> 
</tr>"; 
while($row = mysqli_fetch_array ($result)) 
{ 
echo"<tr> 
<td align= center>".$row['itemcode']."</td> 
<td align=center>".$row['item']."</td> 
</tr>"; 
} 
echo "</table>"; 
mysqli_close($con); 

dbconnect.php

$con = mysqli_connect("localhost","root","root","db1"); 

// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    else 
    { 
    echo "Connected to Database. Please Continue."; 
    } 

Add.php該add.php工作,但刪除沒有。

php 

include('dbconnect.php'); 
function get_posts() { 

    global $con; 
    $txtitemcode = (!empty($_POST['itemcode']) ? $_POST['itemcode'] : null); 
    $txtitem = (!empty($_POST['item']) ? $_POST['item'] : null); 
    $sql1 = "INSERT INTO barcode (itemcode, item) VALUES ('".$txtitemcode."','".$txtitem."')"; 
    if(!mysqli_query($con, $sql1)) 
    { 
     die('Error:'.mysqli_error()); 
    } 
    echo "<script> alert('1 record added'); 
window.location.href='index.php'; 
</script>"; 
} 
get_posts(); //Must have to show posts in table 
mysqli_close($con); 
? 
+0

使用mysql_error添加一些MySQL錯誤輸出()。這甚至不是有效的sql:mysqli_query($ con,「DELETE FROM barcode(itemcode)VALUES('」。$ txtitemcode。「')」); – maxhb

+0

你不提供你的表結構,但是你的SQL語句沒有讀「DELETE FROM barcode WHERE itemcode ='$ txtitemcode'」(你使用了字段名'Name')。 – crafter

+0

另外,請注意@ maxhb關於使用mysql_error()/的警告/不檢查您在黑暗中摸索的錯誤,如果出現任何錯誤。 – crafter

回答

1

您正在執行的東西兩次:

該行執行的查詢,並將結果在到$刪除:

$delete = mysqli_query($con,"DELETE FROM barcode WHERE itemcode='itemcode'"); 

現在你發出另一個查詢從上面的結果:

if(!mysqli_query($con, $delete)) 
{ 
    echo('Error:'.mysqli_error($con)); 
} 

而這是發佈錯誤,因爲發佈:結果在$delete是「1」,「1」不是一個聲明。

變化:

$delete = mysqli_query($con,"DELETE FROM barcode WHERE itemcode='itemcode'"); 

if(!$delete) // or if ($delete === false) 
{ 
    echo('Error:'.mysqli_error($con)); 
} 

而且,這幾行的邏輯以下,我認爲它應該是:

if (isset($txtitemcode)) 
{ 
    $delete = mysqli_query($con,"DELETE FROM barcode WHERE itemcode='" . $txtitemcode . "'"); 

    if(!$delete) // or if ($delete === false) 
    { 
     echo('Error:'.mysqli_error($con)); 
    } 
} 
+0

這擺脫了錯誤您的SQL語法中有錯誤; ...但是這仍然不會刪除數據庫外的項目代碼 –

+0

是否有一行的itemcode等於字符串'「itemcode」'?這是您目前正在刪除的內容。請參閱 \t @crafter以上的評論 –

+0

我編輯了我的原始文件以顯示我的add.php,它可以工作。所以我需要做相反的添加.... –

相關問題