0) { while($row = mysqli_fetch_array($result)) { $start_date = $row['start_date']; $final_date = $row['final_date']; $start_num = $row['start_num']; $final_num = $row['final_num']; $rate = $row['rate']; $comment = $row['comment']; } } #Validate ownership $link_user_id = $_GET['user_id']; $session_user_id = $_SESSION['user_id']; if ($link_user_id != $session_user_id) { header("Location: index.php"); } #Post method if ($_SERVER["REQUEST_METHOD"] == "POST") { #Collect data from html form via POST request method $start_date = $_POST['start_date']; $final_date = $_POST['final_date']; $start_num = $_POST['start_num']; $final_num = $_POST['final_num']; $rate = $_POST['rate']; $comment = $_POST['comment']; #Define error variables and set to empty valuse $dateErr = $numErr = $rateErr = $commentErr = NULL; #Validate form and catch errors #Date confirmation if(empty($start_date) || empty($final_date)) { $dateErr = "Вы не ввели дату"; } #Num confirmation if ($start_num < 0 || empty($final_num)) { $numErr = "Вы не ввели показания счётчика"; } #Rate confirmation if (empty($rate)) { $rateErr = "Вы не ввели тариф"; } #Comment validation if (strlen($comment) > 255) { $commentErr = "Коммнтарий не можеть быть длиннее 255 символов"; } #Post data to the database is there are no errors if ($dateErr == NULL && $numErr == NULL && $rateErr == NULL && $commentErr == NULL) { if ($receipt_id == NULL) { $query = "INSERT INTO Receipts (book_id, start_date, final_date, start_num, final_num, rate, comment) VALUES ('$book_id', '$start_date', '$final_date', '$start_num', '$final_num', '$rate', '$comment')"; if (mysqli_query($conn, $query)) { echo "New record has been created successfully"; header('Location: book.php?user_id='.$session_user_id.'&book_id='.$book_id.''); } else { echo mysqli_errno($conn) . " : " . mysqli_error($conn); } } else { $query = "UPDATE Receipts SET start_date = '$start_date', final_date = '$final_date', start_num = '$start_num', final_num = '$final_num', rate = '$rate', comment = '$comment' WHERE (receipt_id = '$receipt_id')"; if (mysqli_query($conn, $query)) { echo "Receipts has been updated"; header('Location: book.php?user_id='.$session_user_id.'&book_id='.$book_id.''); } else { echo mysqli_errno($conn) . " : " . mysqli_error($conn); } } } mysqli_close($conn); } ?>