0

i'm creating a simple web app which a form to insert data in DB ( Postgres).

I have this tables:

prenotazione (id,nome_rich,cogn_rich,email_rich,oggetto_rich)

interni (id,nome_int,cogn_int,email_int)

esterni (id,nome_est,cogn_est,email_est)

Now about the tables "interni" and "esterni" the users should choose how many data want to insert, so i have implemented a dynamic form:

**index.php

<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>
  <b> Numero partecipanti interni:</b>
  <input type="text" id="n1" value="1"><br>
  <button><a href="#" id="add1">Aggiungi partecipante</a></button>
  </div>

**input.php

$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$testo = $_POST['testo'];

//inserting data order
$query1 = "INSERT INTO prenotazione (id,nome_rich, cogn_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, cogn_int, email_int) VALUES ".$query;
    $result = pg_query($conn, $query2 );

The problem is that when they try to insert data into "interni" and the data are more than one, they can't and i receive an error. ( basically the table "interni" is empty when they try to insert more than one ). How can i solve the problem? Thanks you all

4
  • What is the error you are getting? Commented Jul 25, 2018 at 7:30
  • Missing comma between VALUES. Commented Jul 25, 2018 at 7:32
  • @KMS Basically i can't insert multiple data, because they result concatenating. I receive error in : $result = pg_query($conn, $query2 ); //if you are using pg_query and $conn is the connection resource Commented Jul 25, 2018 at 7:33
  • @ŁukaszKamiński Exactly I thought this was the problem, but I find it difficult to understand how to solve this problem. Commented Jul 25, 2018 at 7:34

1 Answer 1

1

You are missing comma in between VALUES.

This is what you are executing: INSERT INTO table VALUES (stuff) (stuff2) (stuff3)

Correct is: INSERT INTO table VALUES (stuff), (stuff2), (stuff3)

Fix:

    $iemail = empty( $_POST[$key]['iemail'] ) ? NULL : $_POST[$key]['iemail'];
    if($query != "") {
      $query .= ",";
    }
    $query .= " ( '$iname', '$isurname', '$iemail' ) ";
Sign up to request clarification or add additional context in comments.

Comments

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.