Check for existence of register with 3 conditions with PHP

0

I am new to PHP and I am trying to check if there is an equal registration in the database before registering, but so far, the problem is that I can not verify with 3 conditions in WHERE SELECT ex:

SELECT * FROM datashowreserva
WHERE DATASHOW = '{$datashow}' AND 
DATA_RESERVA = '{$data_reserva}' AND
HORARIO_RESERVA = '{$horario_reserva}'

The select itself works because I tested it in mysql, it's the program in php that I think is wrong.

Below is what I've tried:

    $datashow = $_SESSION['select_datashow'];
    $data_reserva = $_SESSION['data_reserva_datashow'];
    $horario_reserva = $_SESSION['select_horario_reserva_datashow'];

    $sql_code_select = "SELECT * FROM datashowreserva WHERE DATASHOW = '{$datashow}' AND DATA_RESERVA = '{$data_reserva}' AND HORARIO_RESERVA = '{$horario_reserva}'";

    $sql_query = mysqli_query($server_mysql,$sql_code_select);

    if (mysqli_num_rows($sql_query) > 0) {

    echo "<script language='javascript' type='text/javascript'>alert('Essa reserva já existe.');window.location.href='Cadastrar_Datashow.php';</script>";

    }
    
asked by anonymous 22.08.2017 / 22:17

2 answers

0
echo in select to see how it is returning, to see if it is correct I would ride like this:

$sql_code_select = "SELECT * FROM datashowreserva WHERE DATASHOW = '".$datashow."' AND DATA_RESERVA = '".$data_reserva."' AND HORARIO_RESERVA = '".$horario_reserva."'";

Another thing if it were you would study PDO I think it is more consistent and is even used in frameworks.

    
22.08.2017 / 23:17
0

Now, my comrades are right, I have converted the date to the 'y-m-d' format that the database accepts, next to the variable declaration and before doing the query like this:

$data_reserva = date('y-m-d', strtotime(str_replace('/', '-', $_SESSION['data_reserva_datashow'])));

I also did the select this way:

$sql_code_select = "SELECT * FROM datashowreserva WHERE DATASHOW = '".$datashow."' AND DATA_RESERVA = '".$data_reserva."' AND HORARIO_RESERVA = '".$horario_reserva."'";

The complete solution looks like this:

    $datashow = $_SESSION['select_datashow'];
    $data_reserva = date('y-m-d', strtotime(str_replace('/', '-', $_SESSION['data_reserva_datashow'])));
    $horario_reserva = $_SESSION['select_horario_reserva_datashow'];

    $sql_code_select = "SELECT * FROM datashowreserva WHERE DATASHOW = '".$datashow."' AND DATA_RESERVA = '".$data_reserva."' AND HORARIO_RESERVA = '".$horario_reserva."'";

    $sql_query = mysqli_query($server_mysql,$sql_code_select);

    if (mysqli_num_rows($sql_query) > 0) {

    echo "<script language='javascript' type='text/javascript'>alert('Essa reserva já existe.');window.location.href='Cadastrar_Datashow.php';</script>";

    }   

Thank you very much!

    
23.08.2017 / 00:45