Wordpress - save data from own plugin - php

My own wordpress plugin generates this data of a file:
filename|creator|date
This is my filename|Max Mustermann|20.11.2017
This is my filename|Anne Mustermann|26.11.2017
This is my filename|Alex Mustermann|27.11.2017
How is wordpress built to save this data? How do I do that the right way?
Thanks for your answers!
Lingo

Try this example,
function bot_install()
{
global $wpdb;
$table = $wpdb->prefix."bot_counter";
$structure = "CREATE TABLE $table (
id INT(9) NOT NULL AUTO_INCREMENT,
bot_name VARCHAR(80) NOT NULL,
bot_mark VARCHAR(20) NOT NULL,
bot_visits INT(9) DEFAULT 0,
UNIQUE KEY id (id)
);";
$wpdb->query($structure);
// Populate table
$wpdb->query("INSERT INTO $table(bot_name, bot_mark)
VALUES('Google Bot', 'googlebot')");
$wpdb->query("INSERT INTO $table(bot_name, bot_mark)
VALUES('Yahoo Slurp', 'yahoo')");
}
You get the result.

Related

How to reinstall database on Prestahop Module?

I'm trying to customize a Prestashop module and I need to add two new colonnes in a table.
The problem is that the database is already created, I guess it did when the module initializing. So if I change the code now, changes does not appear and I keep receiving this mistake:
There is no way to do it in configuration and I suppose if I delete the module my code will dissepear.
How can I do ?
Maybe asking the delete and the create method in a common method (just one time).
I'm new with Smarty and PHP, I see it's not a usual method where the table is created.
Could you help me ?
I'm new with Smarty, how can I do that ?
It looks like it's not a method where the table is created.
$sql = array();
$sql[] =
'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'opartdevis` (
`id_opartdevis` int(10) NOT NULL AUTO_INCREMENT,
`id_shop` int(10) NOT NULL,
`id_cart` int(10) NOT NULL,
`id_customer` int(10) NOT NULL,
`name` varchar(128),
`message_visible` TEXT,
`id_customer_thread` int(10),
`date_add` DATETIME NOT NULL,
`status` int(2) DEFAULT 0,
`id_order` int(10) NULL,
`id_ordered_cart` int(10) NULL,
`remise` int(10),
`delivery` int(10),
PRIMARY KEY (`id_opartdevis`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';
//1.6.1.0 specific price bug fix
if (version_compare(_PS_VERSION_, '1.6.1.0', '=')) {
$sql[] = "ALTER TABLE "._DB_PREFIX_."specific_price DROP INDEX id_product_2";
$sql[] = "ALTER TABLE "._DB_PREFIX_."specific_price ADD INDEX id_product_2 (id_product,id_shop,id_shop_group,id_currency,id_country,id_group,id_customer,id_product_attribute,from_quantity,id_specific_price_rule,id_cart,`from`,`to`)";
}
foreach ($sql as $s) {
if (!Db::getInstance()->execute($s)) {
return false;
}
}
Thanks in advance
Malaury
You need to revert all DB changes when you're uninstalling the module.
Define (or append to existing) uninstall() function in your base module file and run DROP TABLE query on the created table. Example:
public function uninstall() {
if (!parent::uninstall()) {
return false;
}
$sql = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'opartdevis`';
if (!Db::getInstance()->execute($sql)) {
return false;
}
return true;
}
This way, when you're uninstalling the module, the table is going to be deleted (and recrteated when you install it again).

Table in php is not being created when ran through the query

I did this earlier and it worked just fine and I even brought in another table and it was created just fine. I am stuck.
Here is the table that is not working.
<?php
include_once('dbconx.php');
$tbl_pages = "CREATE TABLE IF NOT EXISTS pages (
id INT(11) NOT NULL AUTO_INCREMENT,
label VARCHAR(20) NOT NULL,
title VARCHAR(50) NOT NULL,
body TEXT NOT NULL,
slug VARCHAR(50) NOT NULL,
create TIMESTAMP NOT NULL,
updated TIMESTAMP NULL,
PRIMARY KEY(id),
)";
$query = mysqli_query($dbcon, $tbl_pages);
if ($query === TRUE) {
echo "<h3>Pages table created OK :) </h3>";
} else {
echo "<h3>Pages table NOT created :( </h3>";
}
?>
One of your field names is a MySQL reserved words create. Try changing the field name to something like created. See MySQL Keywords and Reserved Words
You also have an extra comma after the primary key definition.
Try this query:
$tbl_pages = "CREATE TABLE IF NOT EXISTS pages (
id INT(11) NOT NULL AUTO_INCREMENT,
label VARCHAR(20) NOT NULL,
title VARCHAR(50) NOT NULL,
body TEXT NOT NULL,
slug VARCHAR(50) NOT NULL,
created TIMESTAMP NOT NULL,
updated TIMESTAMP NULL,
PRIMARY KEY(id)
)";
You can also echo mysqli_error if it fails so you can see details of the error.

How can configure two tables for admin module?

