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
gothica
Forum Newbie
Posts: 15 Joined: Thu Sep 08, 2005 5:24 am
Post
by gothica » Tue Oct 04, 2005 8:04 pm
Hi i just need a little help with this code.
Code: Select all
$dbCon = mysql_connect ("host","user","password") ;
$dbUse = mysql_select_db("orderdb", $dbCon) ;
$amtQuery = "select prodAmount from ordercart" ;
$amtResult = mysql_query ($amtQuery,$dbCon) ;
$qtyQuery = "select prodQuantity from ordercart" ;
$qtyResult = mysql_query ($qtyQuery,$dbCon) ;
$qtyRow = mysql_num_rows ($qtyQuery) ;
$amt = mysql_fetch_array($amtResult) ;
$qty = mysql_fetch_array($qtyResult) ;
$total = array() ;
for ($count = 0;$count < $qtyRow;$count++)
{
$total[$count] = $amt[$count] * $qty[$count] ;
$totQuery = "insert into ordercart (prodTotal) values ($total[$count])" ;
$totResult = mysql_query ($totQuery,$dbCon) ;
}
mysql_close($dbCon) ;
what i want to happen here is that i want to insert a value on the prodTotal column one at a time for the duration of this loop...
Code: Select all
for ($count = 0;$count < $qtyRow;$count++)
{
$total[$count] = $amt[$count] * $qty[$count] ;
$totQuery = "insert into ordercart (prodTotal) values ($total[$count])" ;
$totResult = mysql_query ($totQuery,$dbCon) ;
}
currently my code is unable to do that. can you help me figure it out? thank you!
mickd
Forum Contributor
Posts: 397 Joined: Tue Jun 21, 2005 9:05 am
Location: Australia
Post
by mickd » Tue Oct 04, 2005 8:14 pm
Code: Select all
$totQuery = "insert into ordercart (prodTotal) values ($total[$count])" ;
should be
Code: Select all
$totQuery = "insert into ordercart (prodTotal) values ('$total[$count]')" ;
$total[$count] should be in '.
gothica
Forum Newbie
Posts: 15 Joined: Thu Sep 08, 2005 5:24 am
Post
by gothica » Tue Oct 04, 2005 8:22 pm
it doesn't work and btw, prodTotal column has a float data type so it's not necessary to put ' ' when inserting data. thanks for the reply though.
mickd
Forum Contributor
Posts: 397 Joined: Tue Jun 21, 2005 9:05 am
Location: Australia
Post
by mickd » Tue Oct 04, 2005 8:25 pm
using " for the mysql query means its supposed to convert all variables to their values so im not sure if this would help but try
$total[" . $count . "]
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Oct 04, 2005 8:26 pm
quotes can be implicitly converted by the database, so that part doesn't matter too much so long as the data is numeric only...
Code: Select all
"insert into ordercart (prodTotal) values ({$total[$count]})" ;may work..... echo the query string to see if the data is being inserted correctly...