...

View Full Version : inserting date time



Jacobb123
03-21-2007, 04:26 AM
I have a mysql database that the row is setup as type 'date time'. I don't want to change this as I am working with OScommerce and it would cause some confusion. How can I insert the date and time into this table with the following code.


<?php

$products_quantity=$_POST['prod_quant'];
$products_model=$_POST['prod_model'];
$products_image='localhost/images/female.gif';
$products_price=$_POST ['price'];
$products_date_added=
$products_last_modified='null';
$products_date_available='null';
$products_weight=$_POST ['prod_weight'];
$products_status='1';
$products_tax_class_id='1';
$manufacturer='10';

$query = "INSERT INTO products (products_quantity, products_model,products_image, products_price, products_date_added, products_weight, products_status, products_tax_class_id,manufacturers_id)
VALUES ('$products_quantity', '$products_model','$products_image', '$products_price','$products_date_added', '$products_weight','$products_status','$products_tax_class_id','10')";

mysql_query($query) or die (mysql_error()) ?>

The variable "$products_date_added=" is where I want to add it from. I cannot figure out how to add the date and time with the row type. Any help is appreciated. Thanks

Inigoesdr
03-21-2007, 04:41 AM
Use NOW() for the value.

Jacobb123
03-21-2007, 05:32 AM
so it should look like this

$products_date_added=NOW();


if so that is not working

Inigoesdr
03-21-2007, 06:06 AM
No, it should look like this:

$query = "INSERT INTO products (products_quantity, products_model,products_image, products_price, products_date_added, products_weight, products_status, products_tax_class_id,manufacturers_id)
VALUES ('$products_quantity', '$products_model','$products_image', '$products_price', NOW(), '$products_weight','$products_status','$products_tax_class_id','10')";

Jacobb123
03-21-2007, 06:12 AM
I get this error message:

Fatal error: Call to undefined function NOW() in C:\server\www\store\vendors\add_product1.php on line 17

This is the code
$query = "INSERT INTO products (products_quantity, products_model,products_image, products_price, products_date_added, products_weight, products_status, products_tax_class_id,manufacturers_id)
VALUES ('$products_quantity', '$products_model','$products_image', '$products_price', NOW(), '$products_weight','$products_status','$products_tax_class_id','10')";

ArcticFox
03-21-2007, 10:30 AM
How about this:




<?php

$products_quantity=$_POST['prod_quant'];
$products_model=$_POST['prod_model'];
$products_image='localhost/images/female.gif';
$products_price=$_POST ['price'];
$products_date_added=date('l, F jS Y - g:ia', time() - 25200);
$products_last_modified='null';
$products_date_available='null';
$products_weight=$_POST ['prod_weight'];
$products_status='1';
$products_tax_class_id='1';
$manufacturer='10';

$query = "INSERT INTO products (products_quantity, products_model,products_image, products_price, products_date_added, products_weight, products_status, products_tax_class_id,manufacturers_id)
VALUES ('$products_quantity', '$products_model','$products_image', '$products_price','$products_date_added', '$products_weight','$products_status','$products_tax_class_id','10')";

mysql_query($query) or die (mysql_error()) ?>



Edit red seconds to compensate for your timezone.

aedrin
03-21-2007, 04:05 PM
Just as a hint, ArcticFox. I would never put a fixed timezone adjustment in code like that. Either use a define(), variable or adjust the timezone through PHP's settings.

NOW() is a MySQL function, not a PHP function. Don't use it in PHP, instead do what Inigoesdr showed you as an example.

Jacobb123
03-21-2007, 05:05 PM
I still cannot get this to work. The now() function does not work.

aedrin
03-21-2007, 05:50 PM
Define "does not work".

It gives an error, or it doesn't put in any value into the table, or ... etc.

Jacobb123
03-21-2007, 06:25 PM
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; , '','1','1','10')' at line 2


$query = "INSERT INTO products (products_quantity, products_model,products_image, products_price, products_date_added, products_weight, products_status, products_tax_class_id,manufacturers_id)
VALUES ('$products_quantity', '$products_model','$products_image', '$products_price',NOW(); , '$products_weight','$products_status','$products_tax_class_id','10')";


If I leave out the semicolon it does not do anything. the script executes but the value is not inserted

aedrin
03-21-2007, 07:28 PM
The only semicolon in MySQL should be the one at the end of a statement. And with a single one you don't need it.

Take out the semicolon, and do this after assigning to $query. Then check to see whether it is using the right values.



echo $query;

Inigoesdr
03-21-2007, 07:30 PM
You need to fix the last line too:

mysql_query($query) or die (mysql_error())
Should be:

mysql_query($query) or die (mysql_error());

Jacobb123
03-21-2007, 07:40 PM
This what I get when I print it to screen.



INSERT INTO products (products_quantity, products_model,products_image, products_price, products_date_added, products_weight, products_status, products_tax_class_id,manufacturers_id) VALUES ('', '','localhost/images/female.gif', '', NOW() , '','1','1','10')

aedrin
03-21-2007, 09:09 PM
That is mostly for yourself. As without knowing the database/design it is hard to say what is wrong when there are no errors.

That looks fine to me.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum