将动态表单中的数据插入数据库

时间:2018-09-12 09:11:39

标签: php html database

我正在学习用Php编程。 我有一个索引页面html,它具有链接到connection.php页面的动态形式,在该页面中我已经完成了与数据库的连接,并且在其中我创建了一个查询以将数据插入数据库中(这是我进行的正确方式吗?)

我想给你看代码:

index.html

<body>    
        <form action="connection.php" class="register" method="POST">
            <h1>Conferenza</h1>
            <fieldset class="row1">
                <legend>Informazioni Richiedente Prenotazione</legend>
                <p>
                    <label>Nome
                    </label>
                    <input name="nome" type="text" required="required"/>
                    <label>Cognome
                    </label>
                    <input name="cognome" type="text" required="required"/>
                    <label>Data Videoconferenza 
                    </label>
                    <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>    
                    <br>            
                    <label>Email 
                    </label>
                   <input name="email" type="text" required="required"/>
                   <label>Stanza
                    </label>
                    <select name="stanza">
                        <option value="1">Stanza 1
                        </option>
                        <option value="2">Stanza 2
                        </option>
                    </select>
                </p>
                <p>
                    <label>Oggetto Prenotazione
                    </label>
                    <textarea rows="4" cols="50" name="oggetto" form="usrform" placeholder="Inserire Oggetto Conferenza"></textarea>
                    <br>

                </p>

                <div class="clear"></div>
            </fieldset>
            <fieldset class="row2">
                <legend>Personale </legend>
                <p> 
                    <input type="button" value="Add Passenger" onClick="addRow('dataTable')" /> 
                    <input type="button" value="Remove Passenger" onClick="deleteRow('dataTable')"  /> 
                    <p>(All acions apply only to entries with check marked check boxes only.)</p>
                </p>
               <table id="dataTable" class="form" border="1">
                  <tbody>
                    <tr>
                      <p>
                        <td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
                        <td>
                            <label>Nome</label>
                            <input type="text" required="required" name="nome_p[]">
                         </td>
                         <td>
                            <label for="Cognome">Cognome</label>
                            <input type="text" required="required" name="cognome_p[]">
                         </td>
                         <td>
                            <label for="Email">Email</label>
                            <input type="text" required="required" name="email_p[]">
                         </td>
                         <td>
                            <label for="Tipologia">Tipologia</label>
                            <select id="BX_birth" name="tipologia[]" required="required">
                                <option>Interno</option>
                                <option>esterno</option>

                            </select>
                         </td>
                            </p>
                    </tr>
                    </tbody>
                </table>
                <div class="clear"></div>
            </fieldset>

            <input class="submit" type="submit" value="Confirm &raquo;" />


            <div class="clear"></div>
        </form>

    </body>

process.php

<body>    
        <form action="connection.php" class="register">
            <h1>YouAreBUS Ticket Reservation</h1>
            <?php if(isset($_POST)==true && empty($_POST)==false): 
                $chkbox = $_POST['chk'];
                $nome = $_POST['nome'];
                $day = $_POST['day'];
                $month = $_POST['month'];
                $mob = $_POST['mob'];
                $type = $_POST['type'];
                $oggetto = $_POST['oggetto'];
                $to=$_POST['to'];
                $stanza=$_POST['stanza'];
                $nome_int=$_POST['nome_int'];
                $cognome_int=$_POST['cognome_int'];         
                $email_int=$_POST['email_int'];
                $tipologia=$_POST['tipologia'];                 
            ?>
            <fieldset class="row1">
                <legend>Informazioni Richiedente Prenotazione</legend>
                <p>
                    <label>Nome 
                    </label>
                    <input name="nome" type="text" readonly="readonly" value="<?php echo $nome ?>"/>
                    <label>Data Videoconferenza
                    </label>
                    <input type="text" readonly="readonly" class="small" value="<?php echo $day ?>"/>
                    <input type="text" readonly="readonly" class="small" value="<?php echo $month ?>"/>
                    <input type="text" readonly="readonly" class="small" value="2013"/>
                    <label>Email 
                    </label>
                    <input  type="text" readonly="readonly" value="<?php echo $email ?>"/>

                </p>
                <p>
                    <label>Oggetto Prenotazione
                    </label>
                    <input name="oggetto" type="text" readonly="readonly" value="<?php echo $oggetto ?>"/>

                    <label>Stanza
                    </label>
                    <input  type="text" readonly="readonly" value="<?php echo $stanza ?>"/>

                </p>


                <div class="clear"></div>
            </fieldset>
            <fieldset class="row2">
                <legend>Personale
                </legend>               
                <table id="dataTable" class="form" border="1">
                    <tbody>
                    <?php foreach($nome_p as $a => $b){ ?>
                        <tr>
                            <p>
                                <td >
                                    <?php echo $a+1; ?>
                                </td>
                                <td>
                                    <label>Nome</label>
                                    <input type="text" readonly="readonly" name="nome_p[$a]" value="<?php echo $nome_p[$a]; ?>">
                                </td>
                                <td>
                                    <label for="Cognome"></label>
                                    <input type="text" readonly="readonly" class="small"  name="cognome_p[]" value="<?php echo $cognome_p[$a]; ?>">
                                </td>
                                <td>
                                    <label for="Email">Email</label>
                                    <input type="text" readonly="readonly" name="email_p[]" value="<?php echo $BX_gender[$a]; ?>">
                                </td>
                                <td>
                                    <label for="BX_birth">Berth Pre</label>
                                    <input type="text" readonly="readonly" name="tipologia[]" value="<?php echo $tipologia[$a]; ?>">
                                </td>
                            </p>
                        </tr>
                    <?php } ?>
                    </tbody>
                </table>
                <div class="clear"></div>
            </fieldset>

          <fieldset class="row5">
                <legend>Terms and Mailing</legend>
                <p>
                    <input class="submit" type="button" value="Make Payment &raquo;" />
                    <a class="submit" href="index.html" type="button"> Back To Demo <a/>
                </p>
                <div class="clear"></div>
            </fieldset>
        <?php else: ?>
        <fieldset class="row1">
            <legend>Sorry</legend>
             <p>Some things went wrong please try again.</p>
        </fieldset>
        <?php endif; ?>
            <div class="clear"></div>
        </form>
    </body>

