1

When trying to do a simple insert in PHP to MySQL database, a value of '.5' is entered into the hours variable and is being ignored and set to 0 in the database. I've tried with the db datatype set to float or decimal. Neither work.

Here is my insert code:

$sql ="INSERT INTO timecarddetails (timecardId, tdate, hours, ot, ticketNumber, km, details) 
            VALUES (?,?,?,?,?,?,?)";
$stmt = $conn->prepare($sql);
if($stmt === false){
    trigger_error('Wrong SQL: ' .$sql. ' Error: ' . $conn->error, E_USER_ERROR);
}
$stmt->bind_param('isiisis',$tcId,$JOBDATE[$key],$HOURS[$key],$OT[$key],$TICKET[$key],$KM[$key],$DETAILS[$key]);
$stmt->execute();

When I do a print of the sql statement it comes out like this:

INSERT INTO timecarddetails (timecardId, tdate, hours, ot, ticketNumber, km, details) VALUES (24,2019-11-04,.5, , 2019450202,,Dispatched ticket)

Below is my table structure: enter image description here

Is there anything wrong with my syntax? If you need any further information, please let me know. Thanks in advance for helping!

1 Answer 1

3

$stmt->bind_param('isiisis', ...

i stands for integer values. For decimals, you need d.

Considering your table structure, you likely want:

$stmt->bind_param(
    'isddsds',
    $tcId,
    $JOBDATE[$key],
    $HOURS[$key],
    $OT[$key],
    $TICKET[$key],
    $KM[$key],
    $DETAILS[$key]
);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you...quite the simple oversight!
Welcome @jd0117! Glad that it helped.

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.