WordPress.org

Ready to get started?Download WordPress

Forums

How to write to a database NOT the wordpress database (3 posts)

  1. suite8
    Member
    Posted 1 year ago #

    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.

  2. Dan Rossiter
    Member
    Posted 1 year ago #

    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.

  3. billibones
    Member
    Posted 1 year ago #

    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'>
    </form>
    
    ?>

    Hope this helps.

Topic Closed

This topic has been closed to new replies.

About this Topic