Support » Plugins » Write to database from form in a plugin

  • Trying to create a simple products plugin just to experiment with plugins in wordpress. I have a plugin and here is the code for it.

    Plugin Name: Simple Products
    Plugin URI:
    Description: Keep a simple list of products.
    Version: 0.1 alpha
    Author: Chet Helms
    Author URI:
    $simple_prod_version = "0.1";
    function simple_prod_install () {
       global $wpdb;
       global $simple_prod_version;
       $table_name = $wpdb->prefix . "simple_prods_six";
       if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
          $sql = "CREATE TABLE " . $table_name . " (
    	  id mediumint(9) NOT NULL AUTO_INCREMENT,
    	  time bigint(11) DEFAULT '0' NOT NULL,
    	  name tinytext NOT NULL,
    	  text text NOT NULL,
    	  price VARCHAR(55) NOT NULL,
    	  imgurl VARCHAR(55) NOT NULL,
    	  UNIQUE KEY id (id)
          require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
          $welcome_name = "Product";
          $welcome_text = "description";
    	  $welcome_price = "99.99";
    	  $welcome_url = "";
          $insert = "INSERT INTO " . $table_name .
                " (time, name, text, price, imgurl) " .
                "VALUES ('" . time() . "','" . $wpdb->escape($welcome_name) . "','" . $wpdb->escape($welcome_text) . "','" . $wpdb->escape($welcome_price) . "','" . $wpdb->escape($welcome_url) . "')";
          $results = $wpdb->query( $insert );
          add_option("simple_prod_version", $simple_prod_version);
    function simple_products_admin() {
    function simple_products_admin_actions() {
    	add_options_page("Simple Products ", "Simple Product", 1, "Simple Product", "simple_products_admin");
    add_action('admin_menu', 'simple_products_admin_actions');

    the page in the admin code is:

    <div class="wrap">
    <?php echo '<h2>Simple Products Admin</h2>'; ?>
    <form name="product_form" action="<?php bloginfo('url'); ?>/wp-content/plugins/simple-products/process.php" method="post">
    				<?php    echo '<h4>Add a Product</h4>'; ?>
    				<p><?php _e("Product Name:" ); ?><input type="text" name="name" value="<?php #echo $dbhost; ?>" size="20"><?php #_e(" ex: localhost" ); ?></p>
    				<p><?php _e("Price: " ); ?><input type="text" name="price" value="<?php #echo $dbname; ?>" size="20"><?php #_e(" ex: oscommerce_shop" ); ?></p>
    				<p><?php _e("Image Url: " ); ?><input type="text" name="imgurl" value="<?php #echo $dbuser; ?>" size="20"><?php #_e(" ex: root" ); ?></p>
    				<p><?php _e("Description: " ); ?><input type="text" name="text" value="<?php #echo $dbpwd; ?>" size="20"><?php #_e(" ex: secretpassword" ); ?></p>	
    				<p class="submit">
    				<input type="submit" name="Submit" value="Submit" />


    global $wpdb;
    $table_name = $wpdb->prefix . "simple_prods_six";
    $insert = "INSERT INTO " . $table_name .
                " (name, text, price, imgurl) " .
                "VALUES ('$name','$text','$price','$imgurl')";
    $results = $wpdb->query( $insert );
    print "Your information has been successfully added to the database.";

    and the process form which doesn’t work or carry over the global. Any ideas? What am I doing wrong? Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Have you solved your problem? I have the same one and still no solution!


    I’ve solved this, here is solution for all anyone who trys this example and has problem to get it to work.

    Since you declare $wpdb as global, and since $wpdb is something that belongs to wordpress and is not part of php, you cant expect it to show up in process.php file if you do not include necessary file.

    make sure you include this file like this:


    ..// is used 3 times to get up from the folder where plugin is located. inside wp-config is declaration of $wpdb so then it becomes available to you in your php file…

    btw thanks for the above example, I was looking into getting into developing more complex wordpress plugins and this example was perfect to get me to understand how to do it 🙂

    If anyone has any more wisewords to share, plz do so

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Write to database from form in a plugin’ is closed to new replies.