>백엔드 개발 >PHP 튜토리얼 >PHP MYSQL实现登陆和模糊查询两大功能_PHP

PHP MYSQL实现登陆和模糊查询两大功能_PHP

WBOY
WBOY원래의
2016-05-27 10:37:291097검색

本文使用的软件版本如下:PHP版本 5.5.12;MYSQL版本 5.6.17;Apache 2.4.9 用的wampserver
一、PHPMYSQL实现登陆
一共含有两个文件:login.phplogincheck.php;
表单代码:

<form action="logincheck.php"method="post">

     Yonghu:<inputtype="text" name="username" />

  <br />

  Mima:<input type="password" name="password" />

  <br />

  <input type="submit" name="submit" value="登陆" ahref="logincheck.php" />

      

  <a href="register.php">zhuce:</a>
</form><br />

后台处理代码:

<&#63;php

     if(isset($_POST["submit"])&& $_POST["submit"] == "登陆")

     {

          $user= $_POST["username"];

          $psw= $_POST["password"];

          if($user== "" || $psw == "")

          {

              echo"<script>alert('请输入用户名或密码!'); history.go(-1);</script>";

          }

          else

          {

              $link= mysqli_connect('localhost', 'sa', '123456','account');//链接数据库

              mysqli_select_db($link,"account");

              mysqli_query($link,'setname utf8');

              $sql= "selectuser from zhanghu where user = '$_POST[username]'";

              $result=mysqli_query($link,$sql)or die("Failed".mysql_error());

              if($num=mysqli_num_rows($result))

              {

                   $row= mysqli_fetch_array($result);    //将数据以索引方式储存在数组中

                   echo"welcome ";

                   echo$row[0];

              }

              else

              {

                   echo"<script>alert('用户名或密码不正确!');history.go(-1);</script>";

              }

          }

     }

     else

     {

          echo"<script>alert('Submit Failed!');history.go(-1);</script>";

     }

 

&#63;>

Account数据库内容:

 

二、PHPMYSQL实现模糊查询

查询数据库(只给PHP的代码):

<&#63;php

     $mysqli=newmysqli();             

     $mysqli->connect("localhost","sa", "123456");

     if(mysqli_connect_errno()) {

          printf("Failllllll:%s\n", mysqli_connect_error());

          exit();

     }

     $mysqli->select_db("booklib");  

     $mysqli->query("SETNAMES utf8");   

     $rsbooks= $mysqli->query("select * from books where Name like'%$_POST[bookname]%'");

  $row_rsbooks = $rsbooks->fetch_assoc();

  $totalRows_rsbooks = $rsbooks->num_rows;    

&#63;> Totel

<&#63;php echo $totalRows_rsbooks &#63;>books;

<table width="600"border="1">

 <tr>

  <td bgcolor="#99CCFF"align="center">Name</td>

  <td bgcolor="#99CCFF" align="center">ISBN</td>

  <td bgcolor="#99CCFF"align="center">Store</td>

  <td bgcolor="#99CCFF"align="center">Do</td>

 </tr>

 <&#63;php do { &#63;>

  <tr>

   <td><&#63;php echo $row_rsbooks['Name']; &#63;></td>

   <td><&#63;php echo $row_rsbooks['ISBN']; &#63;></td>

   <td><&#63;php echo $row_rsbooks['Store']; &#63;></td>

   <td><fontcolor="#110BAA">rent</font></td>

  </tr>

  <&#63;php } while ($row_rsbooks = $rsbooks->fetch_assoc()); &#63;>

</table>

<&#63;php

 $rsbooks->close(); 

 $mysqli->close();

&#63;>

数据库表的内容如下:

查询:

结果:

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.