使用复选框值更新SQL数据库

时间:2013-09-04 20:54:36

标签: php mysql

好的我知道这已经回答了几次,但我似乎无法让我的工作!

我的数据库中有8周(设置为列),我用php显示这些值,如下所示

<form method="post" id="updating" action="<?php $_PHP_SELF ?>">

        <?php
        $count = 1;
        foreach ($results as $v) {
            while($count<9){
                $week = Week.$count;
                $checkvalue = $v -> $week;
                ?>
                Week<?echo $count;?> <input type="checkbox" id="Week<?php echo$count;?>" name="week[]" value="<?php echo $checkvalue;?>" <?php if($checkvalue==1){?>
                checked="checked"
                <?php } ?>
                />
                <?php   
                $count++; 
            }
        }
        ?>
        <input name="update" type="submit" id="update" value="Update">
    </form>

我知道它可能不像它应该的那样优雅,但它会为我输出正确的HTML并显示数据库中显示的信息。

<form method="post" id="updating" action="">

        Week1 <input type="checkbox" id="Week1" name="week[]" value="1"     checked="checked"
        />
    Week2 <input type="checkbox" id="Week2" name="week[]" value="0"     />
    Week3 <input type="checkbox" id="Week3" name="week[]" value="1"     checked="checked"
        />
    Week4 <input type="checkbox" id="Week4" name="week[]" value="1"     checked="checked"
        />
    Week5 <input type="checkbox" id="Week5" name="week[]" value="0"     />
    Week6 <input type="checkbox" id="Week6" name="week[]" value="1"     checked="checked"
        />
    Week7 <input type="checkbox" id="Week7" name="week[]" value="1"     checked="checked"
        />
    Week8 <input type="checkbox" id="Week8" name="week[]" value="1"     checked="checked"
        />
<input name="update" type="submit" id="update" value="Update">
</form>

我遇到的问题是当我勾选或取消选中这些复选框时,似乎没有捕获更新的值,因此数据库没有正确更新。这是处理自我发布请求的地方。

<?php if(isset($_POST['update']))
{
    $count = 1;
    if(isset($_POST['week']) && !empty($_POST['week']))   
    foreach($_POST['week'] as $w){ echo $w; //prints 1 all the time
$week = Week.$count;
echo $week;//prints out weeks I want to update correctly (database headings)
$query = $db->getQuery(true);

// Fields to update.
$fields = array(
    $week.'=' . $db->quote( $w ));

// Conditions for which records should be updated.
$conditions = array(
    'UserID='. (int) $user_id);

$query->update($db->quoteName('tools_engage'))->set($fields)->where($conditions);

$db->setQuery($query);

try {
    $result = $db->query(); // Use $db->execute() for Joomla 3.0.
    echo ("Values saved and updated </br>");
    $count++;
} catch (Exception $e) {
    // Catch the error.
}
}
}?>

我在Joomla中这样做,因此奇怪的数据库更新语法,但它确实有效,我已经在这个程序的其他地方使用它们所以我知道它们不是问题

正如我在代码中评论的那样,无论我检查或取消选中哪个框,数组周[]似乎都包含所有内容的值1。然后,这会导致数据库中的每个值都更新为1 / true,这显然是不对的。

我怀疑问题在于我的HTML输出更新表单,但似乎无法破解它,非常感谢任何帮助!

更新 好的,所以继续nvuono的建议我已经更新我的表格就像这样

<?php
$count = 1;
foreach ($results as $v) {
while($count<9){
    $week = Week.$count;
    $checkvalue = $v -> $week;
    ?>
    Week<?echo $count;?> <input type="checkbox" id="Week<?php echo$count;?>" name="week[<?php echo $count;?>]" value="1" <?php if($checkvalue==1){?>
    checked="checked"
    <?php } ?>
    />
<?php   
$count++; 
}
}
?>

那么现在每个名字在数组中都有一个索引吗?如果是这样的话,我应该

if(isset($_POST['update']))

部分现在不按照输入的顺序打印出数组中的所有值?

即。我的逻辑是它打印出1,0,0,1,0,0,0(选中框1和4)或者检查了哪些框。但相反它只打印出1,1因此我无法知道它们是什么样的索引,以及随后我应该更新的那一周

1 个答案:

答案 0 :(得分:1)

我添加了一些非常hacky的更新方式。使用post时,您需要注意SQL注入和其他不好的事情...确保始终验证和清理数据。 “hacky”方式有效,因为我们使用整数值,并且很容易清理。

