0

我正在使用这个:

foreach($affid as $id) {

   $id = @mysql_real_escape_string(strip_tags($id));

   $otherid = $_POST['postid'];

   $sql = "UPDATE dlbprog SET affId=".$id." WHERE id=".$otherid;

   echo $sql;

   //@mysql_query($sql) or die(mysql_error());
}

结果是这样的:

UPDATE dlbprog SET affId=323 WHERE id=5
UPDATE dlbprog SET affId=424 WHERE id=5 

而我想让它说:

UPDATE dlbprog SET affId=323 WHERE id=1
UPDATE dlbprog SET affId=424 WHERE id=5

(它将最后一个结果保留为当前结果)

这是我的表格:

<table border="1" width="90%" align="center" cellspacing="0" cellpadding="2">
 <tr>
  <td align="left" colspan="2">'.$row["description"].'</td>
 </tr>
 <tr>
  <td align="left" width="55%">
    To Sign Up <a href="'.$row["progUrl"].'" target="_blank">Click Here</a>
    <br />
    <input type="checkbox" name="blocked" value="'.$row['ownerId'].'"  />
    &nbsp;Block this program
  </td>
  <td align="left" width="45%">
   Your Affiliate Id: &nbsp; 
   <input type="text" class="input" size="30" name="affid[]" value="'.$row['affId'].'">
   <input name="postid" value="'.$row["id"].'">
  </td>
 </tr>
</table>
<p>&nbsp;</p>
4

2 回答 2

4

$_POST['postid']是一个过帐值。这个值在你的循环中不会改变,所以很明显它在每次迭代中都是一样的。

于 2012-10-19T21:56:11.170 回答
0

改变

<input name="postid" value="'.$row["id"].'">

<input name="postid[]" value="'.$row["id"].'">

将 PHP 更改为

foreach($i = 0; $i < count($affid); $i++) {
  $id = @mysql_real_escape_string(strip_tags($affid[$i]));
  $otherid = @mysql_real_escape_string(strip_tags($postid[$i]));

  $sql = "UPDATE dlbprog SET affId=".$id." WHERE id=".$otherid;

  echo $sql;

  //@mysql_query($sql) or die(mysql_error());
}
于 2012-10-19T22:04:01.933 回答