Unable to insert price into database

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
cooler75
Forum Commoner
Posts: 40
Joined: Wed May 29, 2002 2:43 pm
Location: RichIsland

Unable to insert price into database

Post by cooler75 »

Hello,

I am trying to insert price value into a database table, but it doesnt seem to capture and send, could someone point me to the right direction?

here is my code:


Code: Select all

$query = "INSERT INTO orders (date_order, price)
                values (NOW(), '$price')";

if (!mysql_query ($query, $link) )
			{
			die (mysql_error());
			 }

print "Thank you! Your order has been completed.";

if ($action == "order")
            print "<form name="order" action="./order.php? action=order" method=post>";
            print "Price:";
            print "<SELECT NAME="price" > ";
            print "<option value="9.99">9.99";
            print "<option value="19.99">19.99";  
            print "<option value="29.99">29.99";
            print "</select>";
            print "<input type=submit value=Order></form>";
And this is how mySQL table looks like:

Code: Select all

create table orders (
id int(11) AUTO_INCREMENT PRIMARY KEY,
clientid int(11),
order_date DATETIME,
price DOUBLE NOT NULL,
feature_price DOUBLE NOT NULL,
total DOUBLE NOT NULL
);
date_order is fine, i mean it's being inserted, but price shows 0 in the database, could someone tell me what did i miss, what i did wrong?

thank you
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

The insert statement is not correct and with an insert statement you must account for every field. If the field exists already, you need to use "update". If it doesn't already exist use insert but account for every field. Something like this:

Code: Select all

insert into orders 
values($some_var,$some_var,order_date,price,$some_var,$some_var);

or this:

insert into orders 
values($some_var,,order_date,price,0 or $some_var,0 or $some_var);

// what to do with the "$some_var":
// if  $some_var is an int with NULL allowed put either its value or
// leave it blank.
// if  $some_var is an int with NULL not allowed put either its value or a "0".
but I am guessing you probably have an existing field and need to use an update statement.

Hope I helped,
Direwolf
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

JohnM, actually, the insert statement is correct. It's inserting, as he said, just not the price.

cooler75: http://www.devnetwork.net/forums/viewtopic.php?t=511

Double check to make sure the above link isn't your problem. =)

And echo out your $query variable to see what you are really sending as the SQL.
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

Sorry for the error Jason,
Informix must be a bit more picky than MySql then.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Johnm: hehe, probably. =)

For MySQL:

Code: Select all

INSERT INTO table_name (field3, field5, field6)
VALUES ( 'value3', 'value5', 'value6' )
Is alright.
cooler75
Forum Commoner
Posts: 40
Joined: Wed May 29, 2002 2:43 pm
Location: RichIsland

Post by cooler75 »

problem has been fixed, thank you.

Johnm-thanks for you tips

Jason-That's not my problem because i am not using PHP 4.2

thanks
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

cooler75 wrote:problem has been fixed, thank you.

Johnm-thanks for you tips

Jason-That's not my problem because i am not using PHP 4.2

thanks
Okay but remember, you don't need to account for all fields in an insert statement, this is true for MySQL, Oracle, Access which all follow the ANSI 92 standard for SQL, as Johnm said maybe Informix is more picky.

So how did you fix it?
Post Reply