Page 1 of 1
updating multiple records
Posted: Mon Nov 07, 2011 11:45 am
by onadvance
Hi
I try to handle a code for updating multiple records from database. Here is the important part of the code which does not work:
Code: Select all
<?
include "db.php";
$sql = mysql_query("select * FROM price", $conn);
while ($rs= mysql_fetch_array($sql)){
$priceid = $rs['price_id'];
$price = $rs['price];
?><TR><TD style=font-size:11px> <input type="text" name="edprice['.$priceid.']" value="<? echo $price; ?>" class="width"> </TD></TR><?
}
?>
AND The updating code:
Code: Select all
<?
if ($edprice) {
foreach ($edprice as $priceid => $theprice)
if ($theprice>0) {
include "db.php";
$sql = mysql_query("UPDATE move SET price = '$theprice' WHERE price_id = '$priceid'");
}
}
?>
Any idea?
Thanks
Re: updating multiple records
Posted: Mon Nov 07, 2011 11:49 am
by Celauran
Fix the missing single quote in $rs['price] and move the include outside of your foreach loop. Also, use [ syntax=php] tags.
Re: updating multiple records
Posted: Mon Nov 07, 2011 12:01 pm
by onadvance
Yes of course. Thank you. This is the modified code here. The problem is something else. Any idea, despite this missing?
Re: updating multiple records
Posted: Mon Nov 07, 2011 1:22 pm
by mikosiko
post your updated code again and explain clearly what exactly "doesn't work"
Re: updating multiple records
Posted: Mon Nov 07, 2011 1:52 pm
by onadvance
pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Here is the updated code:
Code: Select all
<?
include "db.php";
$sql = mysql_query("select * FROM price", $conn);
while ($rs= mysql_fetch_array($sql)){
$priceid = $rs['price_id'];
$price = $rs['price'];
?><TR><TD style=font-size:11px> <input type="text" name="edprice['.$priceid.']" value="<? echo $price; ?>" class="width"> </TD></TR><?
}
?>
--------------------
AND The updating code:
<?
include "db.php";
if ($edprice) {
foreach ($edprice as $priceid => $theprice)
if ($theprice>0) {
$sql = mysql_query("UPDATE price SET price = '$theprice' WHERE price_id = '$priceid'");
}
}
?>
What that happens, is simply that nothing happens, no updating.
pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Re: updating multiple records
Posted: Mon Nov 07, 2011 2:12 pm
by mikosiko
onadvance wrote:Here is the updated code:
<?
include "db.php";
$sql = mysql_query("select * FROM price", $conn);
while ($rs= mysql_fetch_array($sql)){
$priceid = $rs['price_id'];
$price = $rs['price'];
?><TR><TD style=font-size:11px> <input type="text" name="edprice['.$priceid.']" value="<? echo $price; ?>" class="width"> </TD></TR><?
}
?>
this code is incomplete... no
form declared.. table code is incomplete...no calling the other piece of code... no record id to identify the target record, etc..etc... just to start with the simplest errors... do a search and read the right way to implement a FORM... try to fix your errors and came back if in doubt