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");
	}
?>



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

你可能感兴趣的:(php基础教程--数据库连接及登录注册)