2

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();
            }
        }
}
}
1
  • so, check if value = 0 ( or > 0 ) or have your db accept null/zero value Commented Feb 2, 2016 at 3:25

2 Answers 2

2

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)){ ...
Sign up to request clarification or add additional context in comments.

1 Comment

That fixed it! Big thanks, I've been racking my brain over this one.
0

The calls to empty() return FALSE on a 0 so remove this check on the integers, or replace with >== 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.