Hi!
I need
UPDATE ( and in future
INSERT INTO ) one or more field in database table.
I have working "package"
(
https://forums.modx.com/thread/73790/problem-with-absolutely-simple-question-in-database-table )
How i update any selected row in my table?
//.....
if (isset($_POST['title'])) {$title = $_POST['title'];}
//.....
$path = MODX_CORE_PATH . 'components/fotogalerie/';
$result = $modx->addPackage('fotogalerie',$path . 'model/','pp_');
if($result){
$up = $modx->newObject('Galerie');
$up->set('id_category',$_GET['id']);
// $up->fromArray(array('title' => $title ));
$up->set('title',$title);
$up->save();
}
not work
Any advice ?
Thanks
first,
think it isn't a good idea to use $_GET['id'] in a snippet.
try using something else for example $_GET['object_id']
then for updating use something like that:
if($result){
if ($up = $modx->getObject('Galerie',array('id_category'=>$_GET['object_id']))){
$up->set('title',$title);
$up->save();
}
}
Bruno17: Thanks!
I try this code
...
Working!
Thanks!
:-)
$path2 = MODX_CORE_PATH . 'components/fotogalerie/';
$result2 = $modx->addPackage('fotogalerie',$path2 . 'model/','pp_');
if($result2){
if ($up = $modx->getObject('Galerie',array('id_galerie'=>$id_galerie))){
$up->set('title',$title);
$up->save();
}
}
And second question:
When I Need new Record to Database Table ?
this is the same code like this?:
if($result2){
if ($up = $modx->getObject('Galerie',array('id_galerie'=>[????num rows +1 ????]))){
$up->set('title',$title);
$up->set('field2',$field2);
$up->set('field3',$field3);
$up->set('field4',$field4);
...........
$up->save();
}
}
Thanks
if you want to create a new object, if the object is not in the db:
$path2 = MODX_CORE_PATH . 'components/fotogalerie/';
$result2 = $modx->addPackage('fotogalerie',$path2 . 'model/','pp_');
if($result2){
if ($up = $modx->getObject('Galerie',array('id_galerie'=>$id_galerie))){
$up->set('title',$title);
$up->save();
} else {
$newObject = $modx->newObject('Galerie');
$newObject->fromArray(array(
'title' => $title,
'id_galerie'=>$id_galerie
));
$newObject->save();
}
}
Thanks !
Great thank my friends!
:-)