0

I would like a help, I converted the input string from date to array and if I do not input for example 05/26/2019, all 3 fields did an explode to flip an array, then this error occurs. what can I do?

escola.php

 <form method="POST" class="PesquisarData">
    <h5>Pesquisar Data:</h5>
    <input type="text" id="data" name="data" class="selectData">
    <input type="submit" value="Pesquisar" class="buttonData"><br/>
 </form>

if (!empty($_POST['data'])) {
        $_POST['disciplina'] = "";
        $_POST['professor'] = "";

        $data = explode('/', addslashes($_POST['data']));
        $data = $data[2].'-'.$data[1].'-'.$data[0];
        $sqlm = "SELECT * FROM {$pfx}Material WHERE Data LIKE CONCAT('%',?,'%') ORDER BY Data DESC";
        $qrym = $conn->prepare($sqlm);
        $qrym->bindParam(1, $data);
        $qrym->execute() or die('Ocorreu um erro em uma operação do banco de dados: VL0002.2');
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • What does `addslashes()` do in your code? – Dharman May 31 '19 at 21:13
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 May 31 '19 at 21:14

2 Answers2

1

Check that $data has 3 elements before trying to use it.

$data = explode('/', addslashes($_POST['data']));
if (count($data) != 3) {
    die('Invalid date ' . $_POST['data']);
}
$data = $data[2].'-'.$data[1].'-'.$data[0];
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • It worked, thanks, so how do I go back to the page I was in, tried with header, but said that the header was already used to open this page in that session? – RevolveJr games May 31 '19 at 20:52
  • 1
    Don't use `die()` to report the error, use a redirect instead. – Barmar May 31 '19 at 20:53
0

Your programming assumes that the user also uses the slash that you explode! You have to check before the sender whether he slashed these 2 - which you require - also enters

Toxi
  • 109
  • 5