dtwxt88240 2015-05-19 13:25
浏览 42
已采纳

使用PHP和SQL生成输入字段

I'm attempting to generate a bunch of inputs of the same name, and saving it to an sql table. The issue i'm having is, my code works for about 4 of the inputs, then the rest nothing happens.

Code thats generating the inputs:

for ($x = 0; $x < 10; $x++) {
            echo "      ID: <input name=\"IDadd[]\" type=\"text\"> 
"; 
            echo "      Name: <input name=\"Nameadd[]\" type=\"text\"> 
"; 
            echo "      Job: <select name='Jobadd[]'>
"; 
            echo "      <option value='Tech'>Tech</option>
";
            echo "      <option value='Check-In'>Check-In</option>
";
            echo "      <option value='Inventory'>Inventory</option>
"; 
            echo "      <option value='Beats'>Beats</option>
"; 
            echo "      <option value='Computers'>Computers</option>
";
            echo "      <option value='IOS'>IOS</option>
";
            echo "      <option value='Remarketing'>Remarketing</option>
";   
            echo "      </select> 
";
            echo "<br><br>";
        } 

Code that should be sorting it out and saving it:

if ( isset( $_POST[ 'newEmployee' ] ) )
                            {
                            $index = 0;
                                foreach ($_POST as $key => $value){
                                   $userId = $_POST['IDadd'][$index];
                                   $userName = $_POST['Nameadd'][$index];
                                   if (empty($_POST['Nameadd'][$index])) {
                                    return;
                                  }
                                   $userJob = $_POST['Jobadd'][$index];
                                   $index++;
                                   $sql = "INSERT INTO Employee (userId, Name, Job, Week)
                                    VALUES ('$userId', '$userName', '$userJob', '$currentWeekIs')";
                                    mysqli_query($con, $sql);
                                }
                                if($sql) // will return true if succefull else it will return false
                                    {
                                            mysqli_close( $con );
                                            sleep(4);
                                            echo '<META HTTP-EQUIV="Refresh" Content="0; URL=add.php">';
                                    }
                            }

--EDIT

So this should work then?

if ( isset( $_POST[ 'newEmployee' ] ) )
                            {

                                if (!empty($_POST['IDadd'])) {
                                    // loop through the keys of one of the arrays
                                    foreach (array_keys($_POST['IDadd']) as $index) {
                                        $userId = mysqli_real_escape_string($_POST['IDadd'][$index]);
                                        $userName = mysqli_real_escape_string($_POST['Nameadd'][$index]);
                                        $userJob = mysqli_real_escape_string($_POST['Jobadd'][$index]);
                                        $sql = "INSERT INTO Employee (userId, Name, Job, Week)
                                        VALUES ('$userId', '$userName', '$userJob', '$currentWeekIs')";
                                        mysqli_query($con, $sql);
                                    }
                                    if($sql) // will return true if succefull else it will return false
                                    {
                                            mysqli_close( $con );
                                            sleep(4);
                                            echo '<META HTTP-EQUIV="Refresh" Content="0; URL=add.php">';
                                    }
                                }
                            }
  • 写回答

1条回答 默认 最新

  • dongyi2993 2015-05-19 13:37
    关注

    Your problem is that your foreach is looping through $_POST['IDadd'], $_POST['Nameadd'] and $_POST['Jobadd'], not the contents of those arrays. Just checkout the contents of $key.

    What you need to do is make sure they aren't empty and then loop through the contents:

    // I'm just showing checking all the arrays aren't empty
    // you'll also want to check if they have the same length
    if (!empty($_POST['IDadd']) && ...) {
        // loop through the keys of one of the arrays
        foreach (array_keys($_POST['IDadd']) as $index) {
            $userId = mysqli_real_escape_string($_POST['IDadd'][$index]);
            // ...
        }
    }
    

    You should also look into using prepared statements to lessen the chance of SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退