截图:

enter image description here

这是我的表...我为了stackoverflow而调用它...

+----+-------+-------+-------+-------+-------+-------+-------+-------+
| id | week1 | week2 | week3 | week4 | week5 | week6 | week7 | week8 |
+----+-------+-------+-------+-------+-------+-------+-------+-------+
|  1 |       |       |       |       |       |       |       |       |
|  2 | 1     |       |       |       |       |       |       |       |
|  3 | 1     |       | 1     |       |       |       |       |       |
|  4 | 1     |       | 1     |       | 1     |       |       |       |
|  5 | 1     |       | 1     |       | 1     |       | 1     |       |
+----+-------+-------+-------+-------+-------+-------+-------+-------+

这是我的PHP文件....(将其粘贴在一个空的PHP文档中运行,除了更改数据库连接和查询之外,它是自包含的)

<?php 
$mysqli = new mysqli("localhost", "root", "password", "test"); 

if (!empty($_POST)) {
   print "<pre>POST VARS: \n".print_r($_POST,true)."</pre>";

   foreach($_POST as $i => $data) 
   { 
      if (substr($i,0,3)=='row' && is_numeric(substr($i,3)))
      {
         $row_id = substr($i,3);  
         $data = array_flip($data); 
         $values = array(); 

         for ($x=1; $x<9; $x++) {
            $values[] = "week$x = ". ((isset($data[$x])) ? '1' : '0');  
         }   

         $stmt = "\nupdate so ".
                 "\n  set ".implode(", \n      ",$values).
                 "\n  where id = $row_id; \n"; 

         $update = $mysqli->query($stmt); 
         if ($update) { print "Row $row_id updated successfully.<br/>"; } 
      }
      print "<br/>"; 
   }
}
$result = $mysqli->query("select * from so"); 

$mysqli->close();
?>

<form method="post" id="updating" action="<?php $_PHP_SELF ?>"> 
   <?php
   while($row = $result->fetch_object())
   {

      $count = 1;

      print "<div style='border:1px solid black; display:inline-block;'>\n";
      print "Row ".$row->id."<br/>\n"; 
      while($count < 9) 
      {
         $week = "week$count";

         $checkvalue = $row->{$week};
         ?>
         Week<?php echo $count ?> <input type="checkbox" id="Week<?php echo$count;?>" name="row<?php echo $row->id 
         ?>[]" value="<?php echo $count;?>" <?php if($checkvalue==1){
         ?>  checked="checked" <?php } ?> />&nbsp;&nbsp;&nbsp;
         <?php   
         $count++; 
      }
      print "</div><br/><br/>\n\n\n"; 
   }
   ?>
        <input name="update" type="submit" id="update" value="Update">
    </form>

这是提交后的$_POST

POST VARS: 
Array
(
    [row2] => Array
        (
            [0] => 1
        )

    [row3] => Array
        (
            [0] => 1
            [1] => 3
        )

    [row4] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 5
        )

    [row5] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 5
            [3] => 7
        )

    [update] => Update
)

表单的“查看来源”:

<form method="post" id="updating" action=""> 
   <div style='border:1px solid black; display:inline-block;'>
Row 1<br/>
         Week1 <input type="checkbox" id="Week1" name="row1[]" value="1"  />&nbsp;&nbsp;&nbsp;
                  Week2 <input type="checkbox" id="Week2" name="row1[]" value="2"  />&nbsp;&nbsp;&nbsp;
                  Week3 <input type="checkbox" id="Week3" name="row1[]" value="3"  />&nbsp;&nbsp;&nbsp;
                  Week4 <input type="checkbox" id="Week4" name="row1[]" value="4"  />&nbsp;&nbsp;&nbsp;
                  Week5 <input type="checkbox" id="Week5" name="row1[]" value="5"  />&nbsp;&nbsp;&nbsp;
                  Week6 <input type="checkbox" id="Week6" name="row1[]" value="6"  />&nbsp;&nbsp;&nbsp;
                  Week7 <input type="checkbox" id="Week7" name="row1[]" value="7"  />&nbsp;&nbsp;&nbsp;
                  Week8 <input type="checkbox" id="Week8" name="row1[]" value="8"  />&nbsp;&nbsp;&nbsp;
         </div><br/><br/>


