Php 如何基于复选框输入更新MySQL列

Php 如何基于复选框输入更新MySQL列,php,mysql,checkbox,Php,Mysql,Checkbox,我是这个论坛的新成员。对于使用PHP进行编码,作为新手,我希望得到您的帮助 我面临的问题如下 我有一个MySQL表'annound',其中包含以下字段 id ---------- advert ---------- date ---------- file ---------- approv ---------- 从页面中填充数据的对象-其中除approv之外的所有列都获取其值。要填充该字段,有一个新的PHP页面。字段approv根据其中复选框的勾号获取值“approved” 我面临的问题是,

我是这个论坛的新成员。对于使用PHP进行编码,作为新手,我希望得到您的帮助

我面临的问题如下

我有一个MySQL表'annound',其中包含以下字段

id
----------
advert
----------
date
----------
file
----------
approv
----------
从页面中填充数据的对象-其中除approv之外的所有列都获取其值。要填充该字段,有一个新的PHP页面。字段approv根据其中复选框的勾号获取值“approved”

我面临的问题是,我无法读取显示id的文本框的值并获取相应的复选框值,以便特定记录得到批准并更新到MySQL表

下面给出了我编写的代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org     
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Annpouncements | Pending List</title>
<style type="text/css">
.textinput {
height: 20px;
width: 20px;
border-top-width: 0px;
border-right-width: 0px;
border-bottom-width: 0px;
border-left-width: 0px;
border-top-style: none;
border-right-style: none;
border-bottom-style: none;
border-left-style: none;
}
</style>
</head>

<body>

<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="164"><form id="form1" name="form1" method="post" action="">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="11%" height="31" align="center"><label>Id</label></td>
          <td width="15%" height="31" align="center"><label>Date</label></td>
          <td width="52%" align="center"><label>Title</label></td>
          <td width="22%" align="center"><label>Status</label></td>

        </tr>
<?php
//Open the table announce from the database and list date in descending order
$result = mysql_query("SELECT * FROM announce ORDER by date DESC" )or  
die(mysql_error());

//Define a variable to get the rows of the table
  $ann = mysql_fetch_array($result);

//Define a variable to get the no of rows 
  $num = mysql_num_rows($result);
  $i=0;

while($i<$num) {?>
    <?php $approv[$i]= mysql_result($result,$i,"approv"); ?>

        <tr>
            <?php if($approv[$i] !== "approved"){?> 
            <td height="36" align="center"><input name="id" type="text" 
            class="textinput" id="id" value="<?php echo mysql_result($result,$i,"id"); 
            ?>" /></td>
           <?php $ids = mysql_result($result,$i,"id"); 
    //$inp = $_POST["id"][$i];
    //echo 'Input value : ' .$inp. '<br/>' ?>
            <td height="36" align="center"><label>
            <?php echo  mysql_result($result,$i,"date"); ?></label></td>
            <td align="center"><label><?php echo mysql_result($result,$i,"advert"); 
             ?></label></td>

            <td align="center"></label><input type="checkbox" name="approv[]" />
              <label for="approv"></label></td>
             <?php $idan = mysql_query("SELECT * FROM announce WHERE id == $ids"); ?>
             <?php 
               if (isset($_POST['button'])) 
        {
              $apprv = $_POST["approv"];
          //echo 'id = '.$ids.'<br/>';
           $how_many = count($apprv);
         //echo 'Row selected' .$how_many. '<br/>'; 
           foreach ($_POST['approv'] as $apprValue)
           $txtvalue[] = $_POST[$apprValue];
           echo 'txtvalue = ' .$txtvalue. '<br/>';
           mysql_query("UPDATE announce SET approv = 'approved'WHERE id == 
                   $idan ");
                      }
    } 
         ?>
  <?php } ?>

        </tr>

        <?php 
        $i++;
      }
    ?>

        <tr>
          <td height="44">&nbsp;</td>
          <td>&nbsp;</td>
          <td align="center">&nbsp;</td>
          <td align="center"><input type="submit" name="button" id="button" 
           value="Submit" /></td>
        </tr>

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

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

