所以我试图从数据库"条形码"中获取一行要删除的信息,但没有成功。我点击了提交按钮,但它并没有删除我在输入框中键入的"项目代码"。帮助??
编辑后我有一个新错误
错误:您的SQL语法有错误;查看与MySQL服务器版本相对应的手册,了解在第1行"1"附近使用的正确语法
删除.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.phpAdd.php有效,但delete无效。
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);
?
您正在执行两次内容:
此行执行查询并将结果放入$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));
}
}