Page 1 of 1

Unable to insert price into database

Posted: Fri Jun 07, 2002 12:51 pm
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

Posted: Fri Jun 07, 2002 3:14 pm
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

Posted: Fri Jun 07, 2002 3:17 pm
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.

Posted: Fri Jun 07, 2002 3:23 pm
by Johnm
Sorry for the error Jason,
Informix must be a bit more picky than MySql then.

Posted: Fri Jun 07, 2002 4:13 pm
by jason
Johnm: hehe, probably. =)

For MySQL:

Code: Select all

INSERT INTO table_name (field3, field5, field6)
VALUES ( 'value3', 'value5', 'value6' )
Is alright.

Posted: Fri Jun 07, 2002 4:13 pm
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

Posted: Sat Jun 08, 2002 3:57 am
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?