php - Using for() to update multiple rows in mysql database -
i trying update multiple rows in database. have total number of rows in table counted , bound variable:
$result = '32'
i attempt query each row:
for ($x = 0; $x <= $result; $x++) { if ($update = $db -> prepare("update substrates set substrate_name = ?, substrate_desc = ?, substrate_base = ?, substrate_tax = ? substrate_id = ?")) { $substrate_name = $_post['substrate_name']; $substrate_desc = $_post['substrate_desc']; $substrate_base = $_post['substrate_base']; $substrate_tax = $_post['substrate_tax']; $substrate_id = $x; $update -> bind_param('sssss', $substrate_name, $substrate_desc, $substrate_base, $substrate_tax, $substrate_id); if ($update -> execute() == true) { // success stuff... } else { // error stuff... } $update -> close(); } }
my problem when run script, each row in table filled last row edited. example:
aaa | aaaaa | 1.00 | 6.35 bbb | bbbbb | 2.00 | 6.35 ccc | ccccc | 3.00 | 6.35
becomes:
ccc | ccccc | 3.00 | 6.35 ccc | ccccc | 3.00 | 6.35 ccc | ccccc | 3.00 | 6.35
how can fix script update each row individually?
try this:
for ($x = 0; $x <= $result; $x++) { if ($update = $db -> prepare("update substrates set substrate_name = ?, substrate_desc = ?, substrate_base = ?, substrate_tax = ? substrate_id = ?")) { $substrate_name = $_post['substrate_name'][$x]; $substrate_desc = $_post['substrate_desc'][$x]; $substrate_base = $_post['substrate_base'][$x]; $substrate_tax = $_post['substrate_tax'][$x]; $substrate_id = $x; $update -> bind_param('sssss', $substrate_name, $substrate_desc, $substrate_base, $substrate_tax, $substrate_id); if ($update -> execute() == true) { // success stuff... } else { // error stuff... } $update -> close(); } }
Comments
Post a Comment