当前位置:Gxlcms > 数据库问题 > 用示例详解php连接数据库操作

用示例详解php连接数据库操作

时间:2021-07-01 10:21:17 帮助过:41人阅读

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <h1>登录页面</h1> <!--数据提交到chuli_1.php页面,提交的方法为POST方法--> <form action="chuli_1.php" method="post"> <div>用户名:<input type="text" name="uid" /></div> <div>密码:<input type="password" name="pwd"/></div> <div><input type="submit" value="登录"/></div> </form> </body> </html>

在创建一个chuli_1.php来处理提交的数据

<?php
//变量$uid值通过POST方法取得传递过来的uid值
$uid =$_POST["uid"];
//变量$pwd值通过POST方法取得传递过来的pwd值
$pwd =$_POST["pwd"];
//造一个mysql连接对象$db
$db = new MySQLi("localhost","root","root","mydb");
//查询数据库users表,满足传递过来的$uid值和users表中的uid值相同
$sql ="select pwd from users where uid = ‘{$uid}‘";
//$result是一个变量,result是访问数据库的对象,它有一个query方法,即根据指定的$sql查询语句去执行一个查询,并将结果返回给$result
$result = $db->query($sql);
//用$a接收结果,fetch_row为一条数据
$a = $result->fetch_row();
//判断通过POST传递的$pwd值非空(避免sql注入攻击)并且和users表的uid对应的pwd值相同
if(!empty($pwd) && $a[0]==$pwd)
{
    //header("location:main_1.php"); // header() 函数向客户端发送原始的 HTTP 报头
    //跳转到这个地址:main_1.php,即返回之前页面
    echo"<script>window.location = ‘main_1.php‘;</script>";
}
else
{
    echo"用户名或密码错误!";
}


?>

 

用示例详解php连接数据库操作

标签:from   技术分享   数据   传递   cat   xhtml 1.0   避免   UI   执行   

人气教程排行