用PHP动态插入数据库

时间:2018-09-12 21:34:20

标签: php mysql

我正在学习用php编程,遇到一个我无法解决的问题。 基本上,我必须将一个具有动态部分的表单插入数据库(从某种意义上说,用户可以决定要插入多少个字段)。 此表单将填充两个表:Prenotazione,Personale。 对于第一个表,它已正确填充。第二个不是,即仅插入名称,其余部分不插入。我如何按照您的要求进行?我哪里错了?非常感谢您的帮助和时间。

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">Aggiungi partecipante</a></button>


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

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[$key]['cognomep'] ) ? NULL : $_POST[$key]['cognomep'];
        $emailp = empty( $_POST[$key]['emailp'] ) ? NULL : $_POST[$key]['emailp'];
        $tipologia = empty( $_POST[$key]['tipologia'] ) ? NULL : $_POST[$key]['tipologia'];
        $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 ); //
}

?>

utils.js

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

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

Image

0 个答案:

没有答案