0

I'm trying to work with an external db in Wordpress for the first time and can't seem to get it to insert the data into the DB. Here's what I have:

    <?php
    global $wpdb;
    $url = home_url();

        require_once($url.'/viper/confff.php');  //contains variable to fill in below
            $newdb = new wpdb($username, $password, $db_name, $host);
            $newdb->show_errors();
     ?>

$numrows = $newdb->get_results("SELECT * FROM chi_clients WHERE AccountNum='$acctNum'");

      if($numrows!=0)
      {
        $row =  $newdb->get_results("SELECT * FROM chi_clients WHERE AccountNum='$acctNum'",ARRAY_A);    
                $name= $row['lastName'] . ", " . $row['firstName'];
        $phone= $row['phone'];
        $phonearea = substr($phone,0,3);
        $phonemain = substr($phone,3,10);
        $phonenumber = $phonearea ."-". $phonemain;
        $email= $row['email'];

        $newdb ->insert (
                    chi_orders, array(
                        'name' => '$name',
                        'AccountNum' => '$acctNum',
                        'email' => '$email',
                        'OrderDesc' => '$orderDesc',
                        'product' => '$service',
                        'quantity' => '$quantity',
                        'notes' => '$notes',
                        'status' => 'Order Received',
                        'dateIne' => '$todayDate',
                        'dateOut' => '$newDate',
                        'advert' => '$advert',
                        'phone' => '$phonenumber'
                    )
                   );
        }


        else {
           echo'<span style="color: #000; font-family: Verdana; font-weight: bold; font-size: 12px;">Incorrect Account Number!</span>';
                 }

I know the data is being posted to page as I have the variables echoed out to display on the page. However it's not connecting to the Database as I'm not getting the information from the get_results SELECT statement and it's not inserting anything into database. For my $host- I'm using 'localhost' which is what I've always used for this particular database. And I know that the username, pass, and database name are correct.

any ideas? thanks.

0

You are using $wpdb incorrectly Take a look at these lines:

$row =  $newdb->get_results("SELECT * FROM chi_clients WHERE accountNum='$acctNum'",ARRAY_A);    
$name= $row['lastName'] . ", " . $row['firstName'];

$wpdb->get_results is going to return an array of "rows". It is going to multidimensional. You are treating it like it has returned a one-dimensional array. That is, this (completely different data but illustrates the array to expect):

array(10) {
  [0]=>
  array(23) {
    ["ID"]=>
    string(1) "1"
    ["post_author"]=>
    string(1) "1"
    ["post_date"]=>
    string(19) "2013-03-26 18:37:57"
    ["post_date_gmt"]=>
    string(19) "2013-03-26 18:37:57"
    ["post_content"]=>
    string(429) "[testsc]
    ...

You need to loop over that result set to get to the individual rows, or do something like this:

$name= $row[0]['lastName'] . ", " . $row[0]['firstName'];

That is true even if the result set is one row. If you know the result set is only one row you can use $wpdb->get_row instead and that will give you a one dimensional array.

Secondly, variables do not expand inside single quotes so all of your attempts to use variables in this way-- 'name' => '$name',-- simple won't work. You have literally set name to $name, not to the data held in the $name variable.

Third, the first parameter of $wpdb ->insert is a string representing the table name. You have used what PHP will interpret to be a constant, and almost certainly an undefined one. You need quotes around chi_orders.

Those are the issues that I spot. If correcting those doesn't fix it, it will move you forward quite a bit.

  • Thanks for the time to look this over and the explanation! I tried to give myself a crash course in WPDB today. This has helped a lot. ended up that my file I'm trying to include for the connection info (confff.php) isn't working. Since I wasn't getting any errors, I figured it was. I made the adjustments you mentioned and entered my connection info directly into the code and it worked perfectly. Is this wrong? If I echo $url it gives expected result of the site url. $url = home_url(); require_once($url.'/viper/confff.php'); – Andy McCormick Jul 17 '13 at 3:57
  • Got it! Changed to: define( 'SHORTINIT', true ); require_once( $_SERVER['DOCUMENT_ROOT'] . '/viper/confff.php' ); – Andy McCormick Jul 17 '13 at 4:12
0

create a custom template and add the following codes inside

$_POST['yourname'], 'email' => $_POST['email'] ); $format = array( '%s', '%s' ); $success=$wpdb->insert( $table, $data, $format ); if($success){ echo 'data has been save' ; } } else { ?>
  • create a custom template and add the following codes inside<br/><?php<br/>if (!empty($_POST)) {<br/>global $wpdb;<br/>$table = wp_achord;<br/>$data = array(<br/>'name' => $_POST['yourname'],<br/><br/>'email' => $_POST['email'] <br/>);<br/> $format = array(<br/>'%s',<br/> '%s' );<br/><br/>$success=$wpdb->insert( $table, $data, $format );<br/><br/>if($success){<br/>echo 'data has been save' ; }<br/>}<br/><br/>else {<br/>?><br/><br/> <form method="post"> <br/> <input type="text" name="yourname"><br/><input type="text name="email"><br/><input type="submit"> <br/></form> <br/><?php } ?> – Aribam Karan Jul 30 '13 at 16:52
  • Please clean this up. Edit your comment into the body of the answer and use the formatting functions provided by the editor for all of the code so that it is readable. Look for the {} button. – s_ha_dum Jul 30 '13 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.