Jump to content

Form to post current date help


Mutley

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.