如何使用复选框更新特定行

时间:2014-06-03 16:36:19

标签: php

我正在尝试更新从表单构建的表中的特定行。我正在使用复选框,但整个表正在更新,而不是我正在填写的特定行。我试过ISSET,看看我是否可以让它只在框被勾选时更新,但这不起作用。这就是我所拥有的(显然不是整个代码,但我可以根据需要提供更多代码):

<?php

include 'connect.php';


// All possible parameters
$params = array(
    'Student',
    'homeroom',
    'teacher'

);

$wheres = array();
foreach ($params as $param) {

    // Is the param set?
    // If so, let's add it to our list of WHERE clauses
    if (isset($_GET[$param]) && !empty($_GET[$param])) {
        $wheres[] = sprintf("%s LIKE '%%%s%%'", $param, mysqli_real_escape_string($connect, $_GET[$param]));
    }
}

if ($db_found) {

    // Now let's make the SQL.
    $SQL = 'SELECT * FROM studentlist';

    // We only want to add the WHERE clause if we had some parameters passed
    if (count($wheres) > 0) {
        $SQL .= ' WHERE ' . implode(' OR ', $wheres);
    }



    $result = mysqli_query($connect, $SQL);



    while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {

  echo "<tr>";
  echo "<td>" . $row['Student'] . "</td>";
  echo "<td>" . $row['homeroom'] . "</td>";
  echo "<td>" . "<input type='checkbox' name='responsibility' value='E'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='responsibility' value='G'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='responsibility' value='S'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='responsibility' value='N'>" . "</td>";
  echo "<td>" . "" . "</td>";
  echo "<td>" . "<input type='checkbox' name='organization' value='E'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='organization' value='G'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='organization' value='S'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='organization' value='N'>" . "</td>";
  echo "<td>" . "" . "</td>";
  echo "<td>" . "<input type='checkbox' name='independentwork' value='E'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='independentwork' value='G'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='independentwork' value='S'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='independentwork' value='N'>" . "</td>";
  echo "<td>" . "" . "</td>";
  echo "<td>" . "<input type='checkbox' name='collaboration' value='E'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='collaboration' value='G'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='collaboration' value='S'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='collaboration' value='N'>" . "</td>";
  echo "<td>" . "" . "</td>";
  echo "<td>" . "<input type='checkbox' name='initiative' value='E'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='initiative' value='G'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='initiative' value='S'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='initiative' value='N'>" . "</td>";
  echo "<td>" . "" . "</td>";
  echo "<td>" . "<input type='checkbox' name='selfregulation' value='E'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='selfregulation' value='G'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='selfregulation' value='S'>" . "</td>";
  echo "<td>" . "<input type='checkbox' name='selfregulation' value='N'>" . "</td>";

  echo "</tr>"; 

然后我将此作为我的更新:

            <?php

        include 'connect.php';



        // Get values from form 
        $resp=$_POST['responsibility'];
        $org=$_POST['organization'];
        $ind=$_POST['independentwork'];
        $coll=$_POST['collaboration'];
        $init=$_POST['initiative'];
        $self=$_POST['selfregulation'];



       // Insert data into mysqli 
       $sql = "UPDATE studentlist 
       SET responsibility='$resp', organization='$org', independentwork='$ind', 
       collaboration='$coll', initiative='$init', selfregulation='$self'";

       $result=mysqli_query($connect, $sql);

       // if successfully insert data into database, displays message "Successful". 
       if($result){
       echo "Successful";


       }

       else {
       echo "ERROR";
       }
       ?> 

<?php 
// close connection 
mysqli_close($connect);
?>

我知道我的所有复选框都命名相同,但我不确定如何摆脱它。是的,我是新手!!!!!

1 个答案:

答案 0 :(得分:0)

您必须添加到查询的末尾a1}}条件才能更新您想要的行,如果您使用例如WHERE作为您的学生的标识符,那么您写下:

StudentId