【问题标题】:Insert data from a dynamic form into database将动态表单中的数据插入数据库
【发布时间】:2018-09-12 09:11:39
【问题描述】:

我正在学习使用 PHP 编程。 我有一个带有链接到 connection.php 页面的动态表单的索引页面 html,在该页面中我已经完成了与数据库的连接,并且我在其中创建了一个查询以将数据插入到数据库中(我的处理方式是否正确?) .

我想给你看代码:

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>

进程.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: 名称 认知 日期 电子邮件 节 奥吉托

个人: 名称_p 认知_p 电子邮件_p 理论学 email_richiedente oggetto_richiedente

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

非常感谢您的帮助!

更新。当我点击确认按钮时,这是我的页面。 Picture

【问题讨论】:

    标签: php html database


    【解决方案1】:

    代码对我来说似乎很合适,除了一件事。插入 SQL 查询。 PHP 中的表单值可以通过 $_GET[] 或 $_POST[] 数组和 'name' 属性作为键来访问。由于您在表单中使用 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);
    }
    

    【讨论】:

    • 在对数据库运行之前不要忘记首先验证用户的每个值,因为不这样做会导致安全问题。
    • 哦,是的。也不要忘记清理表单数据。
    • 感谢您的帮助!实际上我意识到它并没有像我写的那样工作(实际上它什么也没做,只是插入我写的东西,而不是实际的数据)。我修改了你告诉我的代码,只是这次它没有插入任何东西(准确地说是在 Prenotazione 表中给了我错误,而是在 Personale 表中输入数据,但全部为空,为空)
    • 尝试在 PHPMyAdmin 或任何数据库中运行 SQL 查询并寻找错误。可能是列或表的拼写错误造成的。
    • @GioRodriguez 你真是太好了帮助我!我尝试像这样直接在 phpmyadmin 上运行 SQL 查询: $query = "INSERT into Prenotazione (nome,cognome,data,email,stanza,oggetto) values('$nome','$cognome','$data',' $email','$stanza','$oggetto')";并输入这些值 $nome, $cognome 等 ..into table 但是当我尝试在我的网页上填写表格并单击“确认”按钮时,我被发送回空白页面,没有任何内容被添加到数据库
    【解决方案2】:

    实际上你必须阅读Retrieve post array values 这是因为你的 index.html 中有一些输入标签没有“正确”命名。这可能会导致混乱。输入标签中的“arr[]”之类的名称大部分时间用于在帖子中组织数组,并允许在 php 端访问它们,例如 $_POST['arr']。我不确定 $_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 ) ) 
    

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多