2 months ago by arjan Mulder

Lesson : Checking if a file needs approving

still getting a incorrect integer value 'on' for update 'live' field. i cant see where 'on' is defined. using mysql and 'live' field is a tiny int (1) (boolean)

''SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'on' for column 'live' at row 1 (SQL: update files set updated_at = 2017-08-09 12:59:13, live = on where id = 79) ''

and:

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'false' for column 'live' at row 1 (SQL: update files set updated_at = 2017-08-09 13:13:56, live = false where id = 79)

<input type="hidden" name="live" value="false"> //when i change this to 0 it works

How to handle this ?

2 months ago by Impelling Solutions

Best answer

Is it because "false" is a string? Maybe try casting it to a boolean in your query? Or just use value="false" maybe?

2 months ago by arjan Mulder

if ($request['live'] == "on") { $request['live'] = true; } else { $request['live'] = false; }

did the trick! Very strange.

2 months ago by Simion Chis

It is not strange. Your <input type="hidden" name="live" value="false"> is POSTed to server the "live" input as a string. You can do a var_dump($_POST); In your case on and true are strings not boolean and your database table expected a boolean. You can use ternary operator (short and easy to read).

$live = $request['live'] === "on" ? true : false;

// In this case $live is type of boolean (not string) and it is safe to throw in database table
var_dump($live);

1 week ago by adi mumin

2/2) QueryException SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'on' for column 'live' at row 1 (SQL: update files set live = on, updated_at = 2017-10-08 08:46:52 where id = 1)

Best answer

Is it because "false" is a string?

I tried

<input type="hidden" name="live" value="0"> <input type="hidden" name="live" value=0>

<input type="hidden" name="live" value="bool(false)"> ??

Doesn't work

Please help

1 week ago by Simion Chis

Doesn't work because "false" is a string.

<input type="hidden" name="live" value="bool(false)"> this construction will not work. bool(false) it is not evaluated to false. In fact it is not evaluated at all. It is just a simple value (literal string) . On server side you will have

echo  $_POST['live'];
// Output: bool(false) 

On your form you can have <input type="hidden" name="live" value="false">. After submit, on server side ...


// now the $live will hold a boolean value
$live = $_POST['live'] === "false" ? false : true;

// so your SQL 
$sql = "update files set live = '{$live}' , updated_at = 2017-10-08 08:46:52 where id = 1"

5 days ago by adi mumin

Thank you Chris

I am new to all this. I still don't understand. Can you explain where to put this code

echo $_POST['live']; // Output: bool(false)

// now the $live will hold a boolean value $live = $_POST['live'] === "false" ? false : true;

And can you explain why it works in Alex's code without all this?

Where exactly do i have to make these changes?

thx

5 days ago by Simion Chis

I asume that you have a form in some index.html file and a development server to work with

<form method="POST" action="update_something.php"  >
    <input type="hidden" name="live" value="false">
    .
    <!-- another input fields related to your project  -->
    . 
    <input type="submit" value="Do something" >

    
</form>

When you hit the submit button, the form is send back to server. Now in update_something.php you have to grab the values from that form. The superglobal $_POST holds all the values from the form above. So in $_POST['live'] you will have word/value "false" which it is string. Not a boolean as database expect. By doing $live = $_POST['live'] === "false" ? false : true; you will transform the string "false" into a boolean value and this bollean will be stored in variable $live.

I hope that this will rise the fog.

3 days ago by adi mumin

Thanks Chris

I will try to resolve the problem.

Sign in or create an account to reply to this topic.