还有

connection.php

<?php
$servername = "localhost";
$username = "root";
$password = "123456789";
$dbname = "Project";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['submit'])) {

    $nome = $_POST['nome'];
    $cognome = $_POST['cognome'];
    $day = $_POST['day'];
    $month = $_POST['month'];
    $email = $_POST['email'];
    $stanza = $_POST['stanza'];
    $oggetto = $_POST['oggetto'];
    $nome_p = $_POST['nome_p[]'];
    $cognome_p = $_POST['cognome_p[]'];
    $email_p = $_POST['email_p[]'];
    $tipologia = $_POST['tipologia[]'];


$query = "INSERT into Prenotazione (nome,cognome,day,month,email,stanza,oggetto) values('$nome','$cognome','$day','$month','$email','$stanza','$oggetto')";
$query1 = "INSERT into Personale (nome_p,cognome_p,email_p,tipologia,email,oggetto) values('$nome_p','$cognome_p','$email_p','$tipologia','$email','$oggetto')";

}
if (mysqli_query($conn, $query)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
if (mysqli_query($conn, $query1)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $query1 . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

和scripts.js

function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    if(rowCount < 5){                           // limit the user from creating fields more than your limits
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        for(var i=0; i<colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
        }
    }else{
         alert("Maximum Passenger per ticket is 5.");

    }
}

function deleteRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    for(var i=0; i<rowCount; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 1) {                         // limit the user from removing all the fields
                alert("Cannot Remove all the Passenger.");
                break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
        }
    }
}

我的问题是关于插入数据库,因为我写的查询不正确。 我的数据库有2个表:

Prenotazione:     诺姆     认知度     日期     电子邮件     节     oggetto

个人服务:     nome_p     cognome_p     email_p     olo     email_richiedente     oggetto_richiedente

您认为谁可以编写查询以从动态表单中插入数据?

非常感谢您的帮助!

更新。单击“确认”按钮时,这是我的页面。 Picture

2 个答案:

答案 0 :(得分:0)

代码对我来说似乎很正确,除了一件事。插入SQL查询。可以通过$ _GET []或$ _POST []数组和'name'属性作为键来访问PHP中的表单值。由于您在表单中使用POST方法,因此请使用$ _POST []。我在这里给你一个例子,让你有个主意:

HTML:

<form action="insert.php" method="POST">
    <input type="text" name="name"/><label>Name</label>
    <input type="text" name="age"/><label>Age</label>
    <input type="text name="submit" value="Submit"/>
 </form>

PHP:

/*Now here is the part where you should change your code based on this example
  As you notice we used $_POST['submit'], $_POST['name'] and $_POST['age'] because as I have said we get their values by their name attribute. 
  PHP works this way.
*/

/* Use isset to check if the form datas are there. Often without the isset, you'll get an undefined index error. */
if(isset($_POST['submit'])) {
    /* Declare POST form datas into variables just for a better code format */
    $name = $_POST['name'];
    $age = $_POST['age'];

    $query = "INSERT INTO users(name, age) VALUES($name, $age)";
    $query_run = mysqli_query($query);
}

答案 1 :(得分:0)

实际上,您必须阅读Retrieve post array values,这是因为在index.html中有一些未“正确”命名的输入标签。这可能导致混乱。输入标记中的诸如“ arr []”之类的名称大部分时间用于组织帖子中的数组,并允许它们像$ _POST ['arr']这样在php端进行访问。我不确定$ _POST ['arr []']会返回给您什么,但似乎没有什么用。在您发送的示例中,似乎您需要遍历每个$ _POST子数组以构建用于插入的sql。例如:

<html>
  <body>
   <?php
    print_r($_POST);

   ?>
    <form name="form1" method="POST">
    <input type="text" name="name1" />
    <input type="text" name="name2" />
    <input type="submit" />
    </form>
    <form name="form2" method="POST">
    <input type="text" name="name[1]" />
    <input type="text" name="name[2]" />
    <input type="submit" />
</form>
<form name="form3" method="POST">
    <input type="text" name="name[]" />
    <input type="text" name="name[]" />
    <input type="submit" />
</form>
</body>
</html>

如果使用a和b作为表单输入值,将导致以下输出

Array ( [name1] => a [name2] => b )

第一种形式,

Array ( [name] => Array ( [1] => a [2] => b ) )

第二种形式,

Array ( [name] => Array ( [0] => a [1] => b ) ) 

对于第三种形式(“名称”数组会自动索引)

希望有帮助