Simple PHP message code for this instance

  • 2020-03-31 20:39:35
  • OfStack

config.php
 
<?php 
$conn = @mysql_connect("localhost","root","") or die(" Database connection error !"); 
mysql_select_db("gb",$conn); 
mysql_query("set names 'GBK'"); 
?> 

Add the PHP
 
<?php 
include("config.php"); 
if($_POST['submit']){ 
//In this case, I forgot that there was another field in message, lastdate, that had not been written, resulting in an unsuccessful insert of data. It took a long time to find the mistake.
$sql="insert into message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())"; 
mysql_query($sql); 
echo " successful "; 
} 
?> 
<form action="add.php" method="post"> 
 The user :<input type="text" name="user" /><br> 
 The title :<input type="text" name="title" /><br /> 
 content :<textarea name="content"></textarea><br /> 
<input type="submit" name="submit" value=" submit " /> 
</form> 

The PHP
 
<?php 
include("config.php"); 
?> 
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef"> 
<?php 
$sql="select * from message order by id desc"; 
$query=mysql_query($sql); 
while($row=mysql_fetch_array($query)){ 
?> 
//NND. I use <in the default environment of wampserver. ? = $row [title]?> This syntax just doesn't read the content. It has to be this way. Depressed. It took a long time to figure it out
<tr bgcolor="#eff3ff"> 
<td> Title: <?php echo $row[title];?>  User: <?php echo $row[user];?></td> 
</tr> 
<tr bgColor="#ffffff"> 
<td> Content: <?php echo $row[content];?></td> 
</tr> 
<?php 
} 
?> 
</table> 

And then there's the SQL for the database.
 
CREATE TABLE `message` ( 
`id` tinyint(1) NOT NULL auto_increment, 
`user` varchar(25) NOT NULL, 
`title` varchar(50) NOT NULL, 
`content` tinytext NOT NULL, 
`lastdate` date NOT NULL, 
PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ; 

Related articles: