项目代码不会从数据库中删除

时间:2016-01-01 21:57:18

标签: php mysql mysqli

所以我试图从我的数据库'条形码'中删除一行信息,但它没有发生。我点击了提交按钮,但它没有删除我在输入框中输入的'itemcode'。 HELP ??

编辑后我出现了新错误

  

错误:您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在第1行的“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);
?

1 个答案:

答案 0 :(得分:1)

你正在执行两次:

该行执行查询并将结果放入$ delete:

 $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));
     }
 }