Jesse J.
Jesse J.

Reputation: 13

I am expecting different sql results

I am using this:

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());
}

It gives this as a result:

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

Whereas I want it to say:

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

(It's keeping the last result as the current result)

This is my form:

<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>

Upvotes: 0

Views: 66

Answers (2)

Philip Whitehouse
Philip Whitehouse

Reputation: 4157

Change

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

to

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

Change the PHP to

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());
}

Upvotes: 0

GolezTrol
GolezTrol

Reputation: 116110

$_POST['postid'] is a posted value. This value doesn't change within your loop, so obviously it is the same in each iteration.

Upvotes: 4

Related Questions