PHP Add data save to MySQL database instance tutorial

Source: Internet
Author: User
Tags mysql tutorial php tutorial strlen

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.111cn.net/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>php Tutorial Add data Save to MySQL tutorial data instance Tutorials </title>
<style type= "Text/css Tutorial" >
<!--
Body {
margin-left:0px;
margin-top:10px;
margin-right:0px;
margin-bottom:0px;
}
td{
padding:2px;
}
body,td,th {
font-size:12px;
}
-->
</style>

<body>
<table width= "98%" border= "0" align= "center" cellpadding= "0" cellspacing= "1" bgcolor= "#F2F2F2" >
<form id= "Form1" Name= "Form1" method= "Post" action= "" ><tr>
&LT;TD width= "15%" height= "align=" "Center" bgcolor= "#FFFFFF" > title:</td>
&LT;TD width= "85%" bgcolor= "#FFFFFF" >
<label>
<input type= "text" name= "title"/>
</label> </td>
</tr>
<tr>
&LT;TD height= "align=" "Center" bgcolor= "#FFFFFF" > Keyword:</td>
&LT;TD bgcolor= "#FFFFFF" ><label>
<input name= "keyword" type= "text" id= "keyword" size= "a"/>
</label></td>
</tr>
<tr>
&LT;TD height= "align=" center "bgcolor=" #FFFFFF "> Description:</td>
&LT;TD bgcolor= "#FFFFFF" ><label>
<textarea name= "desc" cols= "rows=" "4" id= "desc" ></textarea>
</label></td>
</tr>
<tr>
&LT;TD height= "align=" "Center" bgcolor= "#FFFFFF" > Introduction:</td>
&LT;TD bgcolor= "#FFFFFF" ><label>
<textarea name= "Body" cols= "rows=" 8 "></textarea>
</label></td>
</tr>
<tr>
&LT;TD height= "align=" center "bgcolor=" #FFFFFF ">&nbsp;</td>
&LT;TD bgcolor= "#FFFFFF" ><label>
<input type= "Submit" name= "Submit" value= "Save Data"/>
</label></td>
</tr>
</form>
</table>
<?php
if ($_post)
{

mysql_connect (' localhost ', ' root ', ' root ');
mysql_select_db (' fangke_cc ');
mysql_query ("Set names ' GBK '");

$title = Htmlspecialchars (isset ($_post[' title ')]? $_post[' title ']: ";
$keyword = Htmlspecialchars (isset ($_post[' keyword ')) $_post[' keyword ']: ');
$desc = Htmlspecialchars (isset ($_post[' desc ')) $_post[' desc ']: ');
$body = Htmlspecialchars (isset ($_post[' body ')), $_post[' body '];


if ($title = = "" | | strlen ($title) > 200)
{
Exit (' <script>alert ("title not valid, re-enter information"); History.back ();</script> ');
}
else if ($body = = "" | | Strlen ($body) >2048)
{
Exit (' <script>alert ("Empty too long, re-enter information"); History.back ();</script> ');
}
Else
{
$time = time ();
$body =preg_replace ("/$title/", "$sql = "INSERT into SU_ZT (title,description,keyword,body,datetimes) VALUES (' $title ', ' $desc ', ' $keyword ', ' $body ', ' $ Time ') ";
mysql_query ($sql) or Die (Mysql_error (). $sql);
Die (' <script>alert ("save Success"); location= "'. $_server[' Http_referer '"; </script> ');
}
}
?>

SU_ZT table Structure

CREATE TABLE IF not EXISTS ' Su_zt ' (
' ID ' int (8) not NULL auto_increment,
' title ' varchar character Set gb2312 default NULL,
' keyword ' varchar character set gb2312 default NULL,
' Description ' text character set gb2312,
' Body ' text character set gb2312,
' DateTimes ' int (4) Default NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8 auto_increment=2;
</body>
Site original tutorial, reprinted annotated source Http://www.111cn.net, otherwise must investigate

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.