Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    311
    Thanks
    0
    Thanked 1 Time in 1 Post

    inserting date time

    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 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

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Use NOW() for the value.

  • #3
    Regular Coder
    Join Date
    Aug 2006
    Posts
    311
    Thanks
    0
    Thanked 1 Time in 1 Post
    so it should look like this

    $products_date_added=NOW();


    if so that is not working

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    No, it should look like this:
    PHP 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')"


  • #5
    Regular Coder
    Join Date
    Aug 2006
    Posts
    311
    Thanks
    0
    Thanked 1 Time in 1 Post
    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
    PHP 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')"


  • #6
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    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.
    Last edited by ArcticFox; 03-21-2007 at 09:33 AM.
    <div> - putting your mind in a box since 1997

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Aug 2006
    Posts
    311
    Thanks
    0
    Thanked 1 Time in 1 Post
    I still cannot get this to work. The now() function does not work.

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Define "does not work".

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

  • #10
    Regular Coder
    Join Date
    Aug 2006
    Posts
    311
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

    PHP 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')"

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

  • #11
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

    PHP Code:
    echo $query

  • #12
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You need to fix the last line too:
    PHP Code:
    mysql_query($query) or die (mysql_error()) 
    Should be:
    PHP Code:
    mysql_query($query) or die (mysql_error()); 
    Last edited by Inigoesdr; 03-21-2007 at 06:31 PM. Reason: aedrin posted faster

  • #13
    Regular Coder
    Join Date
    Aug 2006
    Posts
    311
    Thanks
    0
    Thanked 1 Time in 1 Post
    This what I get when I print it to screen.

    PHP Code:
    [PHP]INSERT INTO products (products_quantityproducts_model,products_imageproducts_priceproducts_date_addedproducts_weightproducts_statusproducts_tax_class_id,manufacturers_idVALUES ('''','localhost/images/female.gif'''NOW() , '','1','1','10'
    [/PHP]

  • #14
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •