0

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.

11
  • This code does not include part that executes those queries. Did you not write it or just didn't show us? Commented Jul 18, 2018 at 9:53
  • 1
    if($_POST['iname']) foreach($_POST['iname'] as $iname) What are you trying to do with these IF / FOREACH statements? Commented Jul 18, 2018 at 9:57
  • @ŁukaszKamiński you are right. I forget to cancel &query. Commented Jul 18, 2018 at 10:05
  • @RiggsFolly In the index.php i have implement a dynamic form, in which you can add a x number of people. Now i Add a part of code .js Commented Jul 18, 2018 at 10:05
  • 1
    so if i understand correct, you dont know how to execute the query. read in the man php.net/manual/de/function.pg-query.php , then read about prepared statements. php.net/manual/de/function.pg-execute.php Commented Jul 18, 2018 at 10:29

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

try to escape the inputs before executing the query as well
Thanks for the answer! I use your code ( i add the connection to db ) and i don't receive any error. I receive the messagge that the connection with DB is ok, but if i go to see the tables i don't have nothing inserted.
that script is just building the query, in order to insert it to the db you would need to use pg_query or prepared statements or PDO as suggested by @FatFreddy
Ok, so i read what he suggest to me. I should insert pg_query where you write "execute the query here", right?
Ok thanks you a lot, now i receive an error i belive because i don't have insert the ID value ( because at the moment i search a method to concatenate the 3 tables with a value, ID )
|
0

the simple way is the beast way

$nombre = $_POST['nombre'];
$result = pg_query($conexion, "INSERT INTO nombre VALUES('$nombre')");
if (!$result){
echo "You have a problem";
exit;
};

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.