Page 1 of 1

insert statement prob

Posted: Tue Nov 03, 2009 9:10 pm
by orodsem
Hi all, im using PHP and MySql. The following parts show my DB table details and the insert statements which made me confused!!!

create table cart(cart_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
product_id int,
product_name varchar(1000),
product_price float(10,2)
);
the first statement works correctly, but in the second not!!!
mysql_query("INSERT INTO cart (product_id, product_name)
VALUES (" . $row["product_id"] . "12" . ")") or die(mysql_error());
mysql_query("INSERT INTO cart (product_id, product_name)
VALUES (" . $row["product_id"] . "Milk" . ")") or die(mysql_error());
i got "Unknown column 'Milk' in 'field list'"... any idea appreciated... 8O

Re: insert statement prob

Posted: Tue Nov 03, 2009 11:59 pm
by mischievous
Here is a quick little foreach statement that will add any elements inside of the array $querys into the database.
The problem looked like you had miss-typed the query need to add in your '' around what your inputing...

Quick read for some help: http://www.w3schools.com/PHP/php_mysql_insert.asp

Code: Select all

 
$querys =  array(
    $row['product_id'] => 12,
    $row['product_id'] => "Milk"
    );
 
foreach($querys as $id => $name)
{
    $insert_query = "INSERT INTO cart (product_id, product_name) VALUES ('".$id."', '".$name."')";
    if(!mysql_query($insert_query))
    {
        die('Error: ' . mysql_error());
    } else {
        echo "Record Added";
    }
}