I can not insert data into my database, i think i have some mistakes in my code, but i can not find it, can anyone show me my mistake. I can add data when the $mieuta ==""
but when $mieuta !== ""
i can not insert it.
Here is my code: please help me.
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');
}
}
You are using !==
Operator which means like that:
$x !== $y
Returns true if $x
is not equal to $y
, or they are not of the same type.
That means $mieuta
is empty variable.
Change this
else if(empty($mieuta)){ # change. Check empty
}
else if(!empty($mieuta)){ # Change. Check not empty
}
EDIT 01
Add
if (!mysqli_query($conn,$sql)) {
echo("Error description: " . mysqli_error($con));
}
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(empty($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
if (!mysqli_query($conn,$sql)) {
echo("Error description: " . mysqli_error($con));
}
}
else if(!empty($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')";
if (!mysqli_query($conn,$sql)) {
echo("Error description: " . mysqli_error($con));
}
}
header('Location:prod_management.php');
}
You can solve this problem by assigning a default value to $mieuta
.
$mieuta = $_POST['mieuta'] || 'NULL';
Then, you just need one query and no if
statements.
$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')";
if (!mysqli_query($conn,$sql)) {
echo("Error description: " . mysqli_error($con));
}
header('Location: prod_management.php');