Support » Plugins » Hacks » How to write to a database NOT the wordpress database

  • Hey there,

    I’m working on a contact form, I need the contact form to submits the fields filled out, not to the wordpress database ($wpdb) but a different database of my own. How could I replace the $wpdb variable with my own.

    I’m sorry is this is a dumb question, I only know the basics of PHP and MySQL.

Viewing 2 replies - 1 through 2 (of 2 total)
  • If at all possible, it’s best to stay within the WordPress system when running WP, but if that’s not possible then you would just need to initialize a connection with your database and handle it as normal.

    Here is the PHP documentation for the Mysqli library, which is the new version of the depreciated Mysql library.

    Liek Dan said it is best to stay within the WP Db, however easiest way to do what you are wanting to do is to create a file called somthing like: db_connect.php (have all your connections to your DB here (ie. db_name, host, user, pass)

    Then (if using a WordPress generated page to collect your users information, you will want to use a plugin like [include it] or create a custom template page based on your theme with your contact form inside of it and make your calls normally as if it were a standard PHP file.)

    Something like this:

    <? include db_connect.php;
    // php / mysql to process collected data
    if (isset($formSubmitted))
    $name = $_POST['name']
    $othervariables = $_POST['othervariables'];
    $query = 'INSERT ..... $name, $othervariables';
    $result = mysql_query($query);
    // user contact form to collect data
    echo 'your form stuff';
    -- Note: make sure you write back to the same page by calling:
    <form action='<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>' method='POST' enctype='application/x-www-form-urlencoded'>
    form submit / clear <inputs>
    <input type='hidden' name'formSubmitted' value='Y'>

    Hope this helps.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to write to a database NOT the wordpress database’ is closed to new replies.