Getting this error... (MySQLiPluggin is my database class name)
Catchable fatal error: Object of class MySQLiPluggin could not be converted to string
I have a php form, that posts the data, and trying to add to the database.
if(isset($_POST["submit"])){
$formNewTreatment->setStickyData($_POST);
$formNewTreatment->checkNotEmpty("treatName");
$formNewTreatment->checkNotEmpty("treatPrice");
$formNewTreatment->checkNotEmpty("treatBlurb");
if ($form->valid){
$addTreatment = new Treatment();
$addTreatment->setTreatName($_POST["treatName"]);
$addTreatment->setTreatPrice($_POST["treatPrice"]);
$addTreatment->setTreatBlurb($_POST["treatBlurb"]);
$addTreatment->setSubID($_POST["treatCategory"]);
$addTreatment->addTreatments();
$sAdminMessage = "saved SMILEYFACE";
}else{
$sAdminMessage = "not saved SADFACE";
}
}
When I click submit, I get the the above error.
Here is my $addTreatment:
public function addTreatments(){
global $database;
if($this->bExisting == false){
$sQuery = "INSERT INTO treatments (`treatmentName`, `treatmentPrice`, `treatmentBlurb`, `subID`)
VALUES ('".$database->escape_value($this->sName)."', '".$database->escape_value($this->sPrice)."', '".$database->escape_value($this->sBlurb)."', '".$database->escape_value($this->iSubID)."')";
$resultAddTreatment = $database->query($sQuery);
if($resultAddTreatment){
$this->$iTreatmentID = $this->$database->get_last_insert_id();
$this->bExsisting = true;
}else{
die("save has failed, you've done something wrong.");
}
}
}
Thanks. :)
Well, not sure if it's the bug that is causing the problem, but this looks like a bug to me:
$this->$database->get_last_insert_id();
Should probably be:
$database->get_last_insert_id();
Related
guys i'm trying to get a login script to work in openshift. nd i get the error
PHP Fatal error: Call to a member function prepare() on a non-object
i was struggling to make it work from yesterday still no luck with it. i would be happy if i get any solutions here.
here is the method from the class that is trying to login to the page.
public function viewProtectedArea($unamel,$passl)
{
try{
$active=1;
$stmth= $this->_db->prepare("select * from user where uname=:uname and pass=:pass and activated=:one");
$stmth->bindValue(":uname",$unamel);
$stmth->bindValue(":pass",$passl);
$stmth->bindValue(":one",$active);
$stmth->execute();
return $stmth->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e){
echo $e->getMessage();
}
}
and here is the login backend script:
<?php
include_once 'dbconfig.inc.php';
if (isset($_POST['submit-login'])) {
$uname= htmlspecialchars($_POST['unamel']);
$unamel= stripslashes($_POST['unamel']);
$pass= htmlspecialchars($_POST['passl']);
$pass1= stripslashes($_POST['passl']);
$passl= md5($pass1);
$user = $project->viewProtectedArea($unamel,$passl);
if ($user!="") {
$_SESSION['id']=$user['user_id'];
$_SESSION['fname']=$user['fname'];
$_SESSION['lname']=$user['lname'];
$_SESSION['uname']=$user['uname'];
$_SESSION['email']=$user['email'];
$_SESSION['phone']=$user['phone'];
$_SESSION['avatar']=$user['avatar'];
$_SESSION['app']=TRUE;
$user_ok=TRUE;
header("location: ../home.php?u={$_SESSION['uname']}");
} else {
header("location: ../index.php?nosession");
}
}
"plus dont mind the md5 i'm just experimenting"
You are getting an exception because there is no instance of $this->_db (therefor is null).
Doing a var_dump($this->_db) may tell you if that is the case.
I am currently working on Yii. I want to check that if some value is exists into database then echo something otherwise save into database.
I am doing:
$model = Users::model()->findByAttributes(array('googleid'=>$google_id));
if($model)
{
echo "Good";
}
else
{
echo $model->googleid = $google_id;
$model->save();
}
But when I am running this code then I am getting error :
Fatal error: Call to undefined method stdClass::save() in E:\wamp\www\customers\protected\views\users\googlelogin.php on line 76
What may be the reason for this error, I am unable to figure out, please help me
Thanks in advance
I got the solution, I was making a mistake that $model was returning a NULL value and I was insterting the value in that model, the following solution made my work :
$model = Users::model()->findByAttributes(array('googleid'=>$google_id));
if($model)
{
echo "Good";
}
else
{
$model_new = new Users;
echo $model_new->googleid = $google_id;
$model_new->save();
}
Thanks for the responses
hey guys was hoping you could help me out..
just to let u know in advance, im a relatively new php coder, doing a practice project, and came across this problem and ive spent like an hour of rechecking and googling but just cant figure out whats causing it
error: Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in C:\wamp\www\forum\classes\ClassUser.php on line 7
the segment of the code causing the problem:
include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
Class User{
__construct($u,$p){ //this is line 7
$user=$u;
if(strlen($p)>30|| empty($p) || !preg_match('/[^a-zA-Z0-9]/i',$p)){
$password=0;
}
else{
$password=hash_hmac('md5',$p,KEY);
}
}
oh and since im new to php, incase im doing something which i should not be, please to recommend.. thanks in advance.
note:ive removed the php tags since they seemed to be messing with the formatting of this post :/
note2: im also getting another notice
Notice: Use of undefined constant KEY - assumed 'KEY' in C:\wamp\www\forum\classes\general.inc on line 20
but im assuming thats more of a warning than an error... but just adding incase it has something to do with the error
general.inc:
//error definations
define("ERROR_FIELD_EMPTY","Error! All required fields not filled");
define("ERROR_INVALID_SIGNIN","Error! Username/password do not match!");
define("ERROR_GENERAL_INPUT", "Error! Invalid input given");
define("ERROR_SQL_CONNECT","Error! Could not connect to sql database");
//field sizes
define("PASSWORD_LENGTH",12);
define("USERNAME_LENGTH",30);
//sql server details
define("SQL_SERVER_NAME","localhost");
define("SQL_SERVER_USERNAME","root");
define("SQL_SERVER_PASSWORD","");
define("SQL_SERVER_DATABASE","forums");
define(KEY,"key");
function __autoload($className){
require_once($_SERVER["DOCUMENT_ROOT"]."forum/classes/Class$className.php");
}
ClassUser.php
include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
Class User{
__construct($u,$p){
$user=$u;
if(strlen($p)>30|| empty($p) || !preg_match('/[^a-zA-Z0-9]/i',$p)){
$password=0;
}
else{
$password=hash_hmac('md5',$p,KEY);
}
}
public function validate(){
if(strlen($user)>30|| empty($user) || preg_match('/[^a-zA-Z0-9]/i',$password==0 )){
throw new Exception(ERROR_GENERAL_INPUT);
}
$user=mysql_real_escape_string($user);
return true;
}
public function insert(){
// this->validate();
$conn= mysqli_connect(SQL_SERVER_NAME,SQL_SERVER_USERNAME,SQL_SERVER_PASSWORD,SQL_SERVER_DATABASE);
if(empty($conn)){
throw new Exception(ERROR_SQL_CONNECT);
}
$query="INSERT into USERS VALUES ($user,$password)";
$conn->query($query);
}
private $user;
private $password;
};
NewUser.php
include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
try{
$user = new User($_POST['UserName'],$_POST['Password']);
$user->insert();
}
catch(Exception $Error){
echo $Error->getMessage();
}
Your __construct needs to have the word function in front of it, or better yet public function, in the same way as the other methods in the class (ie validate and insert in your case).
ie you need the following:
public function __construct($u,$p){ //this is line 7
Change the line to:
public function __construct($u, $p) {
session.php
include("database.php");
function addPOTW($subweek, $subtitle, $subcaption, $subsubmittedby)
{
global $database, $form;
/* Errors exist, have user correct them */
if ($form->num_errors > 0) {
return 1; // Errors with form
}
/* No errors, add the new POTW to the database */
else {
if ($database->addNewPOTW($subweek, $subtitle, $subcaption, $subsubmittedby, $subfile)) {
return 0; //Event signup added succesfully
} else {
return 2; //Event signup attempt failed
}
}
}
This is my function, "addPOTW", located in the file session.php (with useless parts redacted). For some reason, I keep getting the error message: "Fatal error: Call to undefined method MySQLDB::addNewPOTW()" even though it's defined right here:
database.php
class MYSQLDB {
function addNewPOTW($date, $title, $caption, $submitter, $filepath)
{
$q = "INSERT INTO `" . TBL_POTW . "` VALUES ('','$date','$title','$caption','$submitter','$filepath')";
return mysql_query($q, $this->connection);
}
}
I have other functions in session.php accessing functions in database.php using the $database variable in the exact same way, and they work perfectly fine. Any ideas why only this one function is being reported as undefined??
Because you are referring to $this, you would need to instantiate an object from that class and then call the method.
Something like this should get it working...
$database = new MYSQLDB;
Make sure you have it in scope before your addPOTW() function.
I have this function:
public static function getOrdini($sort_order = 4)
{
$con = Propel::getConnection();
$sql = "select * from shop_orders LEFT JOIN shop_orders_total
ON
shop_orders.orders_id = shop_orders_total.orders_id
AND
shop_orders_total.sort_order = :sort_order";
$stmt = $con->prepare($sql);
$result = $stmt->execute(array(':sort_order' => $sort_order));
$ordini = self::populateObjects($stmt);
return $ordini;
}
When I call it I get this error:
( ! ) Catchable fatal error: Object of
class Criteria could not be converted
to string in
/home/javier/Aptana_Studio_Workspace/dev_repo/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel/util/DebugPDOStatement.php
on line 99
but if write the function in this way below I don't get any error:
public static function getOrdini()
{
$sort_order = 4;
$con = Propel::getConnection();
...
Any idea?
Regards
Javi
No bug in the above code it is okay. I tried passing value in the static method and it is fine the error is being generated from the other part of your code checking in the class Criteria would help you here is nothing in the posted.
Ask to propel, symfony. here someone is facing the same problem
http://symfonyexperts.com/question/show/id/51