Reputation: 33
I am very new to php and writing what I thought was a simple php page to insert data into a mysql table. The table only holds a date and two integers. The problem is with the integers. The code works fine for numbers 1 - 100 but when I enter a zero ( 0 ) it will not insert the data. I'm sure there is a simple explanation but I can't find it. Any help is appreciated.
<?php
error_reporting(0);
require 'db/connect.php';
require 'db/security.php';
if (!empty($_POST)) {
if (isset($_POST['logDate'], $_POST['shiftID'], $_POST['deployed'])) {
$date = trim($_POST['logDate']);
$shiftID = trim($_POST['shiftID']);
$deployed = trim($_POST['deployed']);
$logDate = date('Y-m-d', strtotime($date));
if (!empty($logDate) && !empty($shiftID) && !empty($deployed)) {
$insert = $db->prepare("INSERT INTO info
(logDate, shiftID, deployed)
VALUES (?,?,?)");
$insert->bind_param('sii', $logDate, $shiftID, $deployed);
if ($insert->execute()) {
header ('location: test1.php');
die();
}
}
}
}
Upvotes: 2
Views: 173
Reputation: 16963
Look at this statement here,
if (!empty($logDate) && !empty($shiftID) && !empty($deployed)){ ...
And from the documentation of empty()
,
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
So instead of empty()
, use isset()
function, like this:
if (isset($logDate) && isset($shiftID) && isset($deployed)){ ...
Upvotes: 2
Reputation: 182
The calls to empty() return FALSE on a 0 so remove this check on the integers, or replace with >== 0
Upvotes: 0