这个系列的教程由表单开始写,因为表单可以把数据库和web 之间的交互表现得很明显。提交表单 ,数据库记录注册信息。
本教程属于基础教程。大神请略过。 对于php和mysql之间的稳固性很受程序员的喜爱。虽说最近出现了hack语言准备替代php语言,但是这个语言本身也是基于php的,后面我们可以对hack语言进行一定得学习。 好像博客园有好多大牛,不知道会不会被喷,好怕 .......。当然,我是借着自己学习的这股劲,把学习的过程也给记录下来。教程涉及 html 表单 简单的php 和mysql .先用最简单的表单,意思一下之间的交互就行了。 记得当我们什么都不懂的时候,代码也不清楚的时候,我们在想着奇幻的代码世界,感觉无比的奇妙,当我们步入代码世界的时候,我们又感到茫然了。代码是什么。此处从html说起。当然,要是html都不懂, 也不会到博客园来的。随便说一下 就是浏览器中右击,查看源代码的那个代码就是html了。但是它只是网页的结构了,要想形成一个漂亮的网页当然要很多东西了 ,其他的有机会再说,这次说网页之间的交互了。也就是动态网站的开发。 要是表单都不知道的话,搜索一下。就是那个注册的时候,那些框框了。 下面是表单代码。<span><span><</span><span>form </span><span>action</span><span>='index.php' </span><span>method</span><span>='post'</span><span>> //这里的index.php 就是一个动态传送的文件。后面会说。</span> <span><</span><span>p</span><span>></span>please type your first name:<span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="first_name"</span><span> value</span><span>=""</span><span>></</span><span>p</span><span>></span> <span><</span><span>p</span><span>></span>please type your second name:<span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="second_name"</span><span> value</span><span>=""</span> <span>></</span><span>p</span><span>></span> <span><</span><span>p</span><span>></span>please type your age:<span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="age"</span><span> value</span><span>=""</span><span>></</span><span>p</span><span>></span> <span><</span><span>p</span><span>><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>='submit' </span><span>value</span><span>='register'</span><span>></</span><span>p</span><span>></span> <span></</span><span>form</span><span>></span></span>
<span><span><</span><span>p</span><span>></span>please type your first name:<span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="first_name"</span><span> value</span><span>="<? if(isset($_REQUEST['first_name'])) echo $_REQUEST['first_name'];?>"</span><span>></</span><span>p</span><span>></span> <span><</span><span>p</span><span>></span>please type your second name:<span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="second_name"</span><span> value</span><span>="<? if(isset($_REQUEST['first_name'])) echo $_REQUEST['second_name'];?>"</span> <span>></</span><span>p</span><span>></span> <span><</span><span>p</span><span>></span>please type your age:<span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="age"</span><span> value</span><span>="<? if(isset($_REQUEST['age'])) echo $_REQUEST['age'];?>"</span><span>></</span><span>p</span><span>></span> <span><</span><span>p</span><span>><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>='submit' </span><span>value</span><span>='register'</span><span>></</span><span>p</span><span>></span></span>
<span><span>$error</span>=<span>array</span><span>(); </span><span>if</span> (<span>empty</span>(<span>$_REQUEST</span>['first_name'<span>])) { </span><span>echo</span> '<p>you forget type your first_name;<p>' ;<span>#</span><span> code...</span> <span>$error</span>='1'<span>; } </span><span>else</span><span>{ </span><span>$f</span>=<span>$_REQUEST</span>['first_name'<span>]; } </span><span>if</span> (<span>empty</span>(<span>$_REQUEST</span>['second_name'<span>])) { </span><span>echo</span> '<p>you forget type your second_name;<p>' ;<span>#</span><span> code...</span> <span>$error</span>='2'<span>; } </span><span>else</span><span>{ </span><span>$s</span>=<span>$_REQUEST</span>['second_name'<span>]; } </span><span>if</span> (<span>empty</span>(<span>$_REQUEST</span>['age'<span>])) { </span><span>echo</span> '<p>you forget type your age;<p>'; <span>#</span><span> code...</span> <span>$error</span>='3'<span>; } </span><span>else</span><span>{ </span><span>$a</span>=<span>$_REQUEST</span>['age'<span>]; }</span></span>
<span><span>if</span><span> (empty($error)) { echo</span><span>'</span><span><p> everything is ok<p></span><span>'</span><span>; require(</span><span>'</span><span>connect/mysqli_connect.php</span><span>'</span><span>); $q</span><span>=</span>"<span>insert</span> <span>into</span> <span>user</span> (first_name,second_name,age) value(<span>'</span><span>$f</span><span>'</span>,<span>'</span><span>$s</span><span>'</span>,<span>'</span><span>$a</span><span>'</span><span>)"; $r</span><span>=</span><span>@mysqli_query</span><span>($dbc,$q); }</span></span>
那个require( )就是需要连接数据库的文件。数据库的地址啊,表名啊,用户名啊,密码啊 ,靠这些联系数据库。 这个文件就换一个地方去了.直接调用它过来 连接就行了。就是require了。 这个就是 mysqli_connect.php了.
<span><? <span>$dbc</span>=@<span>mysqli_connect</span>(localhost,root,123,<span>test); </span>?></span>
<span><span>create</span> <span>table</span> <span>user</span><span>( first_name </span><span>varchar</span>(<span>20</span>) <span>not</span> <span>null</span><span>, second_name </span><span>varchar</span>(<span>20</span>) <span>not</span> <span>null</span><span>, age </span><span>varchar</span>(<span>20</span>) <span>not</span> <span>null</span><span> );</span></span>
<span> $q<span>=</span>"<span>insert</span> <span>into</span> <span>user</span> (first_name,second_name,age) value(<span>'</span><span>$f</span><span>'</span>,<span>'</span><span>$s</span><span>'</span>,<span>'</span><span>$a</span><span>'</span>)";</span>
<span>$r<span>=</span><span>@mysqli_query</span>($dbc,$q);</span>
this is a test
'; $error=array(); if (empty($_REQUEST['first_name'])) { echo 'you forget type your first_name;
' ;# code... $error='1'; } else{ $f=$_REQUEST['first_name']; } if (empty($_REQUEST['second_name'])) { echo '
you forget type your second_name;
' ;# code... $error='2'; } else{ $s=$_REQUEST['second_name']; } if (empty($_REQUEST['age'])) { echo '
you forget type your age;
'; # code... $error='3'; } else{ $a=$_REQUEST['age']; } if (empty($error)) { echo'
everything is ok
';# code... require('connect/mysqli_connect.php'); $q="insert into user (first_name,second_name,age) value('$f','$s','$a')"; $r=@mysqli_query($dbc,$q); } ?>
View Code同目录下connect 下的mysqli_connect.php 文件。
里面就是
<span><? <span>$dbc</span>=@<span>mysqli_connect</span>(localhost,root,123,<span>test); </span>?></span> View Code应该还有一个在同目录下的includes文件夹下的 header.html 文件。这个就随便了。
恩,这样交互 就形成了。还有很多东西 没加上,今天简单点。就这样。还有错误报告之类的东西。表单以后在逐渐完善全部功能。 恩 ,不错. 后面接着还有好多事情要干呢。恩 ,后面再说吧。 参考资料: larry Ullman 《PHP and MySQL for Dynamic Web Sites visual quickPro Guide foruth Edition》 《php 与mysql动态网站开发》
你试一下,用户名和密码都是admin,你那那个login.php的代码传上来让我帮你找找?