We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12379
    • 460 Posts
    Need to bang some data into a database from an ecommerce site I’ve developed. I’ve got a snippet to process the form data.
    It needs something like the code below to write to the DB. What’s the correct way to do this in Modx within a snippet?

    $dbhost = 'localhost';
    $dbuser = 'username';
    $dbpass = 'password';
    $dbname = 'yourdatabase';
    
    
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
    mysql_select_db($dbname);
    
         $sql = "insert into TABLENAME values ('".$string.",".$today.",".$b_first.".etc."') ";
          mysql_query($sql);
          echo $sql ."<br>\n";
          if(mysql_error()) {
             echo mysql_error() ."<br>\n";
          }
    



      Mostly harmless.
      • 4041
      • 788 Posts
      try this:

      $dbhost = 'localhost';
      $dbuser = 'username';
      $dbpass = 'password';
      $dbname = 'yourdatabase';
      
      
      
      $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
      mysql_select_db($dbname);
      
      $output ="";
          $sql = "insert into TABLENAME values ('".$string.",".$today.",".$b_first.".etc."') ";
            mysql_query($sql);
            $output .=$sql ."<br>\n";
            if(mysql_error()) {
               $output .=mysql_error() ."<br>\n";
            }
      return $output;

        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 12379
        • 460 Posts
        Oh, ok, thought it was going to be more involved than that! Will test shortly. Thank you!
          Mostly harmless.
          • 7231
          • 4,205 Posts
          If it is the same db as modx you can use the dbapi db calls to simplify it.
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]