When requesting to add an entry to the mysql table, all fields are empty, but the entry is added File handler:

<?php $mysqli = new mysqli("localhost", "root", "", "db"); /* проверка соединения */ if ($mysqli->connect_errno) { printf("Соединение не удалось: %s\n", $mysqli->connect_error); exit(); } $query = "INSERT INTO Groups (groupname, course, housing) VALUES ('$_POST[groupName]', '$_POST[groupCourse]', '$_POST[groupHousing]')"; if ($result = $mysqli->query($query)) { echo "OK"; } /* закрытие соединения */ $mysqli->close(); ?> 
Ajax sending form data:

 $(function () { $("body").on('click', '#addG', function () { var gName = $("#groupName").val(); var gCourse = $("#groupCourse").val(); var gHousing = $("#groupHousing").val(); var contentG = $("#gAdd").serialize(); $.ajax({ url : "add.php", type : "POST", data : contentG, success: function (data) { alert(data); } }); }); }); 

The form itself:

 <form id="gAdd"> <div class="form-group"> <input type="text" class="form-control" name="groupName" id="groupName"> </div> <div class="form-group"> <input type="text" class="form-control" name="groupCourse" id="groupCourse"> </div> <div class="form-group"> <input type="text" class="form-control" name="groupHousing" id="groupHousing"> </div> </form> 
Where a jamb, ajax it seems sends not empty fields, and php creates record with empty fields.

  • How to check the data before adding to the database you do not want? - ArchDemon
  • Add a check to the code that the data is not empty and they will not be added - Yuriy Prokopets

0