我无法将数据插入数据库,我认为代码中存在一些错误,但是我找不到它,任何人都可以向我展示我的错误。
我可以在$mieuta ==""
时添加数据,但在$mieuta !== ""
时无法插入数据。
这是我的代码:请帮助我。
if (isset($_POST["add"])) {
$ten_sp = $_POST["ten_sp"];
$ngay_sx= $_POST["ngay_sx"];
$ma_sp = $_POST["ma_sp"];
$vitrilapdat = $_POST["vitrilapdat"];
$chungloai = $_POST["chungloai"];
$nhom = $_POST["nhom"];
$d_an = $_POST["d_an"];
$nhasx = $_POST["nhasx"];
$mieuta = $_POST["mieuta"];
if ($ten_sp == "" || $ngay_sx == "" || $ma_sp == "" || $vitrilapdat =="" || $chungloai =="" || $nhom =="" || $d_an=="" || $nhasx =="") {
echo '<h4 align=center style="color: red;">Vui lòng nhập đầy đủ thông tin</h4>';
}else if($mieuta ==""){
//thực hiện việc lưu trữ dữ liệu vào db
$sql = "INSERT INTO products(
ten_sp,
ngay_sx,
ma_sp,
vitrilapdat,
chungloai,
nhom,
d_an,
nhasx
) VALUES (
'$ten_sp',
'$ngay_sx',
'$ma_sp',
'$vitrilapdat',
'$chungloai',
'$nhom',
'$d_an',
'$nhasx'
)";
// thực thi câu $sql với biến conn lấy từ file connection.php
mysqli_query($conn,$sql);
header('Location:prod_management.php');
}else if($mieuta!== ""){
//thực hiện việc lưu trữ dữ liệu vào db
$sql = "INSERT INTO products(
ten_sp,
ngay_sx,
ma_sp,
vitrilapdat,
chungloai,
nhom,
d_an,
nhasx,
mieuta
) VALUES (
'$ten_sp',
'$ngay_sx',
'$ma_sp',
'$vitrilapdat',
'$chungloai',
'$nhom',
'$d_an',
'$nhasx',
'$mieuta'
)";
mysqli_query($conn,$sql);
header('Location:prod_management.php');
}
}
最佳答案
您正在使用!==
运算符,它意味着:
$x !== $y
如果
$x
不等于$y
或它们不是同一类型,则返回true。这意味着
$mieuta
是空变量。关于php - 无法将数据插入数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34430087/