Page 1 of 1

simple coding question

Posted: Tue Oct 04, 2005 8:04 pm
by gothica
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!

Posted: Tue Oct 04, 2005 8:14 pm
by mickd

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 '.

Posted: Tue Oct 04, 2005 8:22 pm
by gothica
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. :)

Posted: Tue Oct 04, 2005 8:25 pm
by mickd
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 . "]

Posted: Tue Oct 04, 2005 8:26 pm
by feyd
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...