Jump to content

Archived

This topic is now archived and is closed to further replies.

Mutley

Form to post current date help

Recommended Posts

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]<?php
if(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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Still can't get it to post, it doesn't go into the database:

[code]<?php
if(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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.