2017-04-18 39 views
1

尝试插入来自在PHP中动态生成的表单的信息时遇到问题。 表格由可变数量的输入组成,全部由四个输入元素组成。请参见下面是如何产生自己的状态:从多个选中的复选框和输入到mySQL中的文本输入插入PHP数组

<?php $result = mysql_query("SELECT id,name,description FROM todo_q WHERE todo_id = $todo_id AND active = 'y'"); 
       while($todo_q=mysql_fetch_array($result)){ 

               echo '<label>'; 
               echo $todo_q['name']; 
               echo '</label><br>'; 

               echo '<input type="checkbox" name="value[]" value="y" />'; 
               //echo '<input type="hidden" name="value[]" value="n" />'; 

               echo '<label>'; 
               echo $todo_q['description']; 
               echo '</label><br>'; 
               echo '<input type="text" id="comment" name="comment[]">'; 

               echo '<input type="hidden" name="user_id[]" value="'; 
               echo $user_id; 
               echo '" />'; 
               echo '<input type="hidden" name="todo_id[]" value="'; 
               echo $todo_q['id']; 
               echo '" />'; 
               echo '<HR>'; 

               }?> 

这是我尝试插入信息到MySQL:

$query = "INSERT INTO todo_a (value, comment, user_id, todo_id) VALUES "; 
$query_parts = array(); 
for($x=0; $x<count($_POST["value"]); $x++){ 
    $query_parts[] = "('" . $_POST['value'][$x] . "','" . $_POST['comment'][$x] . "'," . $_POST['user_id'][$x] . "," . $_POST['todo_id'][$x] . ")"; 


    } 
    $q_parts = $query_parts; 

     foreach ($q_parts as $q_p){ 

      $insert = ($query .= implode(',', $query_parts)); 
      $result = mysql_query($insert); 
     } 

我的问题是,当检查所有的复选框和评论的一切插入在数据库的右侧,但如果我跳过检查一个复选框,然后它会搞砸了......

我想插入一个新的行,如果它的复选框被选中和/或输入评论。 任何人都可以指向正确的方向吗?

我试图把一个隐藏的输入来获取未经检查的复选框的值,但我似乎并没有工作..这就是为什么我已经注释掉隐藏的复选框。

PS。我知道我应该使用mysqli的,但这是一个较旧的网站,我没有升级尚未..

+2

**强制警告:请请不要在生产中使用此代码.. **。消毒您的查询,以防止一些XSS ..还有你检查了这个相关的问题? http://stackoverflow.com/questions/5640298/php-insert-data-from-checkbox-array-into-mysql?rq=1 – Pogrindis

+2

仅供参考,[你不应该在新代码中使用'mysql_ *'函数]( http://stackoverflow.com/questions/12859942/)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[红盒](http://php.net/manual/en/function.mysql-connect.php)?学习[*准备的语句*](https://en.wikipedia.org/wiki/Prepared_statement),并使用[PDO](http://php.net/pdo)或[MySQLi](http:// php.net/mysqli) - [这篇文章](http://php.net/manual/en/mysqlinfo.api.choosing.php)将帮助你决定哪一个最适合你。 –

+1

你的脚本存在[SQL注入攻击]的风险(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)看看发生了什么事[Little鲍比表](http://bobby-tables.com/)即使[如果你逃避投入,它不安全!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around -mysql-real-escape-string)使用[prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。 –

回答

1

您需要添加索引输入复选框,并发表评论名称,如:

$cbIndex = 0; 
while($todo_q=mysql_fetch_array($result)){ 
    echo '<label>'; 
    echo $todo_q['name']; 
    echo '</label><br>'; 
    // Generate checkbox with index of current result 
    echo '<input type="checkbox" name="value[' . $cbIndex . ']" value="y" />'; 
    // Generate comment with index of current result 
    echo '<input type="text" id="comment" name="comment[' . $cbIndex . ']">'; 
    echo '<input type="hidden" name="user_id[' . $cbIndex . ']" value="'; 
    echo $user_id; 
    echo '" />'; 
    echo '<input type="hidden" name="todo_id[' . $cbIndex . ']" value="'; 
    echo $todo_q['id']; 
    echo '" />'; 
    echo '<HR>'; 
    // Inc of index 
    $cbIndex++; 
} 

当您提交您的形式,只有选中的复选框会出现在$ _ POST [“值”]:

foreach ($_POST["value"] as $cbIndex => $cbValue) { 
    $query_parts[] = "('" . $_POST['value'][$cbIndex] . "','" . $_POST['comment'][$cbIndex] . "'," . $_POST['user_id'][$cbIndex] . "," . $_POST['todo_id'][$cbIndex] . ")"; 
    // or 
    $query_parts[] = "('" . $cbValue . "','" . $_POST['comment'][$cbIndex] . "'," . $_POST['user_id'][$cbIndex] . "," . $_POST['todo_id'][$cbIndex] . ")"; 
} 
... 

顺便说一句,你不需要复选框的储值,那将是“Y”的所有时间。

信息这将是罚款,一个测试应用程序,而是由@Pogrindis和@约翰孔德的评论,这不是安全的代码。 MySQLi/PDO +准备语句将避免SQL注入。

+1

谢谢@camille您的建议。它似乎工作。我不会在“实时”应用程序中使用它。现在这只是一个测试。 – user2533103

+0

完美!很高兴帮助你:-D – Camille

相关问题