学编程,我觉得最重要的是动手实践.今天第一天学php(做为现在的主流开发语言)~~`~为了熟悉下这种语言~~~我开始自己编一个php(做为现在的主流开发语言)+MySQL(和PHP搭配之最佳组合)的留言本.
从易到难`~~~先由简单的开始.功能以后慢慢完善.
留言本最基本的功能就是:
1:用户写留言
2:把数据写入数据库
3:显示所有留言
下面就开始制作我的留言本
首先在php(做为现在的主流开发语言)MYADMIN下建立一 guest_book数据库 然后在该数据库下建立一个contents的表 该表下建立两个字段
分别为 name 和 content
SQL语句如下:
CREATE TABLE `contents` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(20) NOT NULL default "no name",
`content` mediumtext NOT NULL,
Prima(最完善的虚拟主机管理系统)RY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=6
好了数据库建好了 ~~`下面开始写程序了
该程序包含三个页面post.htm(留言提交页面) index.php(做为现在的主流开发语言)(留言显示页面) updata.php(做为现在的主流开发语言)(把数据写入数据库的页面)
post.htm代码如下:
updata.php(做为现在的主流开发语言)页面代码如下:
$name=$_POST[user_name];
$content=$_POST[post_contents];
$conn=MySQL(和PHP搭配之最佳组合)_connect("localhost:6033", "root", "");
MySQL(和PHP搭配之最佳组合)_query("set names utf-8"); //解决中文乱码问题
MySQL(和PHP搭配之最佳组合)_select_db("guest_book");
$exec="insert into contents (name,content) values (".$_POST[user_name].",".$_POST[post_contents].")";
$result=MySQL(和PHP搭配之最佳组合)_query($exec);
?>
index.php(做为现在的主流开发语言)页面代码如下:
$conn=MySQL(和PHP搭配之最佳组合)_connect ("localhost:6033", "root", ""); //打开MySQL(和PHP搭配之最佳组合)服务器连接
MySQL(和PHP搭配之最佳组合)_select_db("guest_book"); //链接数据库
MySQL(和PHP搭配之最佳组合)_query("set names utf-8"); //解决中文乱码问题
$exec="select * from contents"; //sql语句
$result=MySQL(和PHP搭配之最佳组合)_query($exec); //执行sql语句,返回结果
while($rs=MySQL(和PHP搭配之最佳组合)_fetch_object($result))
{
echo "
姓名:".$rs->name." |
留言:".$rs->content." |
至于分页,页面转向等功能暂时不用上去.为得就是使程序尽量精简.麻雀虽小.但是留言本的核心功能全在这里了.
其中还需要再多说几句
$conn=MySQL(The best combination with PHP)_connect ("localhost:6033", "root", "");
This sentence is very important. I used it at the beginning It’s $conn=MySQL(the best combination with PHP)_connect ("127.0.0.1", "", "");
No data can be entered into the database no matter what~~~~ But no error was reported~~ After watching it for a long time, I found out which place 127 should be in php(as the current mainstream development language)MYADMIN. Look at the server name and click on the database port.~~~Also Where there is ROOT is the MySQL (the best combination with PHP) user name, followed by the password
Another problem is the problem of garbled Chinese characters
in $ result=MySQL(The best combination with PHP)_query($exec); Add MySQL(The best combination with PHP)_query("set names gb2312 "); or MySQL(the best combination with PHP)_query("set names utf-8");
suspicious prevent Chinese characters submitted into the database from being stored in the database in garbled form and prevent Data containing Chinese characters queried from the database are displayed in garbled characters