我尝试编写简单的用户注册页面并使用简单的DBCONNECTOR类,如下所示,当我尝试将用户发送到数据库时,我得到mysql_query()error.this是我的PHP代码和类请帮我解决问题
这是我的dbconnector类:
<?php
require_once 'UConfig.inf.php';
class udbcl extends USettings{
var $link;
var $myquery;
function UDbconnect(){
$DBinfo=USettings::GSettings();
$DBhost=$DBinfo['UDbhost'];
$DBname=$DBinfo['UDbname'];
$DBusername=$DBinfo['UDbusername'];
$DBpassword=$DBinfo['UDbpassword'];
$this->link=mysql_connect($DBhost,$DBusername,$DBpassword);
mysql_select_db($DBname);
}
function UQuery($myquery){
$this->link=$myquery;
return mysql_query($myquery, $this->link);
}
function UFetch($result){
return mysql_fetch_array($result);
}
function UDbclose(){
mysql_close($this->link);
}
}
?>
这是我的register.php页面:
<?php
require_once ('UConfig/UDbconnector.php');
$myinfoadd = new udbcl();
if (isset($_POST['username'])) {
$myq = "INSERT INTO userinfo(username,password,email,telphon) VALUES('$_POST[username]','$_POST[password]','$_POST[email]','$_POST[telnumber]')";
$myinfoadd->UQuery($myq);
$myinfoadd->UDbclose();
echo "You registration is Success!";
sleep(3);
header('Location:index.php');
} else {
print <<<hldoc
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>U S R R</title></head><body><form method="POST" action="register.php" ><b>User Name:</b> <input type="text" name="username" /><br/><b>User Password:</b><input type="password" name="password" /><br/><b>User Email:</b><input type="email" name="email" /><br/><b>User Tel:</b><input type="telnumber" name="telnumber" /><br/><input type ="submit" name="submit" value="Register"/></form></body></html>
hldoc;
}
?>
当我尝试注册用户时,我收到的错误如下:
警告:mysql_query():提供的参数不是第18行的C:\ xampp \ htdocs \ uzu \ UConfig \ UDbconnector.php中的有效MySQL-Link资源
警告:mysql_close():提供的参数不是第25行的C:\ xampp \ htdocs \ uzu \ UConfig \ UDbconnector.php中的有效MySQL-Link资源
您注册成功!
警告:无法修改标头信息 - 已在C:\ xampp \ htdocs \ uzu \ register.php上发送的(已在C:\ xampp \ htdocs \ uzu \ UConfig \ UDbconnector.php:18处开始输出)的标头10
答案 0 :(得分:0)
function UQuery($myquery){
$this->link=$myquery;
return mysql_query($myquery, $this->link);
}
以上功能相当于:
function UQuery($myquery){
$this->link=$myquery;
return mysql_query($myquery, $myquery);
}
不要重新分配$this->link
,你应该没事。
<强>更新强>
我也注意到你从未初始化链接。您应该在类构造函数中执行此操作,或将其作为参数传递给构造函数。
class udbcl extends USettings{
var $link;
var $myquery;
public function __construct($dbLink = null) {
if (is_null($dbLink)) {
$dbLink = $this->UDbconnect();
}
$this->link = $dbLink;
}
private function UDbconnect(){
$DBinfo=USettings::GSettings();
$DBhost=$DBinfo['UDbhost'];
$DBname=$DBinfo['UDbname'];
$DBusername=$DBinfo['UDbusername'];
$DBpassword=$DBinfo['UDbpassword'];
$this->link=mysql_connect($DBhost,$DBusername,$DBpassword);
mysql_select_db($DBname);
}
.
.
.
}