0

i have created a page to insert element into a database in php. At the beggining i start use with mySQL, and it works perfectly. Then I'm migrate to Postgres and so i had some problems. I change my code and i don't receive erros,but when i'm trying to insert my elements into the db, this remains empty.

this the structure of my table Prenotazione

CREATE TABLE public."Prenotazione"
(
  id integer NOT NULL DEFAULT nextval('"Prenotazione_id_seq"'::regclass),
  nominativo character(20),
  email character(20),
  oggetto character(200),
  nominativoi character(200),
  nominativoe character(200),
  emaili character(200),
  emaile character(200),
  data date,
  orario_inizio time without time zone,
  orario_fine time without time zone,
  stanza integer
)

this is my index.php

<form method="post" action="input.php">
                        <b>&ensp;Richiedente Conferenza:</b><br><br>
                        &ensp;Nominativo:<br>&ensp;<input type="text" name="nominativo" placeholder="Nome Cognome" size="20"><br>
                        &ensp;Email: <br> &ensp;<input type="email" name="email" size="20" placeholder="email"><br>
                        &ensp;Oggetto Conferenza:<br>&ensp;<textarea name="oggetto" rows="5" cols="40" placeholder="Specificare oggetto Videoconferenza"></textarea><br>
                        &ensp;Data: <br>&ensp;<input  type="date" name="data"  ><br>
                        &ensp;Orario Inizio: <br>&ensp;<input type="time" name="orario_inizio" min="09:30:00" max="16:30:00" ><br>
                        &ensp;Orario Fine: <br>&ensp;<input type="time" name="orario_fine" min="10:00:00" max="18:30:00"><br>

                        <br>
                        <b>&ensp;Partecipanti Interni </b>
                        <br>
                        <br>
                <div id="interni">
                    <div id="first">
                        &ensp;Nominativo:<br>&ensp;<textarea name="nominativoi" rows="5" cols="30" placeholder="Nome Cognome;"  ></textarea><br>
                        &ensp;Email:<br>&ensp; <textarea  name="emaili" rows="5" cols="30" placeholder="Inserire Email"></textarea><br> 
                      <br>
                      <br>
                    </div>
                </div>
                        <b>&ensp;Partecipanti Esterni </b>
                <div id="esterni">
                    <div id="first">
                               &ensp;Nominativo:<br>&ensp;<textarea name="nominativoe" rows="5" cols="30" placeholder="Nome Cognome;" ></textarea><br>
                                &ensp;Email:<br>&ensp;<textarea  name="emaile" rows="5" cols="30" placeholder="Inserire Email"></textarea><br> 
                      <br>
                    &ensp;&ensp;<input type="submit" value="Invia" > 
                        </form>

And finally the input.php

$checkdata = "SELECT count(*) as prenotato
  FROM Prenotazione
 WHERE data='$data'
   AND NOT ('$newTimeEnd' < orario_inizio OR orario_fine < '$orario_inizio')";

$querycheck = $dbh->prepare($checkdata);
$querycheck->execute();
$prenotato = $querycheck->fetch()[0];
var_dump($prenotato);
if ($prenotato == 0 AND $stanza == 0 ) { 

    $query1 = "INSERT INTO Prenotazione (nominativo,email,data,orario_inizio,orario_fine,oggetto,nominativoi,emaili,nominativoe,emaile,stanza) VALUES ('$nominativo','$email','$data','$orario_inizio','$newTimeEnd','$oggetto','$nominativoi','$emaili','$nominativoe','$emaile',1)";
    var_dump($query1);
    $result1 = $dbh->prepare($query1);
    $result1->execute();
    $rex = 1;
}
else if ($prenotato == 1){
    $query1 = "INSERT INTO Prenotazione (nominativo,email,data,orario_inizio,orario_fine,oggetto,nominativoi,emaili,nominativoe,emaile,stanza) VALUES ('$nominativo','$email','$data','$orario_inizio','$orario_fine','$oggetto','$nominativoi','$emaili','$nominativoe','$emaile',2)";
    $result1 = $dbh->prepare($query1);
    $result1->execute();
    $rex = 1;
}

I posted the part that are interesting to the problem. Thanks

The result of var_dump is :

NULL string(269) "INSERT INTO Prenotazione (nominativo,email,data,orario_inizio,orario_fine,oggetto,nominativoi,emaili,nominativoe,emaile,stanza) VALUES ('username','email@email.com','2018-10-03','09:30','12:30','object','username1','email1@email1.com','username2','email2@email2.com',1)"

MrRobinson
  • 39
  • 7
  • Maybe the statement causes an error. Did you check? – Laurenz Albe Oct 03 '18 at 10:04
  • @LaurenzAlbe how can i check ? The stranger thing is that in mysql the code goes well. – MrRobinson Oct 03 '18 at 10:06
  • I have try the only insert into database and he doesn't work. I tried to print the variables like var_dump($nominativo) and it takes this. So i don't understand where is the problem – MrRobinson Oct 03 '18 at 10:15
  • Check if `execute` returns `true` or `false`. – Laurenz Albe Oct 03 '18 at 10:17
  • I try and it prints "Notice: Undefined variable: params in /opt/lampp/htdocs/PhpProject1/inputs.php on line 41 It failed!"! How can i do now??? – MrRobinson Oct 03 '18 at 10:29
  • I used this code: $try = $querycheck->execute($params); if ($try) { echo 'It worked!'; } else { echo 'It failed!'; } – MrRobinson Oct 03 '18 at 10:30
  • Well the failure you're getting now is to do with your php code, but as it was not part of the code posted above (there's no `params` variable) it's difficult to say... – 404 Oct 03 '18 at 11:43
  • @eurotrash oh my fault! I copied the code from a tutorial and i forget to change $params variable. How can i do to see if execute is true or false? – MrRobinson Oct 03 '18 at 12:19
  • Not sure, I only know postgres not php. If you have access to your postgres server you can check the log file to see if there was any error. – 404 Oct 03 '18 at 13:53
  • Ok :( i didn't find nothing – MrRobinson Oct 03 '18 at 15:06

1 Answers1

0

The problem is with your table name in double quotes. Don't use double quotes, or use them in every request.

You can verify your code here:

http://rextester.com/VIEH75483

Also, see more: Omitting the double quote to do query on PostgreSQL

Ilya Konyukhov
  • 2,666
  • 1
  • 12
  • 21