0

I am learning php at the moment and have decided on creating a simple to do list that works just as I wanted it to do. But I decided a good idea would be to add in a dat stamp of when the item was added to the to do list, you know, to keep it organised.

Now when I manually insert an item via phpmyadmin and select todays date, the page displays the date as expected. Yet when I submit an item via my script on the front end the date is set to 0000-00-00 and I am at a point where I am a little stuck, I believe it is to do with the part below not inserting anything relating to the date, but I am not sure what do, I have tried to research but am a little confused:

$content = mysqli_real_escape_string($con, $_POST['content']); $sql="INSERT INTO items (content) VALUES ('$content')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } header('Location: index.php'); mysqli_close($con); 

The item is submitted via this form:

<form action="insert.php" method="post"> <input type="text" name="content"> <input type="submit" value="Add Item"> </form> 

And displayed with this:

$result = mysqli_query($con,"SELECT * FROM items"); while($row = mysqli_fetch_array($result)) { echo $row['added'] . "<br>" . $row['content']; echo "<br>"; echo "Mark as complete"; echo "<br>"; echo "<a onclick='confirmUser()' href='delete.php?id=" . $row['id'] . "'>Delete Item</a>"; echo "<br>"; echo "<a href='update.php?id=" . $row['id'] . "'>Edit Item</a>"; echo "<br>"; echo "<br>"; } mysqli_close($con); 
8
  • I see nothing that is enforcing valid dates being inserted into your database. I bet your date format is incorrect. Commented Jul 14, 2014 at 13:34
  • Define a default value for the date column in your database Commented Jul 14, 2014 at 13:34
  • Doesn't "mysqli_real_escape_string" rather defeat the point of the whole mysqli API? Commented Jul 14, 2014 at 13:36
  • @MarkBaker So that did the trick, how do I handle the fact it is showing the time because I have to select CURRENT_TIMESTAMP with DATETIME as will not allow me to just use DATE? Commented Jul 14, 2014 at 13:44
  • Format the value when you need to display it Commented Jul 14, 2014 at 13:52

2 Answers 2

1

Define default value for date column to current_timestamp.

Sign up to request clarification or add additional context in comments.

Comments

0

either set an default value in database, or add NOW() in your query.

example: $sql="INSERT INTO items (content, date) VALUES ('$content', NOW())"; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.