-1

I have read many of the postings regarding current time stamp and tried many of the solutions provided, however none of them fixed my issue. I need a current time stamp to be added to the database. I think my code may be the reason why the other solutions are not working. Everything else post perfectly. The time stamp just gives me all "0", the other solutions I tried gave me a "line 6 error".

Here is my current code

<?php

$con = mysql_connect("mysql","username","password");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

   /* Prevent duplicate submissions */
if (isset($_COOKIE['FormSubmitted']))
{ 
show_error('You may only submit this form once per session!');
}

 mysql_select_db("seller", $con);

 $sql="INSERT INTO listing (name, email, website, url, dropdown, price, date, visitors, income, host, description)

VALUES

('$_POST[name]', '$_POST[email]', '$_POST[website]', '$_POST[url]', '$_POST  [dropdown]', '$_POST[price]', '$_POST[date]','$_POST[visitors]', '$_POST[income]', '$_POST[host]', '$_POST[description]', 'CURRENT_TIMESTAMP[subdate]' )";

 if (!mysql_query($sql,$con))

  {

   die('Error: ' . mysql_error());

  }

echo "Thank you for listing with us. <a href="#">Explore Now</a>";


mysql_close($con)

?>
vitr
  • 6,766
  • 8
  • 30
  • 50
Reed
  • 127
  • 10
  • 3
    For the love of `$DEITY`, this is 2016. **STOP USING THE `mysql_` APIS, and START USING PARAMETERIZED QUERIES**. – Jonathon Reinhart Jul 28 '16 at 00:47
  • Read up on CURRENT_TIMESTAMP https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_current-timestamp - Plus, you're also trying to enter it as a string. – Funk Forty Niner Jul 28 '16 at 00:56
  • 1
    possible duplicate of [MySQL - insert current date/time?](http://stackoverflow.com/questions/19246309/mysql-insert-current-date-time) – Funk Forty Niner Jul 28 '16 at 01:18

1 Answers1

0

Using CURRENT_TIMESTAMP in the PHP context has no sense!

You have to include the needed default value in your table definition.
Something like this:

CREATE TABLE listing
...
description VARCHAR(...)
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
...

Then in PHP you should only write:

$sql="
  INSERT INTO listing (name, ..., description)
  VALUES ('$_POST[name]', ..., '$_POST[description]')
";
cFreed
  • 4,404
  • 1
  • 23
  • 33
  • *"Using CURRENT_TIMESTAMP in the PHP context has no sense!"* - You mean more like: *"Using `'CURRENT_TIMESTAMP[subdate]'` in the PHP context has no sense!"* - `CURRENT_TIMESTAMP()` or `NOW()` will also both work when used as core MySQL functions and that the column's type is correct. – Funk Forty Niner Jul 28 '16 at 01:19
  • @Fred-ii- Not sure to understand what you mean. Indeed, what has no sense is the complete string used in the OP, but I didn't make the effort to report it exactly, since: 1) _also_ the simple `CURRENT_TIMESTAMP` has no sense in PHP; 2) it _has_ sense in SQL, as is. What puzzles me is that you seem to consider 1) above as false? – cFreed Jul 28 '16 at 01:26
  • No, I never said your answer was false ;-) Just being a bit more specific. – Funk Forty Niner Jul 28 '16 at 01:29
  • @Fred-ii- Anyway, I felt compelled to precise my answer: look at my edit. – cFreed Jul 28 '16 at 01:32
  • I saw the edit and seeing the new code you're suggesting, won't enter the current time, unless the OP has an AI'd column. As I already stated, using CURRENT_TIMESTAMP() or NOW() will, yet the OP's column needs to be the correct type. We've gotten no word from the OP yet, so it's up in the air. I opted not to post an answer for this because of quite a few unknowns. Wishing you well though ;-) – Funk Forty Niner Jul 28 '16 at 01:34
  • @Fred-ii- Sorry, I don't understand at all! You must know that I'm not native-english speaker, so frequently I'm not sure how to interpret what I'm reading, as soon as it's not as precisely expressed than when coding. More over, I don't know what is an AI'd column. Sorry again, could you clarify? TIA – cFreed Jul 28 '16 at 20:40
  • AI = AUTO_INCREMENT. A (date) column will only add the current timestamp automatically if there is an AI'd column when using `CURRENT_TIMESTAMP` during the table creation. Otherwise, the function itself needs to be used `CURRENT_TIMESTAMP()`. The `()` denotes a function. – Funk Forty Niner Jul 28 '16 at 22:53
  • @Fred-ii- Uh... now I understand! But I remain in trouble, because: 1) I vaguely remember something like this constraint but forgot it for years since I _always_ add an AI field to any table structure, as a fundamental rule; 2) so I wanted to retrieve precise information about that... and get quite puzzled: I didn't discovered anything about it, nor googling nor in the MySql documentation! Am I struck with Alzheimer's? – cFreed Jul 29 '16 at 01:12
  • @cFreed This is what I am tying to do. I need to know the day a person signs up so in 30 days their submission will be decommissioned. I have a column in my database for date, so I just need to figure out how to automatically have the date added based on when the person submits the form. If there is an easier or current way of doing this I am open. I am new to SQL and PHP. All books and website I have been reading have not given me the results I am looking for. Thanks again for all your help. – Reed Jul 30 '16 at 19:20
  • @Mickeyatty What is stated by my answer effectively does what you want. Note that: 1) your date column _must_ be of `TIMESTAMP` type; 2) you _must_ not cite it in your `INSERT` query. – cFreed Jul 30 '16 at 22:17
  • @Mickeyatty Glad to help. So might you consider accepting my answer? – cFreed Jul 31 '16 at 09:14
  • Yes, I am going to try it out. Hopefully I can get it to work. – Reed Jul 31 '16 at 14:22
  • @Mickeyatty Oh sorry! Due to your "Perfect", I thought you had already had it working. – cFreed Jul 31 '16 at 18:00
  • @cFreed it worked. Thank you so much for your help on this one! – Reed Aug 02 '16 at 23:24