Set field value for single database row - php

I'm trying to update a row on my profiles table to reset a users profile picture to the default of user.png. I have the following action in my controller:
public function deleteProfilePicture() {
$this->layout = 'ajax';
// First find the profile ID from the user ID
$profileId = $this->Profile->find('first', array(
'condition' => array('User.id' => $this->Auth->user('id')),
'fields' => array('Profile.id'),
'recursive' => -1
));
$this->Profile->id = $profileId['Profile']['id'];
$this->Profile->saveField('picture', 'user.png', false);
}
However, when I request the URL (/profile/deleteProfilePicture) I get no errors but the database row isn't updated. I have made sure the current profile ID is used by using debug($profileId).
What could be going wrong here?
Edit: The return value of saveField():
array(
'Profile' => array(
'id' => '36',
'modified' => '2013-04-05 14:16:57'
)
)

Try
$this->Profile->id = $profileId['Profile']['id'];
$this->Profile->set(array(
'picture' => 'user.png'
));
$this->Post->save();

I see no error in your code. Try seeing what query is getting executed using this
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
based on the result make changes to your query if you find something wrong.
Or try using this
$data['Profile']['id']=$profileId['Profile']['id'];
$data['Profile'['picture']='user.png';
$this->Profile->save($data);

If you set debug to 2 you could see what SQL is being executed, see if your update is actually firing.

Try this
$this->Profile->read(null, $profileId['Profile']['id']);
$this->Profile->saveField('picture', 'user.png', false);
Why are you setting the validation to false? Do you get an error if you omit that?

Related

Eloquent update all column fields

I want to update all the tables in the Article migration to a specific Boolean value which is set by the user.
I have written this code:
public function changeComVote() {
$data = request()->validate([
'status' => 'required'
]);
Article::query()->update(['isOnly' => $data['status']]);
event(new changesMade);
}
Although $data['status'] doesn't get passed inside the query and nothing happens, when i set it manually it works like a charm, what could be the problem?
Using $data['status'] from request will give you a string as a result, not a boolean.
Try this way
Article::query()->update(['isOnly' => $data['status'] == 'true']);
You can delete that "query()" thing and use this instead
Article::update(['isOnly' => $data['status']]);
or
Article::update(['isOnly' => ($data['status']] === "true"));
You can also specify where the row by using id
Article::find($id)->update(['isOnly' => ($data['status']] === "true"));

Data not saved in database cakephp

I have an event table with this fields:
I use this code to insert data in this table :
$oneEvent = array(
'trainer_id' => $event['trainer_id'],
'formation_id' => $event['formation_id'],
'title' => $event['title'],
'start' => $event['start'][$i],
'end' => $event['end'][$i],
);
$success = $this->Event->save($oneEvent);
$events_id= $this->Event->inserted_ids;
when I run this code I get true and ID of insert element (showed using debug)
but in database i can't see this field never !!!!.
and when I insert data in phpmyadmin when this request INSERT INTO events(title,start, end, trainer_id, formation_id) VALUES ('Départ B','2016-11-18 10:00:00','2016-11-18 11:00:00','13','1') it worked
I didn't know what happen here !!??
I solved the problem by using $dataSource->commit(); after calling save() function

Not getting the id in where clause - Laravel 5.2

I need to get the record with special id and i have this in my method :
public function addedMark()
{
$user = Auth::user();
$subject = ClassSubject::where('teacher_id', $user->id)->pluck('id','subject_id');
return view('educator.account.marks', [
'user' => $user,
'marks' => StudentMark::where('subject_id', $subject)->get()
]);
}
When i do dd(ClassSubject::where('teacher_id', $user->id)->pluck('id','subject_id')); i see that I'm getting the information that i need, but when i do dd(StudentMark::where('subject_id', $subject)->get()); it returns an empty array.
Any idea why?
Change it to (whereIn)
'marks' => StudentMark::whereIn('subject_id', $subject)->get()
and let see what hapens
In $subjectyou have id and subject_id. You might wanna just take subject_id.
So change this: StudentMark::where('subject_id', $subject)->get()
to
StudentMark::where('subject_id', $subject[1])->get()

PHP - Session array - prevent duplicate entrys

This one is really bugging me, and can not find a easy solution.
On a detail view of a product, i set the info to a session, maximum 4:
$_SESSION['recent'][] = array(
'id' => $productimgfolder,
'title' => $product['Product']['title'],
'link' => $_SERVER['REQUEST_URI'],
'image' => 'img/products/'.$productimgfolder.'/'.$product['Product']['mainpicture']
);
$_SESSION['recent'] = array_slice($_SESSION['recent'],-4);
This part works, if i output the session:
edit image => this is wat happens if i reload the detail view
The part i'am struggling with is, when i reload a detail view, the info in the session is duplicated.
How can i prevent this from happening?
I tried it with in_array & array_unique, i'am doing something wrong
The easy solution:
if the id is unique, you can do like this:
if(!array_key_exists ($productimgfolder, $_SESSION['recent']))
{
$_SESSION['recent'][$productimgfolder] = array(
'id' => $productimgfolder,
'title' => $product['Product']['title'],
'link' => $_SERVER['REQUEST_URI'],
'image' => 'img/products/'.$productimgfolder.'/'.$product['Product']['mainpicture']
);
}
$_SESSION['recent']=array_slice($arr, -4, 4, true);
other wise you have to foreach the recent array and check for id in the loop...
A different solution may be;
array_unshift($sessionArray, $singleArrayElement);
if (count($sessionArray) > 4) {
array_pop($seassionArray);
};
You need to check isset
if(!isset($_SESSION['recent']))
{
$_SESSION['recent'] = array()
}
THEN YOU CAN CHECK IF SESSION IS EMPTY
if(empty($_SESSION['recent']))
{
//here you add your data
}
because you are using array push, on page reload it adds data unless you check if session is empty. if yes you add data to your session.

Function doesn't return true even though it works

I have a small piece of code inside a function buts it's not working, even when I pull it out and try it on its own it still doesn't work. I used a MySQL Database Class https://github.com/ajillion/PHP-MySQLi-Database-Class
require_once('class.mysql.php');
$_db = new Mysqlidb('localhost', 'root', '', 'database');
$userCredentials = array(
'userID' => 'asdasda',
'username' => 'dsdasdasd',
'password' => 'v423423c342c23',
'email' => '2423v423#gmail.com',
'userType' => 1,
);
if($_db->insert('users', $userCredentials)) echo 'success';
The correct data is inserted into the database but Success is not displayed, is their a reason for this?
Mysqlidb::insert is wrong. It's return $stmt->insert_id instead of $stmt->execute();. Your query does not use autoincrement value, so, you are getting 0.
Tip from comments:
do not use this library
You got an extra coma at the end of the array you passing as a parameter, I don't think its the problem but still...
Have you tryed to see what is really returned via var_dump?

Categories