php.2 php串連資料庫實現增查

來源:互聯網
上載者:User

標籤:串連資料庫   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串連資料庫實現增查

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.