<div style='border:1px solid black; display:inline-block;'>
Row 2<br/>
         Week1 <input type="checkbox" id="Week1" name="row2[]" value="1"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week2 <input type="checkbox" id="Week2" name="row2[]" value="2"  />&nbsp;&nbsp;&nbsp;
                  Week3 <input type="checkbox" id="Week3" name="row2[]" value="3"  />&nbsp;&nbsp;&nbsp;
                  Week4 <input type="checkbox" id="Week4" name="row2[]" value="4"  />&nbsp;&nbsp;&nbsp;
                  Week5 <input type="checkbox" id="Week5" name="row2[]" value="5"  />&nbsp;&nbsp;&nbsp;
                  Week6 <input type="checkbox" id="Week6" name="row2[]" value="6"  />&nbsp;&nbsp;&nbsp;
                  Week7 <input type="checkbox" id="Week7" name="row2[]" value="7"  />&nbsp;&nbsp;&nbsp;
                  Week8 <input type="checkbox" id="Week8" name="row2[]" value="8"  />&nbsp;&nbsp;&nbsp;
         </div><br/><br/>


<div style='border:1px solid black; display:inline-block;'>
Row 3<br/>
         Week1 <input type="checkbox" id="Week1" name="row3[]" value="1"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week2 <input type="checkbox" id="Week2" name="row3[]" value="2"  />&nbsp;&nbsp;&nbsp;
                  Week3 <input type="checkbox" id="Week3" name="row3[]" value="3"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week4 <input type="checkbox" id="Week4" name="row3[]" value="4"  />&nbsp;&nbsp;&nbsp;
                  Week5 <input type="checkbox" id="Week5" name="row3[]" value="5"  />&nbsp;&nbsp;&nbsp;
                  Week6 <input type="checkbox" id="Week6" name="row3[]" value="6"  />&nbsp;&nbsp;&nbsp;
                  Week7 <input type="checkbox" id="Week7" name="row3[]" value="7"  />&nbsp;&nbsp;&nbsp;
                  Week8 <input type="checkbox" id="Week8" name="row3[]" value="8"  />&nbsp;&nbsp;&nbsp;
         </div><br/><br/>


<div style='border:1px solid black; display:inline-block;'>
Row 4<br/>
         Week1 <input type="checkbox" id="Week1" name="row4[]" value="1"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week2 <input type="checkbox" id="Week2" name="row4[]" value="2"  />&nbsp;&nbsp;&nbsp;
                  Week3 <input type="checkbox" id="Week3" name="row4[]" value="3"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week4 <input type="checkbox" id="Week4" name="row4[]" value="4"  />&nbsp;&nbsp;&nbsp;
                  Week5 <input type="checkbox" id="Week5" name="row4[]" value="5"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week6 <input type="checkbox" id="Week6" name="row4[]" value="6"  />&nbsp;&nbsp;&nbsp;
                  Week7 <input type="checkbox" id="Week7" name="row4[]" value="7"  />&nbsp;&nbsp;&nbsp;
                  Week8 <input type="checkbox" id="Week8" name="row4[]" value="8"  />&nbsp;&nbsp;&nbsp;
         </div><br/><br/>


<div style='border:1px solid black; display:inline-block;'>
Row 5<br/>
         Week1 <input type="checkbox" id="Week1" name="row5[]" value="1"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week2 <input type="checkbox" id="Week2" name="row5[]" value="2"  />&nbsp;&nbsp;&nbsp;
                  Week3 <input type="checkbox" id="Week3" name="row5[]" value="3"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week4 <input type="checkbox" id="Week4" name="row5[]" value="4"  />&nbsp;&nbsp;&nbsp;
                  Week5 <input type="checkbox" id="Week5" name="row5[]" value="5"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week6 <input type="checkbox" id="Week6" name="row5[]" value="6"  />&nbsp;&nbsp;&nbsp;
                  Week7 <input type="checkbox" id="Week7" name="row5[]" value="7"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week8 <input type="checkbox" id="Week8" name="row5[]" value="8"  />&nbsp;&nbsp;&nbsp;
         </div><br/><br/>


        <input name="update" type="submit" id="update" value="Update">
    </form>

修改
我回到这个回答引用的东西,我刚刚意识到我正在重复使用ID值作为复选框。在这种情况下这不是问题,但总是不好的做法。每个复选框都应具有页面上唯一的ID。所以....它应该是id="row1week1"id="row1week2" ...... id="row8week1" ... id="row8week8"