insert statement prob

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
orodsem
Forum Newbie
Posts: 2
Joined: Tue Nov 03, 2009 9:04 pm

insert statement prob

Post 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
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: insert statement prob

Post 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";
    }
}
 
Post Reply