I have a custom module. I am using this module to upload images through admin. I have 6 more form fields in my module. I am currently using one table to store values of these form fields. However as like in magento, i am planning to store image values in another table. My table structure is given below.
Table : Banner
Fields : banner_id( primary_key , int(11) )
banner_name( varchar (250) )
banner_count( small_int (6) )
status( small_int (6) )
store_id( varchar (250) )
Table : banner_images
Fields : bi_id( primary_key , int (11) )
banner_id( int (11) ) //this should be the 'banner_id' of banner which holds this imge
bi_name( varchar (255) ) //stores image_name
My current edit file look like this:
<?php
class Karaokeshop_Banner_Block_Adminhtml_Banner_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
$this->_blockGroup = 'banner';
$this->_controller = 'adminhtml_banner';
$this->_updateButton('save', 'label', Mage::helper('banner')->__('Save Banner'));
$this->_updateButton('delete', 'label', Mage::helper('banner')->__('Delete Banner'));
}
public function getHeaderText()
{
if( Mage::registry('banner_data') && Mage::registry('banner_data')->getId() )
{
return Mage::helper('banner')->__("Edit Banner");
}
else
{
return Mage::helper('banner')->__('Add Banner');
}
}
}
My sql file look like this :
?php
$installer = $this;
$installer->startSetup();
$installer->run("
DROP TABLE IF EXISTS {$this->getTable('banner')};
CREATE TABLE {$this->getTable('banner')} (
`banner_id` int(11) unsigned NOT NULL auto_increment,
`banner_name` varchar(255) NOT NULL default '',
`banner_count` smallint(6) NOT NULL default '0',
`status` smallint(6) NOT NULL default '0',
`store_id` varchar(255) NOT NULL default '',
`img` varchar(255) NOT NULL default '',
PRIMARY KEY (`banner_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
How can I achieve this? Please provide links, if there is any tutorials for this. Thanks in advance

Trying to post info to database via wordpress but just showing up as '0'

I have a wordpress theme and i'm trying to add an additional field into the themes backend (it's for a gallery slider that has a description and caption text box, I'm trying to add a "quote" box).
As I'm a novice with databases, I've found the relevant files and copied all references to "description" and changed the word to "quote". I also added a "Quote" column into my table.
Everything appears to work fine, however when I type in text to the input field in the backend and click save, it changes the text to "0"
Does anyone know why this might be, what have I done wrong?
I think this is the code that posts to the DB....like I say though I'm a noob with DB's
add_action('wp_ajax_list_slider_items', 'list_slider_items');
function list_slider_items()
{
global $wpdb;
$result = $wpdb->get_results("SELECT IMAGEID, TYPE, CONTENT, THUMB, CAPTION, DESCRIPTION, QUOTE, WIDTH, HEIGHT FROM {$wpdb->prefix}backgrounds WHERE GALLERYID='".$_POST['GALLERYID']."' ORDER BY SLIDERORDER");
$i=0;
foreach($result as $row)
{
echo getSliderItemImage($row->IMAGEID, $row->TYPE, $row->CONTENT, stripslashes($row->CAPTION), stripslashes($row->DESCRIPTION), stripslashes($row->QUOTE), $row->THUMB, $row->WIDTH, $row->HEIGHT);
$i++;
}
die();
}
add_action('wp_ajax_save_slider_items', 'save_slider_items');
function save_slider_items()
{
global $wpdb;
for($i=0; $i<count($_POST['imageID']); $i++)
{
$wpdb->update($wpdb->prefix.'backgrounds', array('CAPTION'=>$_POST['CAPTION'][$i], 'DESCRIPTION'=>$_POST['DESCRIPTION'][$i], 'QUOTE'=>$_POST['QUOTE'][$i], 'SLIDERORDER'=>($i+1)), array('IMAGEID'=>$_POST['imageID'][$i]), array('%s', '%s', '%d'), array('%d'));
}
die();
}
And this looks liek the code that created the table:
function create_backgrounds_table(){
global $wpdb;
$prf = $wpdb->prefix;
$create_query = <<<EOT
CREATE TABLE `{$prf}backgrounds` (
`IMAGEID` int(11) NOT NULL auto_increment,
`GALLERYID` bigint(20) unsigned NOT NULL,
`SLIDERORDER` int(11) unsigned NOT NULL,
`EXT` varchar(255) NOT NULL,
`CAPTION` text,
`DESCRIPTION` mediumtext NOT NULL,
`QUOTE` mediumtext NOT NULL,
`TYPE` varchar(20) default NULL,
`CONTENT` text,
`THUMB` text,
`WIDTH` int(11) default NULL,
`HEIGHT` int(11) NOT NULL,
PRIMARY KEY (`IMAGEID`),
KEY `GALLERYID` (`GALLERYID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
EOT;
$create = $wpdb->get_results($create_query);
}

Creating database table when installing a wordpress plugin

I want to create a table into the database upon installing the plugin I've created.
In my main plugin file (index.php):
register_activation_hook(__FILE__, 'wnm_install');
global $wnm_db_version;
$wnm_db_version = "1.0";
function wnm_install(){
global $wpdb;
global $wnm_db_version;
$sql = "CREATE TABLE tbl_campaigns (
campaignID int(11) NOT NULL AUTO_INCREMENT,
campaign_name varchar(128) NOT NULL,
start_duration date NOT NULL,
end_duration date NOT NULL,
activity varchar(500) NOT NULL,
survey_settings varchar(50) NOT NULL,
limit varchar(50) NOT NULL,
goal varchar(100) DEFAULT NULL,
PRIMARY KEY (campaignID)
) ;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("wnm_db_version", $wnm_db_version);
}
I just followed the instructions from this http://codex.wordpress.org/Creating_Tables_with_Plugins
But it doesn't work.
What seems to be the problem with this code?
limit varchar(50) NOT NULL,
Limit is a keyword, change to something else like
`limit` varchar(50) NOT NULL,
Use back ticks around keywords

Categories