简体   繁体   English

从数据库中选择值,然后选中该复选框是否存在

[英]Selecting values from the database and make the checkbox checked if it does exist

//query to check if part id number exists in table ATTEND where service id = ...
$result2 = mysql_query("SELECT * FROM attend WHERE SIDno='$SIDno' and ServiceID='$id");
//if exists $ok = true;
  if (mysql_num_rows($result2)>0) {
        $ok == true;
  }
  echo "<tr bgcolor=$bgcolor>";
  echo "<td><a name=$row1[0] id=$row1[0]>$row1[0]</td>";
  echo "<td>" . $row1[1] . "</td>";
  echo "<td>" . $row1[5] . "</td>";
  echo "<td>" . $row1[2] . "</td>";
  echo "<td>" . $row1[3] . "</td>";
  echo "<td><input type='checkbox' name='checkbox[]' value=" . $row1[0];
  if ($ok == true) {
    echo 'disabled="disabled" checked="checked"';
  }
  echo "></td>";
  echo "<input type='hidden' name='ServiceID' value=" . $id . ">";
  echo "<input type='hidden' name='Year' value=" . $Year . ">";
  echo "<input type='hidden' name='Stype' value='Recollection'>";

  echo "</tr>";
  } 
}
echo "<tr>
<td colspan='5' align='right' bgcolor='#FFFFFF'><input name='SUBMIT1' type='submit' id='SUBMIT'value='SUBMIT'></td>
</tr>";

How can implement that on the next load if the value of the checkbox is already available in the database it will now be checked. 如果该复选框的值在数据库中已经可用,那么如何在下一次加载时实现该功能,现在将对其进行检查。 but if it is not yet existing i can check it and save it to the database. 但是,如果尚不存在,我可以检查它并将其保存到数据库中。

I'm guessing the problem lies in this line: 我猜问题出在这一行:

if (mysql_num_rows($result2)>0) {
    $ok == true;
}

It should be: 它应该是:

if (mysql_num_rows($result2)>0) {
    $ok = true;
}

In the first snippet you are just testing if $ok is equal to true , while in the second example an actual assignment to the variable is performed. 在第一个代码段中,您只是测试$ok是否等于true ,而在第二个示例中,将对变量进行实际分配。

Remember: 记得:

= != ==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM