使用PHP的批量SQL行条目


Bulk SQL Row Entry with PHP

我正试图为有队长特权的人批量进入我为工作开发的午餐散步网站。

船长将检查查询中是否包含用户,输入该用户的里程,然后提交查询。服务器将这些值推送到一个数组中,并使用for循环遍历表单并将数据插入sql数据库。

当您检查所有成员,或者只检查列表中的第一个成员时,下面的代码就会起作用。

但是,如果不选中第一个成员,它将根本不会继续运行查询,甚至在查询失败时也不会采取行动。在我看来,错误发生在它尝试运行插入查询之前。

有人能告诉我我做错了什么吗?此外,欢迎其他关于一般编码礼仪的注释。我是自学成才的,所以我不是最好的,我总是欢迎一些技巧和窍门来提高我的技能。

感谢

   <?php
                    if($_SERVER['REQUEST_METHOD'] != 'POST')
                        {
                        $SQL_Get_Team = "SELECT * FROM User_Account WHERE User_Account_Team_Key = '".$_SESSION['Team_ID']."'";
                        $Result_Get_Team = mysql_query($SQL_Get_Team);
                        echo'<form class="form-signin" role="form" data-toggle="validator" method="post" action="">';
                        echo'<table class="table table-striped" width="100%">
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Miles</th>
                                <th>Include</th>
                            </tr>';
                        while($Team = mysql_fetch_assoc($Result_Get_Team))
                            {
                            echo '<tr>
                                    <td><input type="hidden" name="key[]" value="'.$Team['User_Account_Key'].'">'.$Team['User_Account_Key'].'</td>
                                    <td>'.$Team['User_Account_Name'].'</td>
                                    <td><input name="Miles[]" type="text" class="form-control" placeholder="Miles" autofocus></td>
                                    <td style=text-align:"center;"><input type="checkbox" name="include[]" value="Yes"></td>
                                  </tr>';
                            }
                        echo'   </table>
                                <br>
                                <br>
                                <button class="btn btn-lg btn-primary btn-block" type="submit">Add Miles</button>
                            </form>';
                        }
                    else
                        {
                            $key = $_POST["key"];
                            $Miles = $_POST["Miles"];
                            $include = $_POST["include"];
                            $length = count($key);
                            for($x=0;$x<$length;$x++)
                                {
                                if($include[$x] != 'Yes')
                                    {
                                    }
                                else
                                    {
                                    if($Miles[$x] == 0)
                                        {
                                        }
                                    else
                                        {
                                        echo 'User: '.$key[$x] . ' Miles: '.$Miles[$x]. '<br/>';
                                        $SQL_Add_Miles = 'INSERT INTO Miles(Miles_User_Key, Miles_Team_Key, Miles_Amount, Miles_Date)
                                                              VALUES("' . mysql_real_escape_string($key[$x]) . '",
                                                                     "' . mysql_real_escape_string($_SESSION['Team_ID']) . '",
                                                                     "' . mysql_real_escape_string($Miles[$x]) . '",
                                                                     "'.date("Y-m-d").'"
                                                                    )';
                                        $Result_Add_Miles = mysql_query($SQL_Add_Miles);
                                        if(!$Result_Add_Miles)
                                            {
                                                echo '<div style="text-align: left;" class="page-wrap">An error occured while updating account. Please try again later.<br /><br />' . mysql_error(). '</div>';
                                            }
                                        else
                                            {
                                                echo '<div style="text-align: left;" class="page-wrap">Updated account.</div>';
                                            }
                                        }
                                    }
                                }
                        }   
                      ?>

运行查询时,将or die(mysql_error());添加到末尾,如下所示:

$Result_Add_Miles = mysql_query($SQL_Add_Miles) or die(mysql_error());

这至少会告诉你你的错误是什么。

我可以通过将英里表单设置为值0并删除include复选框来计算它。。

而不是使用包含复选框进行检查。我只是确保英里字段大于0。这感觉像是临时的创可贴。。但应该适用于我目前的目的。

我还将该字段设置为数字字段,而不是文本,并将其设置为必填项。