How to Insert a Date in MySQL - php Form

Author Topic: How to Insert a Date in MySQL  (Read 2226 times)

Offline alex

  • Global Moderator
  • *****
  • Posts: 77
  • Karma: +19/-0
    • View Profile
How to Insert a Date in MySQL
« on: October 09, 2009, 02:18:10 PM »


config.php
Code: [Select]
<?php 
$dbhost 
'localhost';
$dbusername 'root';
$dbpassword 'mydbpass';
$dbname 'test';
$conn=mysql_connect($dbhost$dbusername$dbpassword);
if(!
$conn) :
   die(
'Could not connect: ' mysql_error());
endif;
$db=mysql_select_db($dbname$conn);
if(!
$db) :
   die (
'Can\'t connect to database : ' mysql_error());
endif;
?>


install.php
Code: [Select]
<?php
include('config.php');
if(isset(
$_POST['Submit']))
{
mysql_query("CREATE TABLE `page` (
  `id` int(11) NOT NULL auto_increment,
  `text` text collate utf8_unicode_ci,
  `date` datetime,
    PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1"
)
or die(
mysql_error());
echo 
" Installation successful! ";
}else{
?>

<form method="post" action="install.php">
<br><center><input type="Submit" name="Submit" value="Install"><center>
</form>
<?php
}
?>


insert.php
Code: [Select]
<title>Insert Page</title>
<center><h2>INSERT PAGE</h2></center><BR>
<?php
include('config.php');
if(isset(
$_POST['submit']))
{
$text $_POST['text'];
mysql_query("INSERT INTO page (text,date)
VALUES ('$text',NOW())"
)
or die(
mysql_error()); 
echo 
"<a href=\"index.php\">Index Page</a>";
}else{
?>

<form method="post" action="insert.php">
<TABLE><TR><TD>
Title:
</TD>
<TD>
<input name="text" size="60" maxlength="255">
</TD></TR>
<TR><TD>
<input type="submit" name="submit" value="submit">
</TD></TR></TABLE>
</form>
<?php
}
?>


index.php
Code: [Select]
<title>Index Page</title>
<center><h2>INDEX</h2></center><BR>
<?php
include('config.php');
echo 
"&nbsp;<a href=\"insert.php\"><font face=\"tahoma\">INSERT PHP</a><br>";

$result mysql_query("SELECT * FROM page"
or die(
mysql_error()); 
while(
$link=mysql_fetch_array($result)){
echo 
"&nbsp;<a href=\"page.php?id=$link[id]\">$link[text]</a><br>";
echo 
"Text: $link[text] - Added:$link[date]<br>";
}
?>


page.php
Code: [Select]
<?php
include('config.php');
$gid $_GET['id'];
$result mysql_query("SELECT id, text, date FROM page WHERE id='$gid' ")
//OR $result = mysql_query("SELECT * FROM page WHERE id='$gid' ")
or die(mysql_error()); 
while(
$myrow mysql_fetch_assoc($result))
             {
echo 
"<title>$myrow[text]</title>";
echo 
"<center><h2>Text: $myrow[text] - Added:$myrow[date]</center><BR>";

}
?>