i don’t understand why i can’t insert into DB. I have create a DB with this table:
prenotazione (id,nome_rich,cognome_rich,email_rich,oggetto_rich)
interni (id,nome_int,cognome_int,email_int)
esterni (id,nome_est,cognome_est,email_est)
this is my index.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <title>Prenotazione Videoconferenza</title> <script src="utils.js"></script> </head> <body> Inserire i dati richiesti:<br><br> <form method="post" action="input.php"> <b> Richiedente Conferenza:</b><br><br> Nome:<input type="text" name="name" size="20"><br> Cognome:<input type="text" name="surname" size="20"><br> Email: <input type="email" name="email" size="20"><br> Oggetto Conferenza:<br><textarea name="testo" rows="5" cols="40" placeholder="Specificare oggetto Videoconferenza"></textarea><br> <br> <b>Partecipanti Interni</b> <br> <br> <div id="start"> <div id="first"> Nome:<input type="text" name="iname[]" size="20"><br> Cognome: <input type="text" name="isurname[]" size="20"><br> Email: <input type="email" name="iemail[]" size="20"><br> <br> </div> </div> <br> Numero partecipanti interni: <input type="text" id="n1" value="1"><br> <button><a href="#" id="add">Aggiungi partecipante</a></button> <br> <b>Partecipanti Esterni</b> <br> <br> Numero partecipanti Esterni: <input type="text" id="n2" value="1"><br> <button><a href="#" id="add2">Aggiungi partecipante</a></button> <div id="start2"> <div id="first2"> Nome:<input type="text" name="ename[]" size="20"><br> Cognome: <input type="text" name="esurname[]" size="20"><br> Email: <input type="email" name="eemail[]" size="20"><br> <br> </div> </div> <input type="submit" value="Invia" > </form> </body> </html>
And this is the input.php that i used to insert the data ( and also to connect to DB )
<?php $conn = @pg_connect("dbname=postgres user=postgres password=123456789"); if(!$conn) { die('Connessione fallita !<br />'); } else { echo 'Connessione riuscita !<br />'; } // Richiedente $name = $_POST['name']; $surname = $_POST['surname']; $email = $_POST['email']; $testo = $_POST['testo']; // Interni if($_POST['iname']) foreach($_POST['iname'] as $iname) if($_POST['isurname']) foreach($_POST['isurname'] as $isurname) if($_POST['iemail']) foreach($_POST['iemail'] as $iemail) // Esterni if($_POST['ename']) foreach($_POST['ename'] as $ename) if($_POST['esurname']) foreach($_POST['esurname'] as $esurname) if($_POST['eemail']) foreach($_POST['eemail'] as $eemail) //inserting data order INSERT INTO prenotazioni (id,nome_rich, cognome_rich, email_rich,oggetto_rich) VALUES (1,'$name','$surname', '$email','$testo'); INSERT INTO interni (nome_int, cognome_int, email_int) VALUES ('$iname','$isurname','$iemail'); INSERT INTO esterni (nome_est, cognome_est, email_est) VALUES ('$ename','$esurname','$eemail'); ?>
utils.js
$(document).ready(function() { $("#add").click(function(){ var val1 =$("#n1").val(); for(var i=0;i<val1;i++){ $("#start").append($("#first").clone()); } }); }); $(document).ready(function() { $("#add2").click(function(){ var val2 =$("#n2").val(); for(var i=0;i<val2;i++){ $("#start2").append($("#first2").clone()); } }); });
I have test the connection to DB and i Receive the positive message ( in the echo ). But if i controll the tables they are empty.
Advertisement
Answer
Try the below code
// Richiedente $name = $_POST['name']; $surname = $_POST['surname']; $email = $_POST['email']; $testo = $_POST['testo']; //Connecting to db here $conn_string = "host=localhost port=5432 dbname=test user=lamb password=bar"; // change the db credentials here $conn = pg_connect($conn_string); //inserting data order $query1 = "INSERT INTO prenotazioni (id,nome_rich, cognome_rich, email_rich,oggetto_rich) VALUES (1,'$name','$surname', '$email','$testo')"; //execute the query here $result = pg_query($conn, $query1 ); //if you are using pg_query and $conn is the connection resource // Interni $query = ""; if( !empty( $_POST['iname'] ) ) { foreach( $_POST['iname'] as $key => $iname ) { $isurname = empty( $_POST[$key]['isurname'] ) ? NULL : $_POST[$key]['isurname']; $iemail = empty( $_POST[$key]['iemail'] ) ? NULL : $_POST[$key]['iemail']; $query .= " ( '$iname', '$isurname', '$iemail' ) "; } } if( !empty( $query ) ) { $query2 = "INSERT INTO interni (nome_int, cognome_int, email_int) VALUES ".$query; $result = pg_query($conn, $query2 ); //if you are using pg_query and $conn is the connection resource } // Esterni $query = ""; if( !empty( $_POST['ename'] ) ) { foreach( $_POST['ename'] as $key => $ename ) { $esurname = empty( $_POST[$key]['esurname'] ) ? NULL : $_POST[$key]['esurname']; $eemail = empty( $_POST[$key]['eemail'] ) ? NULL : $_POST[$key]['eemail']; $query .= " ( '$ename', '$esurname', '$eemail' ) "; } } if( !empty( $query ) ) { $query3 = "INSERT INTO esterni (nome_est, cognome_est, email_est) VALUES " . $query; $result = pg_query($conn, $query3 ); //if you are using pg_query and $conn is the connection resource }
This code is trying to insert into thte database as a batch. Try to echo the query string, to see the sql query created and run it on PG to see if there is any issues with the query.