简体   繁体   English

使用复选框更新数据库列

[英]Update database column with checkbox

I am having trouble getting a column to update to yes if checked. 如果选中该列,则无法将列更新为是。 I'm not getting any errors though. 我没有任何错误。 What am I doing wrong? 我究竟做错了什么? I've tried a number of methods including leaving the array in and out of quotes. 我尝试了许多方法,包括使数组不加引号。 I know how to do this via a form w/ an email address but not checkboxes. 我知道如何通过带有电子邮件地址但不包含复选框的表单来执行此操作。 I also know how to delete rows from a database using checkboxes. 我也知道如何使用复选框从数据库中删除行。 But not update... 但不更新...

  <?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>";
?>
   $checkSAT = isset($_POST['checkinsat']) ? "yes" : "no";
SET checkinsat = '$checkSAT' ...

变量在PHP中区分大小写。

Just by quickly looking at it, could it be that your code: 只需快速查看一下,可能就是您的代码:

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

...should instead be: ...应改为:

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

?

Same should go for: 同样应该去:

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

to: 至:

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

Here is something that for sure is sketchy though: 这是可以肯定的粗略的东西:

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

$updateSat is not a pure SQL query and should not be called in "mysql_query". $ updateSat不是纯SQL查询,不应在“ mysql_query”中调用。 If you want it to be named $result, just do: 如果要将其命名为$ result,请执行以下操作:

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

Brother ! 兄弟!

  1. Did you notice that you are putting form tag inside while loop ? 您是否注意到要在while循环中放入表单标签? Check the view source of this generated web page and see how many html form tags are showing there ? 检查此生成的网页的查看源,并查看其中显示了多少个html表单标记? Its a wrong approach to put html form tag inside a loop. 将html表单标签放入循环中是错误的方法。 You should be using loop on form elements instead. 您应该在表单元素上使用循环。

  2. Instead of saving yes/no in DB column, I would recommend to save 1 & 0 values. 建议不要保存1和0值,而不是在数据库列中保存是/否。

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

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