How to convert 0 to null (Error inserting NULL into an INT field from a variable)

Asked

Viewed 3,361 times

1

I am editing this question to be more didactic for other members, since there are only publications addressing this situation outside the community en.

I own a field INT property UNIQUE that may have value NULL.

The value that will be inserted in this field comes from a variable that may have value NULL. The problem is that by doing the INSERT, if this variable has a null value, the value 0 (zero) in this field, which will cause problems when entering a new record thanks to the property UNIQUE.

Follow the example that will generate this error:

$varExemplo = null;
$sql = "INSERT INTO usuario (campoIntExemplo) VALUES ('".$varExemplo."');
$query = $mysqli->query(sql);
  • What will be the difference between an Index and the other? could detail better?

  • Why are you saving like zero? I ask you to put an example of your INSERT in full.

  • I edited the publication to be more explanatory.

3 answers

2

You can use a ternary to properly format SQL if there is a value that gives the variable its own value plus simple quotes (in the case of a varchar for numeric types do not add them) otherwise return the null literal.

That’s just one example. It is very important to use Prepared statements or correct healing and escaping user-submitted values to avoid problems with sql Injection.

$inputVazio = empty($_POST['inputVazio']) ? 'null' : (int) $_POST['inputVazio'];
$sql = "INSERT INTO (c1, c1, c3) VALUES('v1', $inputVazio, 'v3')";

Possible exits:

INSERT INTO (c1, c2, c3) values('v1', null, 'v3')
INSERT INTO (c1, c2, c3) VALUES('v1', 'novo valor', 'v3')
  • rray, still the value "0" is stored in the INT field. I believe the only way to make the INT field value NULL is by not trying to enter data into this field. Therefore it would be necessary a kind of IF inside the INSERT that checks if the value of the field is NULL, if yes, INSERT INTO (C2) VALUES $inputVazio; must be ignored.

  • @Hugoguitti got to see the generated sql? in it the value goes as zero even? it seems that mysql is converting the value, because php would not do this since 'null' is a string (which is different from null quote-free).

  • I did as follows: $inputVazio = $_POST['inputVazio']; $inputVazio = Empty($iinputVazio) ? null : $inputVazio; "".

  • @Hugoguitti in that code there are missing simple quotes in null, it is mandatory to have them. Look more calmly at the answer or the last comment.

  • I also tested with simple quotes, in this case echo showed null but in the database was inserted the value 0. But out of curiosity, if my object is to insert nothing in an int field, why should I assign a string value to the variable that will be stored in this int field?

  • @Hugoguitti has some default value for that column, looked at it in the table?

  • The Default value of this field is NULL.

  • @Hugoguitti in php you need to assign the literal 'null' to prevent it from converting some values such as ('', null, false) at zero.

  • But that’s not what the example you set for me does?

  • @Hugoguitti The idea is that your sql will be mounted with null written. If you go with zero you have some point of interference that converts the value.

  • From what I understand from my research, NULL cannot be inserted into an int field. So the only way would be to even mention the field in INSERT. Example if $inputVazio has any value: INSERT INTO (C1, C2, C3) VALUES (v1, $inputVazio, v3); Example if $inputVazio for NULL: INSERT INTO (C1, C3) VALUES (v1, v3); inside the INSERT that deletes both C2 and $inputVazio script.

  • @Hugoguitti you can record null in any kind of field as long as he is not NOT NULL

  • So I don’t know what might be converting this value, pq $inputVazio is only used in my code to store the value of $_POST in addition to INSERT.

  • I solved the problem, thank you very much for your help!

Show 9 more comments

2


I figured out how to solve this problem!

When null values are inserted from a variable PHP in a field INT of the database, the PHP converts null to "" (empty), which is a value STRING, then this empty value was converted to INT when it arrived at the database, generating the value 0 (zero).

The solution was to give a UPDATE after the INSERT using the last id inserted as a parameter for the WHERE.

Following example:

//Assigns the value null to the variable $man

$varExemplo = null;

//Here I created the query that will insert the value of the $varEmplo variable in startEmployment. NOTE: The query will also insert null in the primary field campoPk, as this field is auto_increment, it will generate the record ID automatically.

$sql = "INSERT INTO usuario (campoPk, campoIntExemplo) VALUES (null,'".$varExemplo."');
$query = $mysqli->query(sql);

//With the constant insert_id i take the generated ID for the record we generate with INSERT, and assign this id to the variable $user.

$idUsuario = mysqli->insert_id;

//Finally I make a update using the value of $usuarioId as parameter.

$sql = "UPDATE usuario SET id_visamg = null WHERE id_visamg= 0 and id_usuario = $idUsuario";
$query = $mysqli->query($sql)

Thank you so much for all of @rray’s efforts to help me solve the problem.

1

I believe that’s possible, yes. In that reply has something like what you want:

INSERT INTO etc(campo1, campo2)
VALUES
(
   (CASE campo1 WHEN '' THEN NULL ELSE campo1 END),
   (CASE campo2 WHEN '' THEN NULL ELSE campo2 END)
);

Note: the example got ugly because you did not give many details of table/ fields, but just adapt.

  • Dude, I tried to do it this way but it didn’t work, is it because I’m using a concatenated variable instead of "field1" inside CASE? Here’s an example of how I’m doing: (CASE'". $var1." 'WHEN' THEN NULL ELSE '". $var1." 'END),

  • This shouldn’t be a problem; but it wouldn’t be the case that you do the if before and already fill in $var1 with the data that must be recorded?

  • rLinhares, I tried to do IF before INSERT, as rray suggests, but even in its example the stored value is 0. When I used your CASE example, the stored value was NULL when the value of $var1 is NULL, but the problem is it also stored NULL when $var1 had a value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.