We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2249
    • 60 Posts
    Hey all, I am currently using modx to build a blog and I’m struggling with the dbapi for inserting rows. I have successfully written some ’form>ajax>php’ code that creates a row and inserts what I want.

    Can someone show me how I would put the following code into the modx dbapi?

    $user = ’root’;
    $pass = ’root’;
    $dbname = ’modx_blog’;

    mysql_connect(localhost, $user, $pass) or die(mysql_error());
    mysql_select_db($dbname) or die(mysql_error());


    $result=MYSQL_QUERY("INSERT INTO modx_comments (blog_id,name,email,website,comment)".
    "VALUES (’$blog_id’, ’$name’, ’$email’, ’$website’, ’$comment’)");

    Any help is greatly appreciated!

    Ash
      • 33992 ☆ A M B ☆
      • 455 Posts
      <?php
      $modx->db->connect("localhost", "modx_blog", $user, $pass);
      // mysql_select_db   ->   DBAPI selecs it
      $insert = array(
      	"blog_id" => $blog_id,
      	"name" => $name,
      	"email" => $email,
      	"website" => $website,
      	"comment" => $comment
      );
      $insert_id = $modx->db->insert($insert , "`modx_blog`.`modx_comments`");
      ?>

      manager/includes/extenders/dbapi.mysql.class.inc.php
        God loves me. 【ツ】


        MODX.ir (Persian Support)

        Boplo.ir/modx/ (Persian)
        • 22303 MODX Staff
        • 10,725 Posts
        Why does everyone bypass MODx for Ajax requests? You have a db connection, you have the ability to use chunks and snippets and plugins, and you don’t have to open additional potentially insecure access points (php files) into your site to implement them.
          • 2249
          • 60 Posts
          So I write my query inside a snippet i dont need to open a new connection?
            • 10487 MODX Staff
            • 1,535 Posts
            Quote from: ashfinlayson at Feb 13, 2009, 09:54 AM

            So I write my query inside a snippet i dont need to open a new connection?
            Exactly, write your ajax snippet then add it to a new published MODx page (set Show In Menu and Searchable to false and use the blank template) and then point your ajax requests to that page. That’s what I do anyway smiley
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 33992 ☆ A M B ☆
              • 455 Posts
              You can also call your snippet in current page BUT you must use exit; after printing output.
              echo $output;
              exit;

              it prints $output in a normal page with other snippets and elements and no need to have a blank page.
                God loves me. 【ツ】


                MODX.ir (Persian Support)

                Boplo.ir/modx/ (Persian)