码迷,mamicode.com
首页 > 数据库 > 详细

php.2 php连接数据库实现增查

时间:2015-08-21 00:29:15      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:连接数据库   php   

<html>

<head>

<?php

$message_name=$message_email=$message_pwd="";

$m_name=$m_email=$m_pwd=true;

$name=$email=$pwd="";

$nameexist=true;

if($_SERVER["REQUEST_METHOD"]=="POST")

{

if(empty($_POST["name"])){

$message_name="姓名是必填的";

$m_name=false;

}else{

$name=text_input($_POST["name"]);

if(!preg_match("/^[0-9a-zA-Z]*$/",$name))

{

$message_name="姓名只能是数字和字母";

$m_name=false;

}

}

if(empty($_POST["pwd"])){

$message_pwd="密码是必填的";

$m_pwd=false;

}else{

$pwd=text_input($_POST["pwd"]);

if(!preg_match("/^[0-9a-zA-Z]*$/",$pwd))

{

$message_pwd="密码只能是数字和字母";

$m_pwd=false;

}

}

if(empty($_POST["name"])){

$message_email="电子邮箱是必填的";

$m_email=false;

}else{

$email=text_input($_POST["email"]);

if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))

{

$message_email="电子邮箱格式不正确";

$m_email=false;

}

}

}


function text_input($data){

$data=trim($data);

$data=stripcslashes($data);

$data=htmlspecialchars($data);

return $data;

}

?>

<style type="text/css">   

    span.dx

    {  

    color:#FF0000;  

    }  

    </style>

</head>

<body>

<table  align="center" width="800">

<caption> <h1> 注册 <h1><p> </caption>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<tr>

<td width="150">姓名:</td>

<td width="650">

<input type="text" name="name" value="<?php echo $name;?>">

<span class="dx">* <?php echo $message_name;?></span>

</td>

</tr>

<tr>

<td width="150">密码:</td>

<td width="650">

<input type="text" name="pwd" value="<?php echo $pwd;?>">

<span class="dx">* <?php echo $message_pwd;?></span>

</td>

</tr>

<tr>

<td width="150">电子邮箱:</td>

<td width="650"><input type="text" name="email" value="<?php echo $email;?>">

<span class="dx">* <?php echo $message_email;?></span>

</td>

</tr>

<tr>

<td width="500"><span class="dx">(注意!"*" 表示必须填写的)</span></p></td>

</tr>

<tr>

<td>

<input type="submit" name="submit" value="提交">

</td>

</tr>


<?php

if(isset($_POST["submit"])&&$m_name&&$m_email&&$m_pwd){

$con = mysql_connect("localhost","root","root");

if (!$con)

  {

  die(‘Could not connect: ‘ . mysql_error());

  }


mysql_select_db("login", $con);


$result = mysql_query("SELECT * FROM `login`.`register` WHERE username=$name");


while($row = mysql_fetch_array($result))

  {

 echo ‘<tr><td colspan="3">‘;

 echo ‘<div align=center>用户已存在!</div>‘;

 echo ‘</td></tr>‘;

 $nameexist=false;

 break;

  }

if($nameexist)

{

$sql="INSERT INTO `login`.`register` (`username`, `password`, `email`) VALUES (‘$name‘,‘$pwd‘,‘$email‘)";


if (!mysql_query($sql,$con))

  {

  die(‘Error: ‘ . mysql_error());

  }

  echo ‘<tr><td colspan="3">‘;

  echo ‘<div align=center>恭喜你,注册成功!</div>‘;

  echo ‘</td></tr>‘;

  echo ‘<tr><td>‘;

  echo "姓名:".$name;

  echo ‘</td></tr>‘;

  echo ‘<tr><td>‘;

  echo "密码:******";

  echo ‘</td></tr>‘;

  echo ‘<tr><td>‘;

  echo "电子邮箱:".$email;

  echo ‘</td></tr>‘;

}

mysql_close($con);

}

?>

</form>

</table>

</body>

</html>


本文出自 “软件学习总结” 博客,请务必保留此出处http://bigcrab.blog.51cto.com/10626858/1686619

php.2 php连接数据库实现增查

标签:连接数据库   php   

原文地址:http://bigcrab.blog.51cto.com/10626858/1686619

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!