php - A link to the server could not be established -
a simple insert function, , yet. gives nasty errors...
like:
warning: mysql_query(): access denied user '***.'@'***.one.com' (using password: no) in /customers/***.be/***.be/httpd.www/belastingen/classes/btw.php on line 24 warning: mysql_query(): link server not established in /customers/***.be/***.be/httpd.www/belastingen/classes/btw.php on line 24
and code:
<?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 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>"; } } ?>
and don't worry mysql-injection. adding speak. thought error? , yes i'm sure data connection database correct.
update 1 inc.php
-file, included on top of index.php file.
<?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); ?>
update 2 dbconnections.php
-file
<?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 database: ".mysql_errno()." - ".mysql_error()); } else{ $selected = mysql_select_db($this->db, $this->_connection); if(!$selected) { die("an error occured while connecting 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); } } ?>
update 3
error when replacing thins suggested below
notice: undefined variable: _db in /customers/***/***/httpd.www/belastingen/classes/btw.php on line 13 fatal error: call member function insert() on non-object in /customers/***/***/httpd.www/belastingen/classes/btw.php on line 13
as per our discussion in comments on question, try changing things inc.php required in btw.php; btw.php required in index.php rather inc.php; , 'btw.php` not required in inc.php. reading php.net/manual/en/function.include.php, think might have scope.
edit:
first off, setup have creates custom database object class (dbconnection
) used interface database , execute queries. when used mysql_query
itself, did not have database connection identifier execute query, since dbconnection object abstracts functionality in object methods. why needed use if (!$_db->insert($insert))
.
secondly, , i'm not 100% on this, core problem seems have code in btw.php
not "seeing" database setup code. have been because of 2 things. first, $_db
variable defined after btw.php
code required, , result, when php interpreter parsed btw.php
, $_db
had not yet been defined. order if requires , database object definition matter. secondly, , bit unsure, think there variable scope/access issue when requiring btw.php
within inc.php
(where $_db
defined) rather requiring database setup within btw.php
. in other words, had code uses database object required in script defines it, rather database setup script (including database object declaration) required within code uses it.
i hope makes sense, please let me know if still confusing. tend have problem explaining things concisely.
Comments
Post a Comment