Forum Moderators: coopster

Message Too Old, No Replies

while loop for retrive data from DB

         

yllai

2:32 am on May 4, 2005 (gmt 0)

10+ Year Member



hi,
Below is my php script for retrive data from database and allow user click on the 'Edit' button (each record have a button at the end of row)for editing the record data.

$request=mysql_query("SELECT * FROM stock ORDER BY category, code",$db);
echo"<form name=\"stock\" action=\"stock/modules/\" method=\"post\">";
echo"<table border=\"1\"><tr><td>Category</td><td>Code</td><td>&nbsp;</td></tr>";
while($row=mysql_fetch_array($request,$db)){
$id=$row["id"];
$category=$row["category"];
$code=$row["code"];
echo"<tr><td>$category</td><td>$code</td><td><INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\"><INPUT TYPE=\"SUBMIT\" NAME=\"op\" VALUE=\"Edit\"></td></tr>";
}
echo"</form>";

I can retrive the data well, but the 'id' value attached with each button always cache with the id of my last record, hence I always edit the wrong record. I think that is some problem with my while loop, but I can't find it out. Anyone can help? Any mistake with my script? Any tutorial/ example?

Thanks.

Stormfx

3:17 am on May 4, 2005 (gmt 0)

10+ Year Member



I don't see a problem there, really... Is it possible that the page that updates has a bug?

Can you post a bit of the code?

yllai

4:17 am on May 4, 2005 (gmt 0)

10+ Year Member



I don't think so, I can check that all data in my database are well, non duplicated.

Any other idea?

ironik

4:51 am on May 4, 2005 (gmt 0)

10+ Year Member



Your mysql_fetch_array() has an incorrect argument type. It should be:

mysql_fetch_array($request)

or alternately (if you want to define how it fetches the array):
mysql_fetch_array($request, MYSQL_ASSOC)
mysql_fetch_array($request, MYSQL_NUM)

I'm not sure if that's causing the 'duplication', but I'd hazard a guess that it might be (possibly confusing the function...?) Test it out see how you go.

yllai

5:29 am on May 4, 2005 (gmt 0)

10+ Year Member



I have amended my script as

mysql_fetch_array($request, MYSQL_ASSOC)

but I still get the same result. My problem is not with the data retrieved but is the value of 'id' which in hided when user click ob the 'Edit' button, the value will be pass to the edit function. I have checked out, it have got passing the wrong value, passed the id of last record for each record instead of each id for each record. In other word, no matter which record they click on, they will passing the id for last record.