你能帮我解决这个问题吗?我试图将数据插入我的数据库,而不是插入die()输出;出现
PHP
if (isset($_GET['submit'])) {
$item_code = MysqLi_real_escape_string($conn,$_GET['item_code']);
$item_name = MysqLi_real_escape_string($conn,$_GET['item_name']);
$supplier = MysqLi_real_escape_string($conn,$_GET['supplier_name']);
$brand = MysqLi_real_escape_string($conn,$_GET['brand_name']);
$quantity = MysqLi_real_escape_string($conn,$_GET['quantity']);
$unit = MysqLi_real_escape_string($conn,$_GET['unit_name']);
$price = MysqLi_real_escape_string($conn,$_GET['price']);
$item_type = MysqLi_real_escape_string($conn,$_GET['type_name']);
$category = MysqLi_real_escape_string($conn,$_GET['cat_name']);
if ($item_code == '' || $item_name == '' || $supplier == '' || $brand == '' ||
$quantity == '' ||
$unit == '' ||
$price == '' ||
$item_type == '' ||
$category == ''
) {
header("Location: item.PHP?attempt=empty");
} else {
$sql = MysqLi_query($conn,"INSERT INTO itemlist (item_name,supplier_name,brand_name,quantity,unit_name,price,type_name,cat_name) values('$item_name','$supplier','$brand','$quantity','$unit','$price','$item_type','$category')")
or die("Could not execute the insert query.");
header("Location: item.PHP?attempt=saved");
}
}
?>
最佳答案
我只注意到一些事情:
原文链接:https://www.f2er.com/mysql/432859.html>为什么使用GET方法传递数据而不是POST?
>您可以使用empty()
而不是==”
>您可以尝试prepared statement,而不是转义和清理每个提交的数据
> $_GET [‘item_code’]的目的是什么?你没有在插入中使用它,但它在你的if()条件下.
假设您将表单从GET更改为POST方法:
if (isset($_POST['submit'])) {
if(empty($_POST['item_code']) || empty($_POST['item_name']) || empty($_POST['supplier_name']) || empty($_POST['brand_name']) || empty($_POST['quantity']) || empty($_POST['unit_name']) || empty($_POST['price']) || empty($_POST['type_name']) || empty($_POST['cat_name'])){
header("Location: item.PHP?attempt=empty");
} else {
$stmt = $conn->prepare("INSERT INTO itemlist (item_code,item_name,cat_name) VALUES (?,?,?)");
$stmt->bind_param("sssssssss",$_POST['item_code'],$_POST['item_name'],$_POST['supplier_name'],$_POST['brand_name'],$_POST['quantity'],$_POST['unit_name'],$_POST['price'],$_POST['type_name'],$_POST['cat_name']);
$stmt->execute();
$stmt->close();
header("Location: item.PHP?attempt=saved");
}
}
请记住,一个空数据将进入if()条件并执行标题(“Location:item.PHP?attempt = empty”);.