PDA

View Full Version : What am i doing wrong with UPDATE


halifaxer
05-31-2008, 02:42 AM
I have the following:

$qryTotal=mysql_query("SELECT * FROM admin WHERE dataID='911' AND inStock='yes' AND RRP=dataP ORDER BY brand,name")or
die(mysql_error());
$numrow=mysql_num_rows($qryTotal);

while($row = mysql_fetch_array( $qryTotal )) {

echo '<form method="post">';
echo $row[sku]." ".$row[brand]." ".$row[name]." <a href='".$row[dataLink]."' target='_new'>Link</a>";
echo '<input name="RRP">';
echo '<input type="submit">';
echo '</form>';

$dataLink=$row[dataLink];
$sku=$row[sku];

mysql_query("UPDATE admin SET RRP_test = '$_POST[RRP]' WHERE dataLink='$dataLink' AND sku='$sku'");

}

however it's updating every row within the query, not just the intended one. before you tell me off, yes it is neccessary for it to be contained within an output loop as only certain entries in my database require updating. and sku and dataLink are specific and individual, so i'm confused...

all help graciously received :D

tomws
05-31-2008, 03:45 AM
however it's updating every row within the query, not just the intended one.

It's updating every row because you're telling it to update every row. Explanation in comments.

// I assume * includes dataLink and sku
$qryTotal=mysql_query("SELECT * FROM admin WHERE dataID='911' AND inStock='yes' AND RRP=dataP ORDER BY brand,name")or
die(mysql_error());
$numrow=mysql_num_rows($qryTotal);

while($row = mysql_fetch_array( $qryTotal )) {

// snip form

// here, you're assigning these variables the values from each row
$dataLink=$row[dataLink];
$sku=$row[sku];

// and here, you're telling it to update each row where the values equal what you just set them to
mysql_query("UPDATE admin SET RRP_test = '$_POST[RRP]' WHERE dataLink='$dataLink' AND sku='$sku'");

}

Make sense?

If you intend to update only certain rows, restructure it to something better or use an if statement.