updating multiple records

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
onadvance
Forum Newbie
Posts: 3
Joined: Mon Nov 07, 2011 11:26 am

updating multiple records

Post 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
Last edited by Benjamin on Mon Nov 07, 2011 3:23 pm, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: updating multiple records

Post by Celauran »

Fix the missing single quote in $rs['price] and move the include outside of your foreach loop. Also, use [ syntax=php] tags.
onadvance
Forum Newbie
Posts: 3
Joined: Mon Nov 07, 2011 11:26 am

Re: updating multiple records

Post by onadvance »

Yes of course. Thank you. This is the modified code here. The problem is something else. Any idea, despite this missing?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: updating multiple records

Post by mikosiko »

post your updated code again and explain clearly what exactly "doesn't work"
onadvance
Forum Newbie
Posts: 3
Joined: Mon Nov 07, 2011 11:26 am

Re: updating multiple records

Post 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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: updating multiple records

Post 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
Post Reply