We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24075
    • 83 Posts
    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
      Petr Püschel, Czech Republic
      <hr>
      http://puschpull.org
      <hr>
      • 4172
      • 5,888 Posts
      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(); 
        }
      }
      
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 24075
        • 83 Posts
        Bruno17: Thanks!
        I try this code
        ...
          Petr Püschel, Czech Republic
          <hr>
          http://puschpull.org
          <hr>
          • 24075
          • 83 Posts
          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();
            }
          }
            Petr Püschel, Czech Republic
            <hr>
            http://puschpull.org
            <hr>
            • 24075
            • 83 Posts
            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
              Petr Püschel, Czech Republic
              <hr>
              http://puschpull.org
              <hr>
              • 33968
              • 863 Posts
              Like this:
              $newObject = $modx->newObject('Galerie');
              $newObject->fromArray(array(
                  'title' => $title,
                  'field1' => $field1,
                  'field2' => $field2,
              ));
              $newObject->save();
              
                • 4172
                • 5,888 Posts
                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();
                  } 
                }
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 24075
                  • 83 Posts
                  Thanks !
                  Great thank my friends!
                  :-)
                    Petr Püschel, Czech Republic
                    <hr>
                    http://puschpull.org
                    <hr>