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
Related
I am trying to update one of the sites I maintain to the latest PHP and during this, I have come across the following error:
Fatal error: Call to undefined function tep_session_name() in ... /includes/application_top.php on line 83
The code it is referring to is:
// set the session name and save path
tep_session_name('osCAdminID');
tep_session_save_path(SESSION_WRITE_DIRECTORY);
But I have looked at the sessions.php file are the function is defined in the below code:
function tep_session_name($name = '') {
if ($name != '') {
return session_name($name);
} else {
return session_name();
}
}
Any help in identifying the cause would be greatly appreciated.
Thanks, E.
I'm absolutely sure, that you call this function before you include file with function declaration
After updating our server to PHP 5.4 and a Drupal site to Drupal 6.37 I keep getting this error message and I am not able to get past it
Fatal error: Call to undefined function drupal_get_path() in /home/mysite/public_html/includes/module.inc on line 285
this is the related function call
function module_load_include($type, $module, $name = NULL) {
if (empty($name)) {
$name = $module;
}
$file = './'. drupal_get_path('module', $module) ."/$name.$type";
if (is_file($file)) {
require_once $file;
}
else {
return FALSE;
}
}
Even when I try to run update.php I still get the same error
I am new to Drupal so any help will be greatly appreciated
Please let me know what additional info you may need since it is my first post here and as I said I don't know much about Drupal
For some reason i cant get my model to work.. never had this problem before.
function overview($userid)
{
// Load needed model
$this->load->model('budget_model');
$data['month_budget'] = $this->budget_model->get_monthly_budget($userid);
if(isset($_POST['submit']))
{
foreach($_POST as $key => $value)
{
if(is_numeric($key))
{
$this->buget_model->update_buget($key,$value);
echo "DONE";
}
}
echo "<pre>";
print_r($_POST);
echo "</pre>";
}
$data['main'] = 'super_admin/budget_edit_overview_view';
$this->load->view('default/main_view',$data);
}
The model works fine with "$this->budget_model->get_monthly_budget($userid);" but i keep getting thir error,
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Admin::$buget_model
Filename: controllers/admin.php
Line Number: 166
Fatal error: Call to a member function update_buget() on a non-object
in /Applications/MAMP/htdocs/therace/application/controllers/admin.php
on line 166
The model method,
function update_buget($id,$budget)
{
$this->db->where('id', $id);
// Update the month budget
$data = array(
'month_goal' => $budget
);
$this->db->update('budget_month', $data);
return true;
}
Read the error message carefully:
Message: Undefined property: Admin::$buget_model
Did you make a typo and actually mean $budget_model?
edit: There seems to be a lot of budget vs. buget in your code. I suggest a spellchecker.
You have made a typing error in line 166.
It is $this->budget_model->update_buget($key,$value);
not $this->buget_model->update_buget($key,$value);
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) {
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();