学习记录:
php连接数据库并执行sql语句查询:通过html表单提交数据给php
全部文件:
index.php文件代码:
<?PHP
$date_db='test1';
$conn=mysqli_connect("localhost","root","root");//连接字符串
$comm=mysqli_select_db($conn,$date_db);//利用连接字符串选择数据库
$username = addslashes($_POST['username']);//得到数据
$password = addslashes($_POST['password']);//得到数据
$sql_data="select * from users where userName='$username' and password='$password'";//sql查询语句赋值给变量
$query= mysqli_query($conn,$sql_data);//利用连接字符串运行sql语句
if($conn){
echo"--连接MySQL成功--";
}
else{
echo"--连接MySQL失败--";
}
if($comm){
echo'--连接数据库:';
echo $date_db;
echo'成功--';
}
if( mysqli_num_rows($query)>0){
echo"<br>";
echo $sql_data;
echo'----查询成功--';
}
else{
$url='index.html';
echo "<script>location.href='$url'</script>";//跳转指定网页
echo"<br>";
echo $sql_data;
echo'----语句查询失败--';
}
?>
index.html文件代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body>
<form action="index.php" method="post">
用户名:<br>
<input type="text" name="username" value="ukers">
<br>
密码:<br>
<input type="text" name="password" value="123456">
<br><br>
<input type="submit" value="提交">
</form>
</body>
</html>