<HTML><Head><title>PHP Message Board</title><style>P,textarea{vertical-align:Top;}</style></Head><Body><formAction= "submit.php"Method= "POST"><P>Name:<inputtype= "text"name= "username" /></P><P>Message:<textareacols= "+"rows= "5"name= "comment" /></textarea></P><inputtype= "Submit"value= "Submit"/></form><!--Front-end presentation Code -<?php [email protected]_connect (' localhost ', ' root ', '); mysql_query (' Set names UTF8 '); mysql_select_db (' Guestbook ') , $con); $sql = ' select * from comment '; $res =mysql_query ($sql); $array =array (); while (email Protected]_fetch_array ($res )){?> <b><?php echo $row [' user ']?></b>said:<P><?php echo $row [' comment ']?></P><?php}?></Body></HTML>
Here is the PHP script
<?PHP$user=$_post[' username '];$comment=$_post[' Comment '];Print_r($_post);$con[Email protected]mysql_connect(' localhost ', ' root ', ');mysql_query(' Set names UTF8 ');if(mysql_select_db(' Guestbook ',$con)){ $sql= "INSERT into comment (user,comment) VALUES ('$user‘,‘$comment‘)"; if(mysql_query($sql)){ Echo"Data insertion succeeded"; Header("location:/index.php"); }Else{ Echo"Write Failed"; }}?>
Structure of the data table
Code Essentials
1.textarea can be set size, rows represents the number of lines, cols represents the number of columns
the column name inserted in the 2.insert statement is not enclosed in quotation marks, and may not be recognized, but I can insert the quotation mark anyway.
$sql = "INSERT into comment (user,comment) VALUES (' $user ', ' $comment ')";
3.mysql_query return a resource type to a select,show,explain,describe statement
returns TRUE or FALSE for other statements, remembering to transform in time
4.php Jump Code header ("location:/index.php");
5. Set the MySQL character set mysql_query (' Set names UTF8 ');
6.mysql Resources taken from Select are combined with Mysql_fetch_array while traversal
While ([email Protected]_fetch_array ($res)) {.... function Body}
7.mysql often error, is some function is about to be discarded, see which error, in front with the @ symbol screen off it can be
Php+mysql simple message, suitable for beginners