Php+mysql simple message, suitable for beginners

Source: Internet
Author: User
Tags php script

<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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.