title: Inserting data into a table in WordPress database using WordPress $wpdb
body: <p>I am starting out plugin development and have followed the tutorials on the WordPress Codex sites. I am now stuck - I have a database called "wp_imlisteningto", where the <code>wp_</code> was inserted using:</p>
  
  <pre><code>$table_name = $wpdb-&gt;prefix . "imlisteningto";
  </code></pre>
  
  <p>When the plugin is activated. </p>
  
  <p>The database itself has three columns, set up when the plugin is activated:</p>
  
  <pre><code>$sql = "CREATE TABLE $table_name (
  id mediumint(9) AUTO_INCREMENT,
  album VARCHAR(50),
  artist VARCHAR(50),
  PRIMARY  KEY (id)
  );";
  </code></pre>
  
  <p>I am trying to insert data (by creating a new row) into this database from a php form.</p>
  
  <p>Within the WordPress admin, I create a new page which has the very simple form:</p>
  
  <pre><code>&lt;form action="/wp-content/plugins/listeningto/formhtml.php" method="post"&gt;
  Album: &lt;input type="text" name="album" /&gt;
  Artist: &lt;input type="text" name="artist" /&gt;
  &lt;input type="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Which as you can see calls <code>formhtml.php</code>, which is:</p>
  
  <pre><code>&lt;?php
  global $wpdb;
  
  $wpdb-&gt;insert( $table_name, array( 'album' =&gt; $_POST['album'], 'artist' =&gt; $_POST['artist'] ), array( '$s', '$s' ) );
  ?&gt;
  </code></pre>
  
  <p>When I submit the form, I get an <code>Error 500.0</code> when running the plugin in Worpdress on <code>IIS7.0</code>, and a <code>"Page Not Found"</code> when running on another web server which runs <code>apache</code>.</p>
  
  <p>If I change <code>formhtml.php</code> to:</p>
  
  <pre><code>&lt;?php
  
  echo $_POST['album'];
  echo $_POST['artist'];
  
  ?&gt;
  </code></pre>
  
  <p>Works fine - I get the album and artist that I put in the form. Obviously something I'm doing wrong when inserting the data (in a new row) into the database.</p>
  
  <p>Any thoughts as to what that might be?</p>
  
  <p>UPDATE</p>
  
  <p>Ok, so if I update <code>formhtml.php</code> with this:</p>
  
  <pre><code>&lt;?php
  require_once('../../../wp-config.php');
  $table_name = $wpdb-&gt;prefix . "imlisteningto";
  $wpdb-&gt;insert( $table_name, array( 'album' =&gt; $_POST['album'], 'artist' =&gt; $_POST['artist'] ), array( '$s', '$s' ) );
  ?&gt;
  </code></pre>
  
  <p>I no longer get an error message, but data still doesn't get put into the database.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p><strong>This worked for me:</strong></p>
  
  <pre><code>&lt;?php
  require_once('../../../wp-config.php');
  global $wpdb;
  $table_name = $wpdb-&gt;prefix . "imlisteningto";
  $wpdb-&gt;insert( $table_name, array( 'album' =&gt; $_POST['album'], 'artist' =&gt; $_POST['artist'] ) );
  ?&gt;
  </code></pre>
  
  <p>as did this:</p>
  
  <pre><code>&lt;?php
  require_once('../../../wp-load.php');
  global $wpdb;
  $table_name = $wpdb-&gt;prefix . "imlisteningto";
  $wpdb-&gt;insert( $table_name, array( 'album' =&gt; $_POST['album'], 'artist' =&gt; $_POST['artist'] ) );
  ?&gt;
  </code></pre>
  
  <p>So, for some reason <code>$wpdb</code> was not working unless I required either <code>wp-config</code> or <code>wp-load.php</code>. If include <code>wp-load.php</code>, <code>$wpdb</code> gets values and all is well.</p>