公告|待定清单
.textinput{
高度:20px;
宽度:20px;
边框顶部宽度:0px;
右边框宽度:0px;
边框底宽:0px;
边框左宽度:0px;
边框顶部样式:无;
右边框样式:无;
边框底部样式:无;
左边框样式:无;
}
身份证件
日期
标题
地位

我在您的查询中发现了一些问题

这个

应该是

SELECT * FROM announce WHERE id = '$ids'
还有这个

UPDATE announce SET approv = 'approved'WHERE id == $idan
应该是

UPDATE announce SET approv = 'approved' WHERE id = '$idan'
您的复选框也没有值属性

<input type="checkbox" name="approv[]" value="<echo your table row id here>" />

另一方面,请不要再使用mysql_*函数。它们很快就会被弃用。最好使用
mysqli
PDO
好的,所以这里有很多问题,但我想尝试一下生成复选框的代码行。具体而言,以下位:

 <input name="id" type="text" class="textinput" id="id" value="<?php echo mysql_result($result,$i,"id"); ?>" />
 <input type="checkbox" name="approv[]" />

应该更像:

if (empty($_POST)) {
    // Only run the query when we need to display information
    $result = mysql_query("SELECT * FROM announce ORDER by date DESC") or die(mysql_error());

    //Define a variable to get the rows of the table
    $result_rows = mysql_fetch_array($result);
    foreach($result_rows as $row) { ?>
        ... // Display form
    <? }
} else {
    $success = array();
    foreach($_POST['approv'] as $approved_id) {
        // Ideally we would combine all of these updates into one statement
        $stmt = mysqli_prepare($link, "UPDATE announce SET approv = 'approved' WHERE id = ?i");
        mysqli_stmt_bind_params($stmt, $approved_id);
        // You then can look over the $success array to see if any failed.
        $success[$approved_id] = mysqli_stmt_execute($stmt);
    }
}
if(空($\u POST)){
//仅在需要显示信息时运行查询
$result=mysql_query(“按日期说明从公告订单中选择*”)或die(mysql_error());
//定义一个变量以获取表的行
$result\u rows=mysql\u fetch\u数组($result);
foreach($result_行作为$row){?>
…//显示窗体

你需要首先修复很多错误。逐个解决错误并发布具体问题。到目前为止,问题很多!!
mysql\u查询(“UPDATE announce SET approv='approved'WHERE id=$idan”);
应该变成两条语句。(1)
$stmt=mysqli\u prepare($link,“UPDATE Annound SET approv='approved'WHERE id=?”;
和(2)
mysqli_stmt_bind_params($stmt,$idan);
这将阻止SQL注入。非常感谢。我更正了这些错误。请进一步帮助我解决具体问题。谢谢kyle。我正在按照您所说的更新代码,并会回复您。
 <input name="id" type="text" class="textinput" id="id" value="<?php echo mysql_result($result,$i,"id"); ?>" />
 <input type="checkbox" name="approv[]" />
<input type="checkbox" name="approv[]" />
<input type="checkbox" name="approv[]" value="<?= $id ?>" />
//Define a variable to get the rows of the table
  $ann = mysql_fetch_array($result);

//Define a variable to get the no of rows 
  $num = mysql_num_rows($result);
  $i=0;

while($i<$num) {?>
    ... // do work
    ... // Do something with $_POST. <-- wrong!!!
<? } ?>
if (empty($_POST)) {
    // Only run the query when we need to display information
    $result = mysql_query("SELECT * FROM announce ORDER by date DESC") or die(mysql_error());

    //Define a variable to get the rows of the table
    $result_rows = mysql_fetch_array($result);
    foreach($result_rows as $row) { ?>
        ... // Display form
    <? }
} else {
    $success = array();
    foreach($_POST['approv'] as $approved_id) {
        // Ideally we would combine all of these updates into one statement
        $stmt = mysqli_prepare($link, "UPDATE announce SET approv = 'approved' WHERE id = ?i");
        mysqli_stmt_bind_params($stmt, $approved_id);
        // You then can look over the $success array to see if any failed.
        $success[$approved_id] = mysqli_stmt_execute($stmt);
    }
}