一个非常简单的插入功能,然而.它给出了一些令人讨厌的错误......
喜欢:
Warning: mysql_query(): Access denied for user '***.'@'***.one.com' (using password: NO) in /customers/***.be/***.be/httpd.www/belastingen/classes/btw.php on line 24 Warning: mysql_query(): A link to the server could not be established in /customers/***.be/***.be/httpd.www/belastingen/classes/btw.php on line 24
Run Code Online (Sandbox Code Playgroud)
这是代码:
<?php
if(isset($_POST['submit'])){
$naam = $_POST['name'];
$email = $_POST['email'];
$kind1 = $_POST['kind1'];
$kind2 = $_POST['kind2'];
$kind3 = $_POST['kind3'];
$kind4 = $_POST['kind4'];
$kind5 = $_POST['kind5'];
$captcha = $_POST['captcha'];
if ($captcha == 2){
if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['kind1'])) {
$insert = "INSERT INTO belastingen (ouder, email, kind1, kind2, kind3, kind4, kind5) VALUES (
'".$naam."',
'".$email."',
'".$kind1."',
'".$kind2."',
'".$kind3."',
'".$kind4."',
'".$kind5."')";
if (!mysql_query($insert)) {
echo "<div class=\"feedback\">query invoeren faalt</div>";
} else {
echo "<div class=\"feedback\">Uw registratie werd goed geregistreerd</div>";
}
} else {
echo "<div class=\"feedback\">falen, niveau 2</div>";
}
} else {
echo "<div class=\"feedback\">captcha probleem</div>";
}
}
?>
Run Code Online (Sandbox Code Playgroud)
并且不用担心MySQL注入.我们说话时添加.有没有想过这个错误?是的,我确定连接数据库的数据是正确的.
更新1
这是我的inc.php文件,包含在index.php文件的顶部.
<?php
define('MYSQL_HOST', '***.be.mysql');
define('MYSQL_DB', '***');
define('MYSQL_USER', '***');
define('MYSQL_PASSW', '***');
require_once 'classes/dbconnections.php';
require_once 'classes/btw.php';
$_DB = new DBConnection(MYSQL_HOST, MYSQL_DB, MYSQL_USER, MYSQL_PASSW);
?>
Run Code Online (Sandbox Code Playgroud)
更新2
这是我的dbconnections.php文件
<?php
class DBConnection {
public $host;
public $db;
public $user;
public $password;
private $_connection;
public function __construct($host = null, $db = null, $user = null, $password = null) {
$this->host = $host;
$this->db = $db;
$this->user = $user;
$this->password = $password;
$this->connect();
}
private function connect(){
$this->_connection = mysql_connect($this->host, $this->user, $this->password);
if(!$this->_connection) {
die("An error occured---- while connecting to the database: ".mysql_errno()." - ".mysql_error());
} else{
$selected = mysql_select_db($this->db, $this->_connection);
if(!$selected) {
die("An error occured while connecting to the database: ".mysql_errno()." - ".mysql_error());
}
}
}
public function listing($sql) {
$result = mysql_query($sql, $this->_connection);
while($row=mysql_fetch_array($result)) {
$return[] = $row;
}
return $return;
}
public function select($sql) {
$result = mysql_query($sql, $this->_connection);
return mysql_fetch_array($result);
}
public function insert($sql) {
mysql_query($sql, $this->_connection);
return mysql_affected_rows($this->_connection);
}
public function delete($sql) {
mysql_query($sql, $this->_connection);
return mysql_affected_rows($this->_connection);
}
public function escape($value) {
return mysql_real_escape_string($value);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
更新3
我在更换下面建议的问题时得到的错误
Notice: Undefined variable: _DB in /customers/***/***/httpd.www/belastingen/classes/btw.php on line 13 Fatal error: Call to a member function insert() on a non-object in /customers/***/***/httpd.www/belastingen/classes/btw.php on line 13
Run Code Online (Sandbox Code Playgroud)
根据我们在你的问题评论中的讨论,尝试改变一下,以便在btw.php中需要inc.php; index.php中需要btw.php而不是inc.php; inc.php中不需要'btw.php`.从阅读php.net/manual/en/function.include.php,我认为这可能与范围有关.
编辑:
首先,您的设置创建了一个自定义数据库对象类(DBConnection),用于与数据库连接并执行查询.当您单独使用mysql_query它时,它没有用于执行查询的数据库连接标识符,因为DBConnection对象在对象方法中抽象了该功能.这就是你需要使用的原因if (!$_DB->insert($insert)).
其次,我不是百分之百,但本质上核心问题似乎与代码btw.php没有"看到"数据库设置代码有关.这可能是因为两件事.首先,$_DB变量是在btw.php需要代码之后定义的,因此,当解析PHP解释器时btw.php,$_DB尚未定义.如果需求和数据库对象定义很重要的顺序.其次,这是我有点不确定的地方,但我认为当btw.php从内部inc.php($_DB定义的地方)需要而不需要在其中进行数据库设置时,存在可变范围/访问问题btw.php.换句话说,您拥有使用定义它的脚本所需的数据库对象的代码,而不是使用它的代码中所需的数据库设置脚本(包括数据库对象声明).
我希望这是有道理的,如果它仍然令人困惑,请告诉我.我倾向于简明扼要地解释事情.