Jump to content


Photo

Form to post current date help


  • Please log in to reply
4 replies to this topic

#1 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 07 August 2006 - 11:50 AM

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:

<?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
}
?>

Thanks.
~ Mutley.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 August 2006 - 12:01 PM

rather than using a hidden field, if your data type is a DATE, you can just use the NOW() function in SQL:
<?php
$sql = "INSERT INTO news (title, date, content) VALUES ('$title', NOW(), '$content')";
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 07 August 2006 - 12:31 PM

Still can't get it to post, it doesn't go into the database:

<?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
}
?>

~ Mutley.

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 07 August 2006 - 12:38 PM

I say:
<?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')";
?>
Is the best. Your error is probbly the " $sql .= "VALUES ";" you forgot to delete.

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 07 August 2006 - 12:57 PM

Worked, thank-you guys. Great resource.
~ Mutley.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users