用户注册一般都涉及到数据库的操作,几向数据库表中插入新的用户信息。所以在此实例进行之前,我们先要创建一个数据表:

SQL创建数据表代码如下:

create table user_info (
user_id char(18),
fname char(15),
email char(35));

 

下面进行php代码的编写:

<?
$form = \"
<form action=\\\"index.php\\\" method=\\\"post\\\">
<input type=\\\"hidden\\\" name=\\\"seenform\\\" value=\\\"y\\\">
Your first name?:<br>
<input type=\\\"text\\\" name=\\\"fname\\\" value=\\\"\\\"><br>
Your email?:<br>
<input type=\\\"text\\\" name=\\\"email\\\" value=\\\"\\\"><br>
<input type=\\\"submit\\\" value=\\\"Register!\\\">
</form>
\";
if ((! isset ($seenform)) && (! isset ($userid))) :
     print $form;
elseif (isset ($seenform) && (! isset ($userid))) :
     $uniq_id = uniqid(rand());
     @mysql_pconnect(\"localhost\", \"root\", \"\") or die(\"Could not connect to MySQL server!\");
     @mysql_select_db(\"user\") or die(\"Could not select user data !\");
     $query = \"INSERT INTO user_info VALUES(\'$uniq_id\', \'$fname\', \'$email\')\";
     $result = mysql_query($query) or die(\"Could not insert user information!\");
     setcookie (\"userid\", $uniq_id, time()+2592000);

     print \"Congratulations $fname! You are now registered!.\";
elseif (isset($userid)) :
     @mysql_pconnect(\"localhost\", \"root\", \"\") or die(\"Could not connect to MySQL server!\");
     @mysql_select_db(\"user\") or die(\"Could not select user data !\");
     $query = \"SELECT * FROM user_info WHERE user_id = \'$userid\'\";
     $result = mysql_query($query) or die(\"Could not extract user information!\");

     $row = mysql_fetch_array($result);
     print \"Hi \".$row[\"fname\"].\",<br>\";
     print \"Your email address is \".$row[\"email\"];

endif;

?>
收藏 打印