Title: theme/functions.php insert won&#039;t work
Last modified: August 20, 2016

---

# theme/functions.php insert won't work

 *  [dbaltozer](https://wordpress.org/support/users/dbaltozer/)
 * (@dbaltozer)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/themefunctionsphp-insert-wont-work/)
 * Hi All! Okay, I’ve read all the documentation on INSERTING data. I’m working 
   on the localhost using XAMPP. I’m adding a function to the themes/functions.php
   file
 * I made a custom table. I added it to the includes/db.php. I tried the recommened
   INSERT statement and every variation of it found in the documentation and around
   Google. I tried the QUERY statement. It just seems my query will nto run, not
   even the SELECT and it will NEVER INSERT a simple ONE RECORD into a ONE COLUMN
   table. It’s only one column for now until I get the insert working.
    I hard coded
   the use database; insert into statement and ran it on the database itself and
   there are no syntax error. I do know PHP and MySQL very well, but WP has be turning
   even more grey! LOL I did declare the global $wpdb and even asked it to show 
   errors, though that won’t happen either. I emulated the WP syntax in various 
   formats and nothing! It just won’t run any queries!
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_
 * I know there are many different ways to write this function – but this is as 
   hardcoded and verbose as I could make it to try and find the problem.
    <p>Anyone??`

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [digibucc](https://wordpress.org/support/users/digibucc/)
 * (@digibucc)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/themefunctionsphp-insert-wont-work/#post-2270898)
 * please update your code with pastebin, so we can have an idea.
 * i have a custom table that is written/read/deleted to with:
 * `$wpdb->insert( wp_custom_table, $entry);`
 *     ```
       $wpdb->query ('SELECT * FROM wp_custom_table');
       wp_reset_postdata();
       wp_reset_query();
       ```
   
 * &
 * `$wpdb->query("DELETE FROM wp_custom_table WHERE number = '$number'");`
 * try a different wordpress function, to make sure it is loading properly. get 
   the current logged in user or echo the site name or something.
 *  Thread Starter [dbaltozer](https://wordpress.org/support/users/dbaltozer/)
 * (@dbaltozer)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/themefunctionsphp-insert-wont-work/#post-2270904)
 * Is this what you’re lookin for?
 * [http://pastebin.com/embed_iframe.php?i=wu8nQwBs](http://pastebin.com/embed_iframe.php?i=wu8nQwBs)
 *  [digibucc](https://wordpress.org/support/users/digibucc/)
 * (@digibucc)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/themefunctionsphp-insert-wont-work/#post-2270911)
 * yes.
 * i would try some very simple implementations like i have above, to verify table
   name is correct and all.
 * you have : wp_gbsavingsclub.wp_access_codes
 * doesn’t that tell it to look in a db named gbsavingsclub for the table wp_access_codes?
   in my setup as you can see, it’s just the table name. i’m pretty sure wpdb defines
   the db you are working in, and you just define the table.
 * note that though wpdb uses mysql syntax you can’t use just any mysql query.
 *  Thread Starter [dbaltozer](https://wordpress.org/support/users/dbaltozer/)
 * (@dbaltozer)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/themefunctionsphp-insert-wont-work/#post-2270925)
 * Yes, this code also talkes to another database so I explicitly told it to use
   that database to rule out any conflict. I’ve tried all the WP syntax conventions
   and absolutly no query at all will run.
 * I tried the WP convention $wpdb->access_codes
    I added the new table to the db.
   php file I copied the code into the SQL prompt inside the database and ran it–
   replacing the vaiable of course, and it runs fine, even with the naming convention
   I have in the code as you see it.
 * I tried to use the porper WP SQL $wpdb->insert(table, array(col=> value)
 * I tried to use $wpdb->get_var(select…)
 * I read this over and over [http://codex.wordpress.org/Class_Reference/wpdb](http://codex.wordpress.org/Class_Reference/wpdb)
 * The queries just NEVER execute. The SELECT checks the table for the variable …
   I entered it in the database myself and it still evealuated as if it wasn’t in
   the database so I know even the SELECT isn’t running.
 * The insert is in both places just so it run, until I get it working.
 * Queries just aren’t working though the rest of the registration function executes.
 * It’s really weird. I’ve been trying for 6 hours easy on what is supposed to be
   a simple thing lol
 *  [digibucc](https://wordpress.org/support/users/digibucc/)
 * (@digibucc)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/themefunctionsphp-insert-wont-work/#post-2270931)
 * that code may use 2 dbs but wordpress doesn’t, does it? you can only use wpdb
   to mess with the db that wordpress runs off of, for any others you need to use
   mysql statements.
 * so if the table name is “wp_access_codes” , (and it should have the wp_ in the
   front, visible from phpmyadmin for example)
 * then run:
 *     ```
       $wpdb->query ('SELECT * FROM wp_access_codes');
       while ($n <= $wpdb->num_rows) {
       $entry = $wpdb->get_row('SELECT * FROM wp_driver_alerts', ARRAY_A, $n);
       echo 'entry #'. $n. ' =  '. $entry. '<br />';
       $n++;
       ```
   
 * it should print everything in the table. if not, you are not calling the table
   correctly (the name is wrong, you’re specifying the db, etc)
 * make sure to:
    `require_once('/wp-load.php');` with whatever relative location
   it’s in
 *  Thread Starter [dbaltozer](https://wordpress.org/support/users/dbaltozer/)
 * (@dbaltozer)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/themefunctionsphp-insert-wont-work/#post-2270936)
 * Yes, this one installation actually talks to two different databases using the
   naming convention I have in my code databasename(DOT)tablename. Normal MySQL.
   Should work. wp_ is visible in phpmyadmin – I kinda live in a database lol and
   PHP is my favorite, so not exactly an amature – but sure feeling like it now 
   lol
 * I’ll try your code snippet and see what’ts up.
 * Thanks!
 *  [digibucc](https://wordpress.org/support/users/digibucc/)
 * (@digibucc)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/themefunctionsphp-insert-wont-work/#post-2270937)
 * yeah i use mysql whenever i need a db but php is where it’s at for me 🙂
 * i can tell you know, as your syntax was correct. i didn’t even know wp could 
   run off two db, i need that for a project so now have something to learn 🙂
 * try running a straight mysql statement? obviously it should work but sometimes
   going back farther than necessary triggers the solution in my mind.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘theme/functions.php insert won't work’ is closed to new replies.

## Tags

 * [function](https://wordpress.org/support/topic-tag/function/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 7 replies
 * 2 participants
 * Last reply from: [digibucc](https://wordpress.org/support/users/digibucc/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/themefunctionsphp-insert-wont-work/#post-2270937)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
