php基础教程--数据库连接及登录注册

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Login Page</title>
	<link rel="shortcut icon" href="/favicon.ico">
	<link rel="stylesheet" type="text/css" href="js/style.css" />
</head>

<body>
	<form id="login-form" action="b.php" method="post">
		<fieldset>		
			<legend>Log in</legend>			
			<label for="login">username</label>
			<input type="text" id="login" name="username"/>
			<div class="clear"></div>		
			<label for="password">passwords</label>
			<input type="password" id="password" name="password"/>
			<div class="clear"></div>
			<br />
			<br><br>
			<input type="submit" style="margin: -20px 0 0 287px;" class="button" name="commit" value="submit"/>	
		</fieldset>
	</form>	
<br /><br />
<br />
<br />
<br />
<br />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Login Page</title>
	<link rel="shortcut icon" href="/favicon.ico">
	<link rel="stylesheet" type="text/css" href="js/style.css" />
</head>

<body>
	<?php 
	$username = $_POST['username'];
	$passwords = $_POST['password'];
	if(is_null($username)||is_null($passwords)){
		echo "请确定用户名和密码都已填写";
	}else {
		$sql="select 'name','password' from userlist where name='".$username."'and password = '".$passwords."'";
	}
	class DataBase{
		public $dbhost = "localhost";
		public $dbuser = "root";
		public $dbpass = "123456";
		public $dbname = "test";
		function conn(){
			$dbconn = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die("database error!".mysql_error());
			mysql_select_db($this->dbname) or die("can not connect database:".mysql_error());
			return $dbconn;
		}
		function indb($in_sql){
			$result_indb = mysql_query($insql) or die("can not run the sql language$insql:".mysql_error());
			return $result_indb;
		}
	}
	$db = new DataBase();
	$db->conn();
	echo $db->indb($sql);
	?>
<br /><br />
<br />
<br />
<br />
<br />
</body>
</html>

此段程序运行会有错误,而下面的则正确,为什么呢?下课后仔细想想,会知道的。

<?php
	ob_start();
	$username = $_POST['username'];
	$passwords = $_POST['password'];
	if(is_null($username)||is_null($passwords)){
		echo "请确定用户名和密码都已填写";
	}else {
		$sql="select 'name','password' from userlist where username = '".$username."' and passwords = '".$passwords."';";
	}
	class DataBase{
		public $dbhost = "localhost";
		public $dbuser = "zhanqingchun";
		public $dbpass = "12345679";
		public $dbname = "zhanqingchun";
		function conn(){
			$dbconn = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die("database error!".mysql_error());
			mysql_select_db($this->dbname) or die("can not connect database:".mysql_error());
			return $dbconn;
		}
		function indb($in_sql){
			$result_indb = mysql_query($in_sql) or die("can not run the sql language:".mysql_error());
			return $result_indb;
		}
	}
	$db = new DataBase();
	$db->conn();
	$k= $db->indb($sql);
	if(mysql_fetch_row($k)){
	      header("location:http://www.baidu.com"); 
          exit;
	}else{
	      header("location:music/musicbox.php");
	}
?>



好了,同学们,这个可以作为一个模板,好好学习,程序员最重要的是要独立自主,自学能力一定要强。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值