Mutley Posted August 7, 2006 Share Posted August 7, 2006 Hi there,Got a problem, I'm making a basic news script and want it to have a title form and a content for the news inputs.However, I don't want the user to input the current date all the time, so I would like it to put the current date automaticly in the database using a hidden field? I'm not sure but I can't get it to work, not done dates before.If someone could look the code over for me and help me out, that would be great:[code]<?phpif(isset($_POST['submit'])) { // Insert the data to the table require_once("connection.php"); // Only connect to the database if you're actually using the database $title = addslashes($_POST['title']); $date = $_POST['date']; $content = addslashes($_POST['content']); $sql = "INSERT INTO `news` "; $sql .= "(title, date, content) "; $sql .= "VALUES "; $sql .= "('".$title."', '"date('Y-m-d')"', '".$content."')"; mysql_query($sql); $profile_id = mysql_insert_id(); // Do insert //Redirect to form echo "<b>Profile Inserted</b><br /><br />"; echo "<a href=form.php>Click here to continue</a>"; exit();}else { // Show form?><form enctype="multipart/form-data" action="form.php" method="post"><table class="x2" width="80%"><tr><td>Title:</td><td><input type="text" size="20" maxlength="12" name="title"></td></tr><tr><td>Content:</td><td><textarea name="content" rows="5" cols="40"></textarea></td></tr> <input type="hidden" name="date" /><tr><td><input type="submit" name="submit" value="Add Profile"></td></tr></table></form><?php}?>[/code]Thanks. Link to comment https://forums.phpfreaks.com/topic/16783-form-to-post-current-date-help/ Share on other sites More sharing options...
obsidian Posted August 7, 2006 Share Posted August 7, 2006 rather than using a hidden field, if your data type is a DATE, you can just use the NOW() function in SQL:[code]<?php$sql = "INSERT INTO news (title, date, content) VALUES ('$title', NOW(), '$content')";?>[/code]hope this helps Link to comment https://forums.phpfreaks.com/topic/16783-form-to-post-current-date-help/#findComment-70600 Share on other sites More sharing options...
Mutley Posted August 7, 2006 Author Share Posted August 7, 2006 Still can't get it to post, it doesn't go into the database:[code]<?phpif(isset($_POST['submit'])) { // Insert the data to the table require_once("connection.php"); // Only connect to the database if you're actually using the database $title = addslashes($_POST['title']); $date = date('Y-m-d'); $content = addslashes($_POST['content']); $sql = "INSERT INTO news (title, date, content) VALUES ('$title', NOW(), '$content')"; $sql .= "VALUES "; mysql_query($sql); // Do insert //Redirect to form echo "<b>Profile Inserted</b><br /><br />"; echo "<a href=form.php>Click here to continue</a>"; exit();}else { // Show form?><form enctype="multipart/form-data" action="form.php" method="post"><table class="x2" width="80%"><tr><td>Title:</td><td><input type="text" size="20" maxlength="12" name="title"></td></tr><tr><td>Content:</td><td><textarea name="content" rows="5" cols="40"></textarea></td></tr><tr><td><input type="submit" name="submit" value="Submit News"></td></tr></table></form><?php}?>[/code] Link to comment https://forums.phpfreaks.com/topic/16783-form-to-post-current-date-help/#findComment-70611 Share on other sites More sharing options...
Orio Posted August 7, 2006 Share Posted August 7, 2006 I say:[code]<?php$title = addslashes($_POST['title']);$date = date('Y-m-d');$content = addslashes($_POST['content']);$sql = "INSERT INTO news (title, date, content) VALUES ('$title', '$date', '$content')";?>[/code]Is the best. Your error is probbly the " $sql .= "VALUES ";" you forgot to delete.Orio. Link to comment https://forums.phpfreaks.com/topic/16783-form-to-post-current-date-help/#findComment-70618 Share on other sites More sharing options...
Mutley Posted August 7, 2006 Author Share Posted August 7, 2006 Worked, thank-you guys. Great resource. Link to comment https://forums.phpfreaks.com/topic/16783-form-to-post-current-date-help/#findComment-70630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.