I have a form with a date selection:

<form method="post" name="date" action="date.php" > <input class="form-control" type="date" name="delivery_date" value="<?=htmlspecialchars(date('Ym-d', strtotime($users['delivery_date'])), ENT_QUOTES)?>" id="example-date-input"> <button type="button" class="btn btn-primary btn-xs mb-3">Найти</button> </form> 

I need to remove from the database all the records for the specified date, I try to do this:

 <?php require_once('db.php'); if(isset($_POST['date'])) { $date = ($_POST['date']); $SQL = "SELECT * FROM Users WHERE delivery_date='$date' AND delivery='1' ORDER BY id DESC"; print_r ($_POST); $result = mysqli_query($link, $SQL); if ($result) { header('Location:print.php'); } else { printf("Ошибка: %s\n", mysqli_error($link)); } } ?> 

Is the screen correct with the request? And how to correctly display the data? I am trying to display all records for the specified date.

  • completely wrong. A huge bunch of mistakes - Ipatiev
  • @ Ipatiev can then at least explain? :( - web2k17
  • It is impossible to teach programming in comments with a length of 20 words. You do not write meaningful code, you collect it from cubes, simply piling them on each other, pulling out from some other place. So that I can explain, you have to understand for yourself what $ dbtable is and where it came from here at all. Why is there a function? where $ date should come from. I suggest starting with the basics. First master SQL. then learn to work with the database through PDO and prepared expressions. then learn how to work with data from forms. And only then try to wrap it all in function - Ipatiev
  • @ Ipatiev I did not attach the code entirely, corrected it. $ Dbtable is the name of the table, is taken from another file. - web2k17
  • it is necessary to remove the function and change the code so that it executes the query through the prepared expressions, rather than inserting variables directly into the query - Ipatiev

1 answer 1

The name of the table should not be taken from the file, but written directly in the query.
PDO should be used instead of mysqli. Code for db.php taken from here
The function to remove from the code, it does not make sense here.

 <?php require_once('db.php'); if(isset($_POST['date'])) { $sql = "SELECT * FROM table WHERE delivery_date=? ORDER BY id DESC"; $stmt = $pdo->prepare($sql); $stmt->execute([$_POST['date']]); $del_date = $stmt->fetchAll(); } 

If $ _POST ['date'] contains a date in the format 2019-03-29 then after executing this code, the $ del_date variable will have an array with all the entries for the desired date

  • PDO should be used instead of mysqli. Is this imposing your opinion, or recommendation?) - Dizzy221
  • @ Dizzy221 rewrite this code on mysqli, compare what happened - Ipatiev
  • Most importantly, how can I correctly display this on the page? - web2k17 3:31 pm
  • @ web2k17 in a loop. any operations with arrays are performed using cycles - Ipatiev