简体   繁体   中英

Updating array values for a particular id in database

Im working on a web application where i need to let users update several tds in a tr of a table. As of now i can fetch and show the database values in the respective td's. But when i try to update them, the last entered values in the last tr gets updated for all the td's in the table.

Below is the part of the query im trying. All the values are considered array since values are gathered from several input box.

$cno="123";
$c  = count($ndv);
for($i=0;$i<$c;$i++)
{

$query_dv="UPDATE device SET cid = '$cno',name = '$ndv[$i]',type = '$tdv[$i]',serialno = '$sdv[$i]',model = '$mdv[$i]',location = '$ldv[$i]'  WHERE cid = ".$cno;

$sql_device = mysqli_query($conn, $query_dv) or die(mysqli_error($conn));

}

so say right now i try to update with values 123,1,1,1,1,1 and 123,2,2,2,2,2

i get 123,2,2,2,2,2 and 123,2,2,2,2,2 .. i do understand that its getting reupdated due to the for loop. So im trying to fix this part of the code. And im struggling to fix it. Any help would be appreciated.

Your issue is you are not uniquely identifying the row you are wanting to update, todo this add a column to your database called something like id make it the primary key and have it auto increment, then in your table add a hidden/disabled input containing the id and update your query to be something along the lines of

 $query_dv="UPDATE device SET name = '$ndv[$i]',type = '$tdv[$i]',serialno = '$sdv[$i]',model = '$mdv[$i]',location = '$ldv[$i]'  WHERE id = " . $id[$i];

I removed the update for cid because from your question it doesn't look like it changes so no need to update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM