动态插入数据库

时间:2018-07-25 07:18:02

标签: php database postgresql

我正在创建一个简单的Web应用程序,该表单可用于在DB(Postgres)中插入数据。

我有这张桌子:

  

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)

现在关于表“ interni”和“ esterni”,用户应该选择要插入多少数据,因此我实现了一种动态形式:

** 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 );

问题是,当他们尝试将数据插入“ interni”并且数据不只一个时,他们无法这样做,并且我收到错误消息。 (当他们尝试插入多个表时,基本上“ interni”表为空)。 我该如何解决这个问题? 谢谢大家

1 个答案:

答案 0 :(得分:1)

您在VALUES之间缺少逗号。

这是您要执行的操作:parallel one: { stage ('Starting Test') { build (job: 'myJob1', parameters: [booleanParam (name: 'startServer', value: false)], quietPeriod: 60, wait: false) } }, two: { stage ('Starting Test2') { build (job: 'myJob2', parameters: [booleanParam (name: 'startServer', value: false)], quietPeriod: 60, wait: false) } }

正确的是:INSERT INTO table VALUES (stuff) (stuff2) (stuff3)

修复:

INSERT INTO table VALUES (stuff), (stuff2), (stuff3)