There is such a request

if(isset($_POST['ajax']) && $_POST['ajax'] == 2 ) { if (!empty($_POST['number_points'])){ $USP1= $_POST['st_performance']; $USP2= $_POST['semestr_performance']; $USP3= $_POST['academicYear_performance']; $USP4= $_POST['subject_performance']; $USP5= $_POST['vidKont_performance']; $USP6= $_POST['national']; $USP6ECTS= $_POST['ECTS']; $USP6_100= $_POST['number_points']; $USP7= $_POST['date_performance']; $USP8= $_POST['lecturer_performance']; $USP9= $_POST['type_statement']; $USP10= $_POST['statement_performance']; for ($i=0; $i < count($USP1) ; $i++) { $mysqli->query("INSERT INTO usp (USP1, USP2, USP3, USP4, USP5,USP6_100,USP6,USP6ECTS, USP7, USP8,USP9,USP10) VALUES ('$USP1[$i]', '$USP2', '$USP3', '$USP4', '$USP5', '$USP6_100[$i]','$USP6[$i]','$USP6ECTS[$i]', '$USP7[$i]', '$USP8', '$USP9[$i]', '$USP10[$i]')"); } echo("Успешно добавлено"); } else { $USP1= $_POST['st_performance']; $USP2= $_POST['semestr_performance']; $USP3= $_POST['academicYear_performance']; $USP4= $_POST['subject_performance']; $USP5= $_POST['vidKont_performance']; $USP6= $_POST['national']; $USP6ECTS= $_POST['ECTS']; $USP6_100= $_POST['number_points']; $USP7= $_POST['date_performance']; $USP8= $_POST['lecturer_performance']; $USP9= $_POST['type_statement']; $USP10= $_POST['statement_performance']; for ($i=0; $i < count($USP1) ; $i++) { $mysqli->query("INSERT INTO usp (USP1, USP2, USP3, USP4, USP5,USP6_100,USP6,USP6ECTS, USP7, USP8,USP9,USP10) VALUES ('$USP1[$i]', '$USP2', '$USP3', '$USP4', '$USP5', NULL,NULL,NULL, NULL, '$USP8', NULL, NULL)"); } echo("Успешно добавлено"); } $mysqli->close() ; } 

It works, but partially, this part works.

 if(isset($_POST['ajax']) && $_POST['ajax'] == 2 ){ if (!empty($_POST['number_points'])){ $USP1= $_POST['st_performance']; $USP2= $_POST['semestr_performance']; $USP3= $_POST['academicYear_performance']; $USP4= $_POST['subject_performance']; $USP5= $_POST['vidKont_performance']; $USP6= $_POST['national']; $USP6ECTS= $_POST['ECTS']; $USP6_100= $_POST['number_points']; $USP7= $_POST['date_performance']; $USP8= $_POST['lecturer_performance']; $USP9= $_POST['type_statement']; $USP10= $_POST['statement_performance']; for ($i=0; $i < count($USP1) ; $i++) { $mysqli->query("INSERT INTO usp (USP1, USP2, USP3, USP4, USP5,USP6_100,USP6,USP6ECTS, USP7, USP8,USP9,USP10) VALUES ('$USP1[$i]', '$USP2', '$USP3', '$USP4', '$USP5', '$USP6_100[$i]','$USP6[$i]','$USP6ECTS[$i]', '$USP7[$i]', '$USP8', '$USP9[$i]', '$USP10[$i]')"); } echo("Успешно добавлено"); } 

That is, if there is a void, $ _POST ['number_points']) is empty, then it does not store NULL in the database. What could be the problem?

  • The problem may be that the 'NOT NULL' constraint is placed on this field in the table. In order to get an answer to the question add to the question the text of the error - Roman Andreev
  • So there is no error. Just initially, they are filled in the fields, but I need to make it so that if $ _POST ['number_points'] = empty, then the rest (which I put in the query NULL), then they were saved in the database with NULL. And the query doesn't do that, it saves with values, even if $ _POST ['number_points']) = NULL - Odium

1 answer 1

There are several errors in your code:

  • the else block is not enclosed in {} , so if !empty($_POST['number_points']) == true , duplicate records will be created (with and without null)

  • Not added as an error in the request. Before the last NULL an apostrophe

-

 NULL, NULL, NULL, '$USP8', NULL, 'NULL)"); ^вот тут 
  • corrected (above code), but why it still does not work - Odium