如果其值以DB为单位,请选中复选框。PHP

如果其值以DB为单位,请选中复选框。PHP,php,checkbox,Php,Checkbox,我有一个MySql表格练习中的复选框循环,如下所示: <form id="form1" name="form1" method="POST" action="insertDrills.php"> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <?php do { ?> <tr> <td> <input t

我有一个MySql表格练习中的复选框循环,如下所示:

<form id="form1" name="form1" method="POST" action="insertDrills.php">

  <table width="100%" border="1" cellspacing="0" cellpadding="0">
    <?php do { ?>
      <tr>
        <td>


<input type="checkbox" name="drillSelect[]" value="<?php echo $row_Recordset1['drillID']; ?>" id="drillSelect" />

        <?php echo $row_Recordset1['rubrik']; ?>


        </td>
      </tr>
      <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
    <tr>
      <td colspan="2">



      <input name="spelarID" type="hidden" id="spelarID" value="<?php echo $row_rsSpelare['id']; ?>" />
      <input name="date" type="hidden" id="date" value="<? print date("Y-m-d h:i:s"); ?>" />
  <input type="submit" name="submit" id="submit" value="Välj övningar" />
  <input type="button" value="Avbryt" onclick="window.close();"></button>



      </td>
    </tr>
  </table>
</form>

好的,您需要从表中选择项,将它们存储到一个数组中,如果当前复选框的ID与数组中的某个内容匹配,请在HTML中将checked属性添加到它。

SQL injection。。。到处都是SQL注入!除此之外:。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,.Ok ThiefMaster,我计划在脚本按预期工作后处理SQL注入。这只是一个用mysql编写的综合性网站,所以我认为它可能很容易像其他网站一样继续。通常我会这样做:
<?php
$id = $_POST['spelarID']; 
$drills = $_POST['drillSelect'];
$date = $_POST['date']; 

$inserts = array(); 
foreach ($drills as $drills)
    $inserts[] = "('$id','$drills','','$date')"; 


$query = "REPLACE INTO exercise_spelare_ref VALUES ". implode(", ", $inserts);

//echo "query = $query"; // for debugging purposes, remove this once it is working 
mysql_query($query) or die(mysql_error()); 
?>
$uid = $row_rsSpelare['id']; // your logged in user's ID

$exercise = array();

// get an array of exercise
$sql = "SELECT drillID FROM exercise";
$res = mysql_query($sql);
while ($row = mysql_fetch_object($res)) {
    $exercise[$row->id] = $row->drillID;
}

// get an array of exercise user has checked
$sql = "SELECT DISTINCT drillID FROM exercise_spelare_ref WHERE id = '$uid'";
$res = mysql_query($sql);
while ($row = mysql_fetch_object($res)) {
    $checked[] = $row->drillID;
}

// this would be templated in a real world situation
foreach ($exercise as $id => $drillID) {
    $checked = "";
    // check box if user has selected this exercise
    if (in_array($checked, $id)) {
        $checked = 'checked="checked" ';
    }
    echo '<input type="checkbox" name="drillSelect[]" value="'.$id.'" '.$checked.'/>';
}
 if (in_array($checked, $id)) {