nuttycoder Posted July 7, 2007 Share Posted July 7, 2007 Hi I am trying to auto date dates into the database with every entry but so far all can get is 0000-00-00 00:00:00 using datetime data type. Here is my code for inserting into the databse if(isset($_POST['Submit'])) { $newsTitle = $_POST['newsTitle']; $newsCont = $_POST['newsCont']; if(!get_magic_quotes_gpc()) { $newsTitle = addslashes($newsTitle); $newsCont = addslashes($newsCont); } $query = " INSERT INTO news (newsTitle, newsCont, newsDate) VALUES ('$newsTitle', '$newsCont', '$newsDate')"; mysql_query($query) or die('Error ,query failed'); echo "'$newsTitle' added"; } ?> <table width="71%" border="0" align="center"> <tr> <td> <form method="post" action="insert-news.php"> <input type="hidden" name="newsID" value="<?=$newsID;?>"> <p>News Title <br /> <input name="newsTitle" type="text" size="40" maxlength="255" > <br /> <br /> News Content<br /> <textarea name="newsCont" cols="80" rows="15" id="newsCont"></textarea> <input type="hidden" name="newsDate" value="<?=$newsDate;?>"> <br /> <input type="submit" name="Submit" value="Add News"> </p> <p align="center"><a href="../stories">Back to Admin</a></p> </form> </td> </tr> </table> Any help on guide me where am going wrong would be great Thanks Quote Link to comment https://forums.phpfreaks.com/topic/58847-adding-dates-to-mysql/ Share on other sites More sharing options...
Wildbug Posted July 7, 2007 Share Posted July 7, 2007 You want timestamped rows? Either use the TIMESTAMP column type which will automatically set itself on an INSERT or UPDATE or a DATETIME column (like you already have) using the NOW() function when you INSERT the row. Quote Link to comment https://forums.phpfreaks.com/topic/58847-adding-dates-to-mysql/#findComment-291981 Share on other sites More sharing options...
nuttycoder Posted July 7, 2007 Author Share Posted July 7, 2007 Thanks I tried doing the NOW() <input type="hidden" name="newsDate" value="<?= NOW();?>"> but that gives me Fatal error: Call to undefined function: Quote Link to comment https://forums.phpfreaks.com/topic/58847-adding-dates-to-mysql/#findComment-291994 Share on other sites More sharing options...
Wildbug Posted July 7, 2007 Share Posted July 7, 2007 Eh... NOW() is a MySQL function. And don't put it in the form; put it in the code that inserts the row. <?php $query = "INSERT INTO news (newsTitle, newsCont, newsDate, timeStampColumn) VALUES ('$newsTitle', '$newsCont', '$newsDate',NOW())"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/58847-adding-dates-to-mysql/#findComment-292002 Share on other sites More sharing options...
nuttycoder Posted July 7, 2007 Author Share Posted July 7, 2007 I have changed my table structue to use timestamp and have changed my sql query to match <?php $query = "INSERT INTO news (newsTitle, newsCont, newsDate, timestampColumn) VALUES ('$newsTitle', '$newsCont', '$newsDate',NOW())"; ?> but now the request just fails. Quote Link to comment https://forums.phpfreaks.com/topic/58847-adding-dates-to-mysql/#findComment-292016 Share on other sites More sharing options...
Wildbug Posted July 7, 2007 Share Posted July 7, 2007 Have you checked for errors? If so, what are they? <?php $query = "INSERT INTO news (newsTitle, newsCont, newsDate, timestampColumn) VALUES ('$newsTitle', '$newsCont', '$newsDate',NOW())"; mysql_query($query) or die(mysql_error() . "<br />\nQuery: $query"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/58847-adding-dates-to-mysql/#findComment-292032 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.