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.
<?php /* Plugin Name: Simple Products Plugin URI: Description: Keep a simple list of products. Version: 0.1 alpha Author: Chet Helms Author URI: */ register_activation_hook(__FILE__,'simple_prod_install'); $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'); dbDelta($sql); $welcome_name = "Product"; $welcome_text = "description"; $welcome_price = "99.99"; $welcome_url = "https://site.com"; $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() { include('simple_products_admin_page.php'); } 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" /> </p> </form> </div>
process.php
global $wpdb; $table_name = $wpdb->prefix . "simple_prods_six"; $name=$_POST['name']; $price=$_POST['price']; $text=$_POST['text']; $imgurl=$_POST['imgurl']; $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)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Write to database from form in a plugin’ is closed to new replies.