1

I have read several posts that relate to this question, but I still can not understand what I am doing wrong. For instance:

Saving timestamp in mysql table using php and Display the date of creation of a row in mysql

All I want is to have the date a record is created put in a column of mysql database and then post it to a php results table.

I have added a column to the database called 'Date' with Type:timestamp, Default:current_timestamp(), and Attributes:ON UPDATE CURRENT_TIMESTAMP(). For some reason all rows in the 'Date' column show nothing but zeroes?

How can I set up the column so it actually displays the creation date for each record?

Here is a screenshot of my database structure: enter image description here

And here is a screenshot of the database records:

enter image description here

Any help would be greatly appreciated.

5150 Design
  • 179
  • 14

1 Answers1

0

I figured it out! I was trying to insert the date via my insert.php script and it was finding a null value. Once I removed the insertion of 'Date' it works as expected. Rookie mistake.

Here is what the insert.php looked like prior to my edit:

$sql = "INSERT INTO Medicard (Date, Name, Number, PartA, PartB, Full_Name, Address, Apt, City, State, Zip, Phone, Email) VALUES ('$_POST[Date]','$_POST[Name]','$_POST[Number]','$_POST[parta]','$_POST[partb]','$_POST[Full_Name]','$_POST[Address]','$_POST[Apt]','$_POST[City]','$_POST[State]','$_POST[Zip]','$_POST[Phone]','$_POST[Email]')";

I then removed the 'Date' insertion and it worked:

$sql = "INSERT INTO Medicard (Name, Number, PartA, PartB, Full_Name, Address, Apt, City, State, Zip, Phone, Email) VALUES ('$_POST[Name]','$_POST[Number]','$_POST[parta]','$_POST[partb]','$_POST[Full_Name]','$_POST[Address]','$_POST[Apt]','$_POST[City]','$_POST[State]','$_POST[Zip]','$_POST[Phone]','$_POST[Email]')";
5150 Design
  • 179
  • 14
  • Great. For the other rookie mistake, see: [Why should I provide an MCVE for what seems to me to be a very simple SQL query?](https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry Jul 19 '19 at 16:57
  • @Strawberry You are correct. Had I included my insert.php code a more experienced coder may have been able to quickly provide a solution. I will be more diligent in the future. – 5150 Design Jul 19 '19 at 18:37
  • Incidentally, this approach leaves you exposed to sql injection. See about the importance of prepared and bound queries. – Strawberry Jul 25 '19 at 07:04