使用复选框更新数据库列

时间:2012-06-02 04:05:24

标签: php mysql checkbox

如果选中,我无法将列更新为yes。我没有收到任何错误。我究竟做错了什么?我尝试了很多方法,包括将数组留在引号中。我知道如何通过带有电子邮件地址的表单而不是复选框来执行此操作。我也知道如何使用复选框从数据库中删除行。但不是更新......

  <?php 
      $id = $_GET['id'];
      $select = mysql_query("SELECT * FROM volsMain WHERE ID = '$id'");
      $data = mysql_fetch_array($select);
  ?>

  <?php
      $query="SELECT * FROM volsMain ORDER BY shift_times, position";
      $result = mysql_query($query) or die(mysql_error()); 
      while ($row = mysql_fetch_array($result)){    
  ?>

  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="id" value="<?php echo $id; ?>" />
    <input type="checkbox" name="checkinsat" value="yes"<?php
            if($data['checkinsat'] == 'yes') echo 'checked'; ?>/>
        <input type="submit" >
  </form>

  <?php
   if ($row['saturday'] === 'A.M.') {
       echo '<span class="amShift">';
   } else if ($row['saturday'] === 'P.M.') {
       echo '<span class="pmShift">';
   } else {
    echo '<span>';
   }
    echo "SATURDAY:" . $row['saturday'] . '</span><br />';

   if ($row['sunday'] === 'A.M.') {
    echo '<span class="amShift">';
   } else if ($row['sunday'] === 'P.M.') {
    echo '<span class="pmShift">';
   } else {
    echo '<span>';
   }

   echo "SUNDAY:". $row['sunday'] . '</span><br />';
   echo "<br />SHIRT SIZE:<h2>" . $row['shirt'] . "</h2>";
   echo "<br />VOLUNTEER NAME:<h2>" . $row['agreeName'] . "</h2>";
   echo "<p>Assigned as a volunteer for:<br />" . $row['position'] . "</p>";
   echo "<p>Shift times are scheduled for:<br />" . $row['shift_times'] . "</p>";
   echo "<p>Shifts have been confirmed:<br />" . $row['confirmed'] . "</p>";
   echo "<p>Checked in Friday:<br />" . $row['checkinfri'] . "</p>";
   echo "<p>Checked in Saturday:<br />" . $row['checkinsat'] . "</p>";
   echo "<p>Checked in Sunday:<br />" . $row['checkinsun'] . "</p>";
}
?>

<?php 
   $checkSAT = isset($_POST['checkinsat']) ? "yes" : "no";
   $updateSat = mysql_query("UPDATE volsMain SET checkinsat = '$checkSAT' WHERE ID = '$id'");
   $query = mysql_query($updateSat); 
   $result = mysql_query($query);
   echo "<p>" . print_r($checkSAT) . "</p>";
?>

4 个答案:

答案 0 :(得分:1)

   $checkSAT = isset($_POST['checkinsat']) ? "yes" : "no";

答案 1 :(得分:0)

SET checkinsat = '$checkSAT' ...

变量在PHP中区分大小写。

答案 2 :(得分:0)

只要快速查看它,就可能是您的代码:

$updateSat = mysql_query("UPDATE volsMain SET checkinsat = '$checkSAT' WHERE ID = '$id'");

......应该是:

$updateSat = mysql_query('UPDATE volsMain SET checkinsat = ' . $checkSAT . ' WHERE ID = ' . $id . '');

同样应该:

$select = mysql_query("SELECT * FROM volsMain WHERE ID = '$id'");

到:

$select = mysql_query('SELECT * FROM volsMain WHERE ID = ' . $id . '');

这肯定是粗略的:

    $updateSat = mysql_query("UPDATE volsMain SET checkinsat = '$checkSAT' WHERE ID =  '$id'");
$query = mysql_query($updateSat); 
$result = mysql_query($query);

$ updateSat不是纯SQL查询,不应在“mysql_query”中调用。如果您希望将其命名为$ result,请执行以下操作:

        $result = mysql_query("UPDATE volsMain SET checkinsat = '$checkSAT' WHERE ID =  '$id'");

答案 3 :(得分:0)

兄弟!

  1. 您是否注意到在循环中将表单标记放入其中? 检查此生成的网页的查看源,看看有多少html表单标签显示在那里? 将html表单标记放在循环中是一种错误的方法。你应该在表单元素上使用循环。

  2. 我建议不要在DB列中保存是/否,而是保存1&amp; 0值。