1

我是这个论坛的新成员。并使用 PHP 编码,所以作为新手,我希望得到你的所有帮助。

我面临的问题如下。

我有一个 MySQL 表“公告”,其中包含以下字段

id
----------
advert
----------
date
----------
file
----------
approv
----------

数据从页面填充到其中 - 除了批准之外,所有列都获得其值。要填充该字段,有一个新的 PHP 页面。字段 approv 根据其中复选框的勾号获取值“已批准”

我面临的问题是我无法读取显示 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>

因此,请帮助我获得正确的解决方案,以便仅针对已检查的那些行使用“已批准”值更新表。

期待您尽快提供宝贵的帮助

4

2 回答 2

0

A few issues that I found in your queries

This

SELECT * FROM announce WHERE id == $ids

Should be

SELECT * FROM announce WHERE id = '$ids'

And this

UPDATE announce SET approv = 'approved'WHERE id == $idan

Should be

UPDATE announce SET approv = 'approved' WHERE id = '$idan'

Your checkbox doesn't have a value attribute too

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

Then use

foreach($_POST['approv'] as $apprValue)
{
    mysql_query(UPDATE announce SET approv = 'approved' WHERE id = '$apprValue');
}

On a completely side note, please don't use mysql_* functions anymore. They are going to be deprecated soon. Better to go for mysqli or PDO

于 2012-09-06T05:23:38.170 回答
0

好的,所以这里有很多问题,但我将针对生成复选框的代码行来解决这个问题。特别是以下位:

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

出于多种原因,这并不是创建所需信息的可靠方法。

  1. $_POST['id']将只包含一个值。它不会创建似乎是您意图的批准状态的 id 映射。
  2. $_POST['approv']将是一个零索引数组,如果不保留to 循环计数id的映射,您将无法将复选框与 an 关联起来。id(但如果您要这样做,只需使用 PHP 为您提供的结构。)

好的,现在我们知道我们需要进行一些更改。首先,我们将尝试使复选框起作用。但是我们仍然会遇到一些代码结构的问题。特别是因为看起来您根本不了解请求是如何使用 PHP 工作的。

首先,我们想要一种方法来确保我们知道每个 id 的批准状态。这实际上很容易补救。我们可以改变:

<input type="checkbox" name="approv[]" />

<input type="checkbox" name="approv[]" value="<?= $id ?>" />

这样,当你循环时$_POST['approv'],你实际上得到了一个 id,而不仅仅是一个偏移量。这将允许您在底部保持相同的 for 循环。

但仍有问题

没有理由在渲染循环 ( while($i<$num)) 中查看 $_POST。使用 PHP 结构对您有利。foreach($result_rows as $row)比您的代码更惯用和可读:

//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);
    }
}
于 2012-09-06T05:46:33.843 回答