Check for 3-condition registration with PHP

Asked

Viewed 244 times

0

I am new to PHP and I am trying to check if there is equal record in the database before registering, but until then everything right, the problem is that I can not check with 3 conditions in WHERE of SELECT ex:

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

select itself works because it tested in mysql, is the php program 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>";

    }
  • 'Cause you’re wearing it { } even?

  • That must be the problem, your date must be like this 2017-08-22 I imagine the user sends 22/08/2017

  • @rray is informed in the field in the format dd/mm/yyyy then I use it to cause the date to enter in the format yyyy-mm-dd accepted by the bank. //Catch date entered in the HTML field in the format dd/mm/yyyy $data = $_SESSION['data_reserva_datashow']; //Exploding the date to enter the format accepted by BD $dataExplode = explodes('/', $data); $dataNoFormatoParaOBanco = $dataExplode[2]. '-'. $dataExplode[1]. '-'. $dataExplode[0];

  • Your field in the field is date or datetime?

  • @rray is in date

  • Using ' } is wrong? @white

  • If you give a echo $sql_code_select; how the query is assembled?

  • @Lucasbarbosafonseca in this situation there I do not usually put.

  • @rray is mounted with the variable value instead of the variable in select

  • Friend, check the date if it is in this format: 2017-08-22. Otherwise you do not pass in this format should convert the format so: $datashow = date('Y-m-d',strtotime(str_replace('/', '-', $_SESSION['select_datashow']))) before carrying out the consultation.

  • And you also don’t need {} to declare a variable in a string with "". So if so: $variavel['offset']

  • @I’ll check the bank?

  • In what format is your date in the variable? So: 22/08/2017? If it is should change to the format that the bank accepts it and so: 2017-08-22.

  • To make this conversion before making the query one can do so: $datashow = date('Y-m-d',strtotime(str_replace('/', '-', $_SESSION['select_datashow'])))

  • the explode is correct, returns yyyy/mm/dd the problem is at another point

  • The schedule reserves as it is in the bank and as it comes from the Second?

  • @Leocaracciolo the time is varchar, I am using html select option

  • when vc explodes the date the variable name is $dataNoFormatoParaOBanco and in query vc uses different variable name. Vc checked this?

  • I’m setting up a table in the database to do some tests, only instead of using Sesssions I’ll use dates even though I believe your Sesssions returns the dates correctly

  • I tested with the following data 22/08/2017 - 20/08/2017 - 10:12:00 In the database 2017-08-22 - 2017-08-20 - 10:12:00 I checked the application and gave an alert "This reservation already exists." and threw me on another page. So the error must be in some other part of your application, perhaps in the form, in the construction of the Sesssions, etc.. and that you didn’t post in your question

  • see query SELECT * FROM datashowreserva WHERE DATASHOW = '2017-08-22' AND DATA_RESERVA = '2017-08-20' AND HORARIO_RESERVA = '10:12:00'

Show 16 more comments

2 answers

0

Of the one echo in select to see how it is returning, to see if it is correct I’d ride like this:

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

Anything else if I were you I’d study PDO I find it more consistent and is more used even in frameworks.

0

Now it worked my comrades, I made the conversion of the date to the format 'y-m-d' that the database accepts, together with the variable declaration and before making the query in this way:

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

I also selected it that way:

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

The complete solution was 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 friends!

  • I used your exploding code, which is there in the comets, in the dates and worked out too. I didn’t change the query that’s there in your question and it worked too. So I don’t think the reasons given in your answer are the problems. Strange this. Anyway, what matters that it’s working and zefini :)

  • How I gave you the problem was with the date format. Now the question about changing the query I think it’s best to use the declared variable without closing and opening quotes. type like this: $sql_code_select = "SELECT * FROM datashowreserva WHERE DATASHOW = '$datashow' AND DATA_RESERVA = '$data_reserva' AND HORARIO_RESERVA = '$horario_reserva'";

  • The better the performance and insignificant, the more the code gets cleaner. These are things you will begin to understand over time. {} in strings is used when you need to pass value of a position of some array, guy: $sql_code_select = "SELECT * FROM datashowreserva WHERE DATASHOW = '{$datas['show']}' AND DATA_RESERVA = '{$datas['reserva']}' AND HORARIO_RESERVA = '{$datas['horario_reserva']}'";

  • You who are starting with PHP, would indicate you practice some activities, solving some problems and studying the documentation of it. Then start making systems with frameworks. It will help you a lot. Today I work with Codeigniter, but has CakePHP, Zend, Laravel and many others. I have cited the most used. Plus what is fashionable today and the Laravel. Search for why to use Frameworks.

  • Thanks for the tips guys

Browser other questions tagged

You are not signed in. Login or sign up in order to post.