Trying to create a pair of database tables within my table. Below is the code block that is being executed on the activation of my plugin. Wordpress reports that it's successful, however, when refreshing the database the employee table is not being created. The department table however is created successfully.
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
// create the database table.
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . "plugin_department";
$table_name2 = $wpdb->prefix . "plugin_employee";
$sql = "CREATE TABLE $table_name (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
$sql2 = "CREATE TABLE $table_name2 (
id INT(11) NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
department_id INT(11) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT department_id
FOREIGN KEY (id)
REFERENCES plugin_department (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
) $charset_collate;";
dbDelta($sql);
dbDelta($sql2);
Ideas on how to resolve would be greatly appreciated.
Discovered the issue, using mysql workbench I had it generate the create table script for me, then was a simple copy and paste into the $sql2 variable. My assumption is that there must be a wonky character or a syntax error that is hard for my eyes to pick out.
$sql2 = "CREATE TABLE $table_name2 (
`id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(255) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`department_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `wp_plugin_employee_department_idx` (`department_id` ASC),
CONSTRAINT `wp_plugin_employee_department`
FOREIGN KEY (`department_id`)
REFERENCES `plugin_intranet`.`wp_plugin_department` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)";
Create Table
global $wpdb;
if ($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}tabloadi'") != $wpdb->prefix . 'tabloadi'){
$wpdb->query("CREATE TABLE {$wpdb->prefix}tabloadi(
id integer not null auto_increment,
alan1 TINYTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
alan2 TINYTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
alan3 TINYTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
alan4tarih TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);");
}
SELECT
global $wpdb;
$tabloadi = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}tabloadi WHERE sart1=$deger1" );
foreach($tabloadi as $row)
{
echo $row->id;
echo $row->alan1;
echo $row->tarih;
}
Delete
global $wpdb;
$delete = $wpdb->delete($wpdb->prefix.'tabloadi',array('alan1'=>$alan1deger,'alan2'=>$alan2deger));
Related
I cannot create table in wordpress, i'm not understand why?
private function create($prefix)
{
$sql = "CREATE TABLE " . $prefix . "submit (
submit_id bigint PRIMARY KEY AUTO_INCREMENT,
post_id bigint,
user_id bigint,
author text,
user_email text,
source text,
pass text,
language text,
time datetime,
CONSTRAINT post_id FOREIGN KEY (post_id) REFERENCES wp_posts(ID),
CONSTRAINT user_id FOREIGN KEY (user_id) REFERENCES wp_users(ID)
)";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
var_dump(dbDelta($sql));
}
The var_dump() shows:
array(1) { ["wp_submit"]=> string(23) "Created table wp_submit" }
add_action("after_switch_theme", "my_custom_table_setup");
function my_custom_table_setup()
{
global $wpdb;
$create_table_query = "
CREATE TABLE {$wpdb->prefix}products_by_sku (
id int(11) NOT NULL auto_increment,
product_id int(11) NOT NULL,
lot_id varchar(25) NOT NULL,
title varchar(255) NOT NULL,
LP varchar(25) NOT NULL,
catgeory varchar(255) NOT NULL,
product_name varchar(255) NOT NULL,
UPC varchar(255) DEFAULT NULL,
ASIN varchar(255) DEFAULT NULL,
original_retail_price float DEFAULT NULL,
quantity int(11) DEFAULT NULL,
total_original_retail float NOT NULL DEFAULT 0,
stock_image text,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $create_table_query );
}
Try this code.
Thank you.
You can use this code:
global $jal_db_version;
$jal_db_version = '1.0';
function jal_install() {
global $wpdb;
global $jal_db_version;
$table_name = $wpdb->prefix . 'liveshoutbox';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url varchar(55) DEFAULT '' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
add_option( 'jal_db_version', $jal_db_version );
}
register_activation_hook( __FILE__, 'jal_install' );
I found this useful Internationalization code:
http://pastebin.com/SyKmPYTX
everything works well except I am unable to connect to database.
what I need:
1) I need to check last segment
2) this is my db
DROP TABLE IF EXISTS `translate`;
DROP TABLE IF EXISTS `trans_data`;
DROP TABLE IF EXISTS `languages`;
/*Table structure for table `languages` */
CREATE TABLE `languages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`key` varchar(5) NOT NULL,
`default` tinyint(1) NOT NULL DEFAULT '0',
`display` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
/*Data for the table `languages` */
insert into `languages`(`name`,`key`,`default`,`display`) values
('GEO','ka',1,1),
('ENG','en',0,1),
('RUS','ru',0,1);
/*Table structure for table `trans_data` */
CREATE TABLE `trans_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
/*Table structure for table `translate` */
CREATE TABLE `translate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lang` int(11) NOT NULL,
`data` int(11) NOT NULL,
`trans` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `lang_2` (`lang`,`data`),
KEY `lang` (`lang`),
KEY `data` (`data`),
CONSTRAINT `translate_ibfk_1` FOREIGN KEY (`lang`) REFERENCES `languages` (`id`),
CONSTRAINT `translate_ibfk_2` FOREIGN KEY (`data`) REFERENCES `trans_data` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=340 DEFAULT CHARSET=utf8;
in the first I need to do something like this
$this->db->where('key', end($this->segments));
$query = $this->db->get('languages');
$lang = $query->row();
if(empty($lang)){
$this->db->where('default', 1);
$query2 = $this->db->get('languages');
$lang = $query2->row();
}
$lang_array = [
"lang_key" => $lang->key,
"lang_id" => $lang->id
];
$this->session->set_userdata($lang_array);
if(end($this->uri->segment_array()) == $lang->key){
unset($this->uri->segments[intval($this->uri->total_segments() - 1)]);
}
after this functionality
then I need to download translates from database and save it in object with this query
SELECT TD.`name` , T.`trans`
FROM `translate` AS T
INNER JOIN `languages` AS L ON (T.`lang` = L.`id`)
INNER JOIN `trans_data` AS TD ON (T.`data` = TD.`id`)
WHERE (L.`key` = :lang_key);
and when I call from view
echo $this->lang->line('trans_key');
I need to get the value
but I do not undestand how to edit this class to do this job...
can you help me and edit this class for me?
thank you for future...
I got this table
CREATE TABLE IF NOT EXISTS `set_indice_cuestionarios` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cuestionario` int(11) NOT NULL,
`fecha` date NOT NULL,
`hora` time NOT NULL,
`aplico` varchar(100) NOT NULL,
`cliente` int(11) NOT NULL,
`tienda` int(11) NOT NULL,
`status` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `tienda` (`tienda`),
UNIQUE KEY `cuestionario` (`cuestionario`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
And this is my SQL sentence:
$sql_indice = "INSERT INTO set_indice_cuestionarios (cuestionario, fecha, hora, aplico, cliente, tienda) values('$cuestionario','$fecha','$hora','$aplico','$cliente','$tienda') ON DUPLICATE KEY UPDATE cuestionario = '$cuestionario', fecha = '$fecha', hora = '$hora', aplico = '$aplico', cliente = '$cliente', status = 0";
I want to Update a row if $tienda = tienda and $cuestionario = cuestionario and to Insert a new one if the any of the values doesn't match.
The Update is happening when both values match but when I only change $cuestionario it Updates the table, instead of inserting a new record, and when I only change $tienda it does the Insert. I have no idea what I' am doing wrong. Thank you in advance!
I am trying to create a Wordpress plugin. The code for setting up the database tables will be provided shortly. However the problem I'm facing is not not an error as much as a lack thereof. No matter what I do, dbdelta seems to do absolutely nothing - and wordpress will not generate any relevant error to at least give me a clue on what's going wrong.
The code is:
function create_tables()
{
global $wpdb;
$pl_nm = VSystem::$plugin_name;
$create_voters = "create table if not exists {$wpdb->prefix}{$pl_nm}_voters(
ID bigint(20) unsigned auto_increment,
user_id bigint(20) unsigned not null,
primary key (ID),
foreign key (user_id) references {$wpdb->prefix}users(ID)
);";
$create_links = "create table if not exists {$wpdb->prefix}{$pl_nm}_links(
ID bigint(20) unsigned auto_increment,
product bigint(20) unsigned,
name varchar(256) not null,
url varchar(512) not null,
description text,
posted datetime,
poster bigint(20) unsigned not null,
primary key (ID),
foreign key (poster) references {$wpdb->prefix}users(ID)
);";
$create_votes = "create table if not exists {$wpdb->prefix}{$pl_nm}_votes(
ID bigint(20) unsigned auto_increment,
link_id bigint(20) unsigned not null,
voter_id bigint(20) unsigned not null,
choice int not null,
primary key (ID),
foreign key (link_id) references {$wpdb->prefix}{$pl_nm}_links(ID),
foreign key (voter_id) references {$wpdb->prefix}users(ID)
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $create_voters );
dbDelta( $create_links );
dbDelta( $create_votes );
dbDelta("asdasdsadsad");
//wp_die("{$wpdb->prefix}{$pl_nm}");
add_option('db_version', VSystem::$defaults['db_version']);
}
function vsystem_install()
{
create_tables();
}
Now I know this function is being called at least because uncommenting the wp_die command acts as it is supposed to. Can I get any help?
dbDelta("asdasdsadsad");
Not even that seems to generate any kind of response. I might be able to do this on my own if someone could tell me how to get dbdelta to do anything.
I restarted the MySQL service and I attempted to use my PHP programs delete function to delete an existing row but I'm finding although the delete queries were counted the row was not deleted. I tried applying on delete cascade to the foreign key of the child table but that did not seem to have an effect. I'm wondering why the delete would be doing nothing.
CREATE TABLE `customers` (
`idcustomers` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(45) DEFAULT NULL,
`lastname` varchar(45) DEFAULT NULL,
`address1` varchar(45) DEFAULT NULL,
`address2` varchar(45) DEFAULT NULL,
`city` varchar(45) DEFAULT NULL,
`state` varchar(45) DEFAULT NULL,
`zip` varchar(45) DEFAULT NULL,
`phone` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`cell` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idcustomers`),
UNIQUE KEY `idcustomers_UNIQUE` (`idcustomers`)
) ENGINE=InnoDB AUTO_INCREMENT=54 DEFAULT CHARSET=latin1
CREATE TABLE `events` (
`idevents` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(250) DEFAULT NULL,
`start` datetime DEFAULT NULL,
`end` datetime DEFAULT NULL,
`allday` varchar(50) DEFAULT NULL,
`url` varchar(1000) DEFAULT NULL,
`customerid` int(11) NOT NULL,
`memo` longtext,
`dispatchstatus` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idevents`),
KEY `FK_events` (`customerid`),
CONSTRAINT `FK_events` FOREIGN KEY (`customerid`) REFERENCES `customers` (`idcustomers`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1
Com_delete 2
The PHP looks like this:
<?php
session_start();
date_default_timezone_set("America/Los_Angeles");
if($_SESSION['loggedin'] != TRUE)
{
header("Location: index.php");
}
require_once('../php.securelogin/include.securelogin.php');
$mysqli = new mysqli($ad_host, $ad_user, $ad_password, "samedaycrm");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$customerid = $_SESSION['customer_id'];
$tSQL = "delete from events where customerid = \"$customerid\"";
$result = $mysqli->query($tSQL);
$tSQL = "delete from customers where idcustomers = \"$customerid\"";
$result = $mysqli->query($tSQL);
echo $mysqli->error;
?>
Assuming that the customerid and idcustomers columns are both numeric it should be fine. You should not need to quote the variables in those queries btw, then you wouldnt need to escape them. You may try:
$tSQL = "delete from events where customerid = $customerid";
but it should not be any different than what you used already. Of course if you are not sure of the type of the column you can use:
$tSQL = "delete from events where customerid = '".$customerid."'";
or you can get away with:
$tSQL = "delete from events where customerid = '$customerid'";
but I have always hated that for some reason.
if all of that fails troubleshoot by spitting out the $customerid (or even the whole $tSQL) variable and then trying the query manually in phpmyadmin or toad or whatever db client you use, and see what it tells you. If it just says 0 rows affected, then run it like a select instead. Tailor to fit.