WordPress 5.3
I'm writing a plugin thif method removes the database table.
When I put a breakpoint in this function and execute the copied value of $sql, the table is been really removed. But this very code doesn't remove the table. Could you help me here?
public function deactivate(){
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$sql = "drop table if exists $this->table_name;";
dbDelta($sql);
}
Try this:
$sql = "drop table if exists ".$this->table_name.";";
or this:
$sql = "drop table if exists {$this->table_name};";
Related
I have a link which will use to create sql table in database . I want to use date + string as table name . When I am just using string that is creating table in my database . But when I am using variable as table name with date function , its not working and not creating any table .
<?php require("connection.php");
$ins = new Db();
$ins->dbconnect();
$tablename = date('d-m-y')."customername";
$table= "CREATE TABLE $tablename(id int) ";
Create table
When I am just using string in $tablename and its create table . But when using date() function in $tablename , its not creating table anymore .
You're creating a empty table which will error out, if you want a table without columns you can do it like this:
CREATE TABLE 01_01_01(t INT);
ALTER TABLE 01_01_01
DROP COLUMN t;
Notice you have to use underscores instead of hyphens!
I have done it as follows :
public function table($tname){
return $this->dbconnection->query("CREATE TABLE $tname(id int)");
}
$n = $validation['name'];
$m = str_replace(" ","",$n);
$m=date("d_m_Y_g_i_s_A_").$m;
$tname = "$m";
$t = $ins->table($tname);
I have paste those code in submit button and its finely creating new table with date + string .
I am trying to CREATE TRIGGER on custom table in mySQL db with dbDelta() function in wordpress plugin I am building!
On plugin activation, tables are created and populated with default data. When execute this function:
function PDFsem_initial_trigger($ldd_tabela) {
global $wpdb;
$table_name = $wpdb->prefix.$ldd_tabela;
//it works!!!
$sql = "CREATE TRIGGER brisIspod BEFORE DELETE ON ".$table_name." FOR EACH ROW INSERT INTO wp_ldd_usluga (usluga_naziv,usluga_rbr,seminar_id) VALUES('NEW TRIGGERED DATA', 2, 2)";
/*
//dont work!!!
$sql = "CREATE TRIGGER brisIspod BEFORE DELETE ON wp_ldd_seminar FOR EACH ROW DELETE FROM wp_ldd_usluga WHERE wp_ldd_usluga.seminar_id = old.id_seminar;";
*/
dbDelta( $sql );
}
Function creates trigger on BEFORE DELETE ... INSERT INTO..., but wont create trigger on BEFORE DELETE ... DELETE... . If execute sql query in phpMysqlAdmin console directly trigger is created regularly.
I am not creating both triggers in same time so one is under comment. First trigger is just test sample.
Is it possible to add trigger I want with dbDelta() in wordpress mySql db?
Trigger in mySql can be created with :
$sql = "CREATE TRIGGER brisIspod BEFORE DELETE ON wp_ldd_seminar FOR EACH ROW DELETE FROM wp_ldd_usluga WHERE wp_ldd_usluga.seminar_id = old.id_seminar;";
$wpdb->query($sql);
I have installed WordPress and the plugin contact form 7 in it.
To make it a multi-page contact form I have installed Contact Form 7 Multi-Step Forms as well.
Everything works fine until yet. Even the mail gets sent.
The problem I am having is, that I want to run some PHP-code before the emails get sent.
I have inserted this code to try the ability of the plugin to run it.
function testfunc( $cf7 )
{
mysql_connect("localhost", "user_name", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
mysql_query("CREATE TABLE `aaaaaaaaaaa` ( test VARCHAR(30))");
}
add_action( 'wpcf7_before_send_mail', 'testfunc', 1);
The function even works fine when I run it outside of the plugin in an extra php-file.
Now I cannot figure out why the function does not work when inserted in the plugin?
Query database in wordpress using it's global wpdb object like this way
Refer this
function testfunc( $cf7 )
{
global $wpdb;
$wpdb->query("CREATE TABLE `aaaaaaaaaaa` ( test VARCHAR(30))");
}
add_action( 'wpcf7_before_send_mail', 'testfunc', 1);
wordpress create tabel using this way not in php structure for connect:
More info hear for create, get table data for wp
function testfunc( $cf7 )
{
global $wpdb;
$table_name = $wpdb->prefix . 'tablename';
$sql = "CREATE TABLE IF NOT EXISTS ".$table_name."(
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) DEFAULT NULL,
UNIQUE KEY id (id)
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
+1 for Rave Patel
I only suggest that you check if the table exists before trying to create it everytime.
function testfunc( $cf7 )
{
global $wpdb;
$table_name = $wpdb->prefix . 'tablename';
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE $table_name (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) DEFAULT NULL,
UNIQUE KEY id (id)
)";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
}
enter
<?php $dob=$_SESSION['dob'];
$month=$_SESSION['month'];
$exists = false;
$columns = mysql_query("show columns from $month");
while($c = mysql_fetch_assoc($columns)){
if($c['Field'] == $dob){
$exists = true;
break;
}
}
if(!$exists){
mysql_query("ALTER TABLE `$month` ADD `$dob` varchar(100) default absent");
}
$roll=$_SESSION['var'];
foreach( $roll as $value=>$roll) {
$name = mysql_real_escape_string ($roll);
$sql="insert into $month (roll) values ('$name')";
mysql_query($sql)or die(mysql_error());
}
?>
Q:-how to create a table and alter table in mysql php?
To create a table do:
CREATE TABLE `tableName` (
... Columns ...
);
cretae table:
CREATE TABLE <tablename> (<column_name <data_type>(length if_required), .... );
Alter Table:
ALTER TABLE <tablename> ADD <newcolumnname> <data_type>(length if_required);
ALTER TABLE <tablename> DROP <columnname>;
ALTER TABLE <tablename> MODIFY <columnname> <data_type>(length if_required);
Update table:
UPDATE <tablename> SET <columnname> = <value> WHERE(if_required) <condition>;
Maybe you need this? This ORM library allow you to develop DB schema on the flight. More here.
Or maybe you need so called CODE FIRST STYLE ORM LIBRARY FOR PHP? I know one but it creates/modifies/alters tables according to your class definitions:
For example if you have:
class user
{
public $id;
public $name;
}
Bu default it will create table user with fields id and name. Also that framework gives full control on field properties and table properties using doc comments. It is called db.php (http://db.php).
I am making a wordpress plugin, where I have functions in the main php file:
<?php
include_once "define.php";
include_once 'includes/form_processor.php';
include_once 'includes/email_subscriber.php';
include_once 'includes/options_page.php';
include_once 'includes/widget.php';
//register the plugin installation methods
register_activation_hook(__FILE__, 'install_simple_subscriber_plugin');
register_deactivation_hook(__FILE__, 'uninstall_simple_subscriber_plugin');
//register the plugin url
wp_enqueue_style('simple-email-subscriber-css', plugins_url('/simple_email_subscriber/stylesheets/style.css'));
function install_simple_subscriber_plugin(){
add_action('plugins_loaded', 'setup_plugin_actions');
email_subscriber::install_database();
email_subscriber::update_database();
}
function setup_plugin_actions(){
if(has_action('publish_post')){
$simple_email_subscriber = new email_subscriber();
//add action listener to post publication
add_action('publish_post', array($simple_email_subscriber, 'on_publish_post'));
add_action('publish__future_post', array($simple_email_subscriber, 'on_publish_post'));
}
//add the admin menu pages
$options_page = new options_page();
}
function uninstall_simple_subscriber_plugin(){
remove_action('plugins_loaded', 'setup_plugin_actions');
}
?>
AND in my custom class I have:
<?php
class email_subscriber{
/*
* Plugin Core Methods
* */
static function install_database() {
//define create table query
$sql = "CREATE TABLE IF NOT EXISTS ".SIMPLE_EMAIL_SUBSCRIBER_DB_NAME."(
id mediumint(9) NOT NULL AUTO_INCREMENT,
subscribe_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
email VARCHAR(55) NOT NULL,
UNIQUE KEY id (id)
);";
//update db and make it auto adjust
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
//HERE IS THE NEW BITS ADDED TO THE EXISTING PLUGIN THAT'S NOT GET CALLED
static function update_database(){
$sql=
"DELIMITER $$
CREATE PROCEDURE Alter_Table()
BEGIN
DECLARE _count INT;
SET _count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'email_subscription' AND
COLUMN_NAME = 'subscribe_all');
IF _count = 0 THEN
ALTER TABLE ".SIMPLE_EMAIL_SUBSCRIBER_DB_NAME."
ADD COLUMN subscribe_all TINYINT(1) DEFAULT 1,
ADD COLUMN subscribe_category varchar(512) DEFAULT NULL;
END IF;
END $$
DELIMITER ;
CALL Alter_Table();
DROP PROCEDURE Alter_Table;
";
//update db and make it auto adjust
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
//add the options for db defination
add_option("ses_db_version", SES_PLUGIN_VERSION);
}
.................and other codes
}
The update_database() method is the new method I added in, that's trying to update the database for extra functions. but it's never get run when the plugin activates.
You just want to execute that query.
global $wpdb;
$wpdb->query($sql);
dbDelta is used to create new tables or update existing ones to conform to an updated scheme. Simply put, it accepts a CREATE TABLE query and transforms it into an ALTER TABLE query if necessary. The Codex says:
The dbDelta function examines the current table structure, compares it
to the desired table structure, and either adds or modifies the table
as necessary, so it can be very handy for updates [...] Note that the
dbDelta function is rather picky, however.