将动态表单插入数据库

时间:2018-09-13 07:45:23

标签: php mysql database

早上好!我正在学习用PHP编程,并且在数据库中以表格形式插入数据时遇到问题。 特别是,我可以输入所有数据,除非按钮使用户能够选择要插入的字段数。在这种情况下,它不会插入任何内容。

input.php

<?php
//Connecting to db here

$servername = "localhost";
$username = "root";
$password = "123456789";
$dbname = "Project1";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

// Richiedente
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$email = $_POST['email'];
$oggetto = $_POST['oggetto'];
$day = $_POST['day'];
$month = $_POST['month'];

// Personale
$nomep = $_POST['nomep'];
$cognomep = $_POST['cognomep'];
$emailp = $_POST['emailp'];
$tipologia = $_POST['tipologia'];

//inserting data order
$query1 = "INSERT INTO Prenotazione (nome,cognome,email,oggetto,day,month) VALUES ('$nome','$cognome','$email','$oggetto', $day, $month)";
//execute the query here
$result = mysqli_query($conn, $query1 ); //if you are using pg_query and $conn is the connection resource
// Interni
$query = "";
if( !empty( $_POST['nomep'] ) ) {

    foreach( $_POST['nomep'] as $key => $nomep ) {

        $cognomep = empty( $_POST['cognomep'][$key] ) ? NULL : $_POST['cognomep'][$key];
        $emailp = empty( $_POST['emailp'][$key] ) ? NULL : $_POST['emailp'][$key];
        $tipologia = empty( $_POST['tipologia'][$key] ) ? NULL : $_POST['tipologia'][$key];
        $query .= " ( '$nomep', '$cognomep', '$emailp','$tipologia', '$email', '$oggetto' ) ";
    }
}
if( !empty( $query ) ) {

    $query2 = "INSERT INTO Personale (nomep, cognomep, emailp, tipologia, email_rich,oggetto_rich) VALUES ".$query;
    $result = mysqli_query($conn, $query2 ); //
}

?>

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="nome" size="20"><br>
Cognome:<input type="text" name="cognome" size="20"><br>
Email: <input type="email" name="email" size="20"><br>
Oggetto Conferenza:<br><textarea name="oggetto" rows="5" cols="40" placeholder="Specificare oggetto Videoconferenza"></textarea><br>
<select class="date" name="day">
                        <option value="1">01
                        </option>
                        <option value="2">02
                        </option>
                        <option value="3">03
                        </option>
                        <option value="4">04
                        </option>
                        <option value="5">05
                        </option>
                        <option value="6">06
                        </option>
                        <option value="7">07
                        </option>
                        <option value="8">08
                        </option>
                        <option value="9">09
                        </option>
                        <option value="10">10
                        </option>
                        <option value="11">11
                        </option>
                        <option value="12">12
                        </option>
                        <option value="13">13
                        </option>
                        <option value="14">14
                        </option>
                        <option value="15">15
                        </option>
                        <option value="16">16
                        </option>
                        <option value="17">17
                        </option>
                        <option value="18">18
                        </option>
                        <option value="19">19
                        </option>
                        <option value="20">20
                        </option>
                        <option value="21">21
                        </option>
                        <option value="22">22
                        </option>
                        <option value="23">23
                        </option>
                        <option value="24">24
                        </option>
                        <option value="25">25
                        </option>
                        <option value="26">26
                        </option>
                        <option value="27">27
                        </option>
                        <option value="28">28
                        </option>
                        <option value="29">29
                        </option>
                        <option value="30">30
                        </option>
                        <option value="31">31
                        </option>
                    </select>
                    <select name="month">
                        <option value="1">January
                        </option>
                        <option value="2">February
                        </option>
                        <option value="3">March
                        </option>
                        <option value="4">April
                        </option>
                        <option value="5">May
                        </option>
                        <option value="6">June
                        </option>
                        <option value="7">July
                        </option>
                        <option value="8">August
                        </option>
                        <option value="9">September
                        </option>
                        <option value="10">October
                        </option>
                        <option value="11">November
                        </option>
                        <option value="12">December
                        </option>
                    </select>   
<br>
<b>Partecipanti </b>
<br>
<br>
<div id="start">
    <div id="first">
      Nome:<input type="text" name="nomep[]" size="20"><br> 
      Cognome: <input type="text" name="cognomep[]" size="20"><br> 
      Email: <input type="email" name="emailp[]" size="20"><br>
      Personale:
       <select name="tipologia[]" required="required">
                <option>Interno</option>
                <option>Esterno</option>
       </select>
      <br>
      <br>
    </div>
  </div>
  <br>
 Numero partecipanti interni:
 <input type="text" id="n1" size="1" value="1"><br>

 <button><a href="#" id="add">Add User</a></button>


<input type="submit" value="Invia" > 
</form>
</body>
</html>

使用js脚本

$(document).ready(function() {
    $("#add").click(function(){

      var val1 =$("#n1").val();
      for(var i=0;i<val1;i++){
      $("#start").append($("#first").clone());
      }
    });
});

当我单击“添加用户”时,我复制了有关nomep,cognomep,emailp,tipologia的表格部分。如果单击"Add User",则无法将数据插入数据库;而如果仅保留一个字段(nomep,cognomep,emailp,tipologia),则插入成功。 您认为我该如何解决?

非常感谢您的帮助和时间。

1 个答案:

答案 0 :(得分:0)

在循环中,您正在做:

foreach( $_POST['nomep'] as $key => $nomep ) {

    $cognomep = empty( $_POST['cognomep'][$key] ) ? NULL : $_POST['cognomep'][$key];
    $emailp = empty( $_POST['emailp'][$key] ) ? NULL : $_POST['emailp'][$key];
    $tipologia = empty( $_POST['tipologia'][$key] ) ? NULL : $_POST['tipologia'][$key];
    $query .= " ( '$nomep', '$cognomep', '$emailp','$tipologia', '$email', '$oggetto' ) ";
}

此$ query变量不断追加(“此处的查询”),因此最后查询将如下所示:

( 'name', 'lastname', 'email@email.com','something', 'other email', 'something' )
( 'name2', 'lastname2', 'email2@email.com','something2', 'other email2', 'something2' )

问题在于链接,您将需要在值数组之间使用逗号。像这样:

INSERT INTO MyTable ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )

请注意值集之间的逗号。

已编辑

您的问题的可能解决方案可能是更新ur foreach,如下所示

foreach( $_POST['nomep'] as $key => $nomep ) {

    $cognomep = empty( $_POST['cognomep'][$key] ) ? NULL : $_POST['cognomep'][$key];
    $emailp = empty( $_POST['emailp'][$key] ) ? NULL : $_POST['emailp'][$key];
    $tipologia = empty( $_POST['tipologia'][$key] ) ? NULL : $_POST['tipologia'][$key];
    $query .= " ( '$nomep', '$cognomep', '$emailp','$tipologia', '$email', '$oggetto' ) ".(end($$_POST['nomep']) ? '' : ',');
}

请注意,在foreach循环的最后有一点额外的功能。