D
D
DmitryOxi2020-11-22 01:15:17
PHP
DmitryOxi, 2020-11-22 01:15:17

How to add NULL from PHP to MySQL DB?

Friends, tell me.
When trying to enter the database, an error occurs:
Incorrect integer value: '' for column 'nameN' at row 1.
The field type is INT, it is possible to add NULL.
SQL is trying to add something (not NULL), but not an int either.
I cannot rule this out. Changing just the type to int = 0 is also not an option, since for some id it exists and is logically important.

How to add NULL ? How to be?

$sql = "
  INSERT INTO users_units 
  SET 
  `date`='$date', 
  `name1`='$array1[name1]', 	
  `name2`='$array1[name2]', 
  ........       ........
  `name30`='$array1[name30], 

  ON DUPLICATE KEY UPDATE 
  `date`='$date', 
  `name2`='$array1[name2]', 
   ........       ........
  `name30`='$array1[name30]'
";


One more question right away, if you will :) , how can I shorten the code, it takes about 100 lines in the end, and the logic is elementary.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2020-11-22
@galaxy

This is how it is written in SQL SET `name1` = NULL ..., NULL - without quotes.

F
FanatPHP, 2020-11-22
@FanatPHP

Always execute queries through prepared expressions only. This solves a LOT of problems right away, including your null pain.
As for automation, there are options, but it seems to me that in your case this liver sausage from name1 - name30 should be cut into pieces and written humanly in separate columns . And then there will be no problems automating requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question