I created the first table for my plugin, and after working on adding more features I realized I needed to add a second table for an additional type of data.
Troubleshooting efforts so far have resolved the issue with dbDelta() not using the same variable name for the sql data, running dbDelta() both as a combined sql var ($setup_sql .= '...' instead of $setup_sql = '...'), and also running the code as a simple function rather than a class.
I even tried running a single table creation with a different name, just to check the code worked, and it still didn't create another table.
I have had no errors running through all these tests, and for all intents and purposes it seems that Wordpress doesn't allow a plugin to create more than one table - but I know that's not the case.
class activation_setup {
public static function wpd_activate() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$wpd_table_trig = $wpdb->prefix . 'wpd_triggers';
$setup_sql_trig = "CREATE TABLE $wpd_table_trig (
id int(11 ) NOT NULL AUTO_INCREMENT,
trigger varchar(255) DEFAULT NULL,
popup varchar(255) DEFAULT NULL,
notify varchar(255) DEFAULT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
dbDelta( $setup_sql_trig );
$wpd_table_sel = $wpdb->prefix . 'wpd_selections';
$setup_sql_sel = "CREATE TABLE $wpd_table_sel (
id int(11 ) NOT NULL AUTO_INCREMENT,
selected varchar(255) DEFAULT NULL,
session varchar(255) DEFAULT NULL,
ip varchar(255) DEFAULT NULL,
page varchar(255) DEFAULT NULL,
date varchar(255) DEFAULT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
dbDelta( $setup_sql_sel );
update_option('wpd_activated',true);
}
}
$activation_setup = new activation_setup();
register_activation_hook( __FILE__ , array( $activation_setup, 'wpd_activate' ) );
I'm going absolutely nuts trying to work out WHY this code doesn't add the second table, even after stripping out the duplicate table code and running it with a different table name.
I've through a ton of blog posts and Stack answers trying to get to the bottom of it.
Any help would be fantastic.
Issue is with the table definition.
You've written trigger in your table definition it is keyword also for mysql.
That was the issue.
Your problem will be solved by this change.
Use "`" when writing field name.
class activation_setup {
public static function wpd_activate() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$wpd_table_trig = $wpdb->prefix . 'wpd_triggers';
$setup_sql_trig = "CREATE TABLE $wpd_table_trig (
`id` int(11 ) NOT NULL AUTO_INCREMENT,
`trigger` varchar(255) DEFAULT NULL,
`popup` varchar(255) DEFAULT NULL,
`notify` varchar(255) DEFAULT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
dbDelta( $setup_sql_trig );
$wpd_table_sel = $wpdb->prefix . 'wpd_selections';
$setup_sql_sel = "CREATE TABLE $wpd_table_sel (
`id` int(11 ) NOT NULL AUTO_INCREMENT,
`selected` varchar(255) DEFAULT NULL,
`session` varchar(255) DEFAULT NULL,
`ip` varchar(255) DEFAULT NULL,
`page` varchar(255) DEFAULT NULL,
`date` varchar(255) DEFAULT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
dbDelta( $setup_sql_sel );
}
}
$activation_setup = new activation_setup();
register_activation_hook( __FILE__ , array( $activation_setup, 'wpd_activate' ) );
If you want to make sure that issue is with that trigger then remove "`" from trigger field and then again activate plugin.
Related
I have my databse: public
I have my table: info
in row files of player Maria i have this:
['python22.dll', ''], ['python27.dll', ''], ['channel.inf', ''], ['devil.dll', ''],['is_hack.exe', '']
The first 4 is normal so I don't want to see them in the list .. i just want to catch the "is_hack.exe"
My mysql query to get all ( including my normal files) is:
query("SELECT files FROM public.info WHERE player = 'Maria' ORDER BY actual_time DESC LIMIT 0,1;")
I need to see all the names that are not mine. Like:
FILE_FROM_OUTSIDE1 = is_hack.exe
FILE_FROM_OUTSIDE2 = stackoverflow.dll
If you know about LUA, i can get the entire query results to LUA then begin to parse.. but how?
EDIT:
This is my table:
CREATE TABLE `info` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`player` varchar(12) DEFAULT NULL,
`type_check` varchar(10) DEFAULT NULL,
`normal` varchar(20) DEFAULT NULL,
`actual` int(3) DEFAULT NULL,
`actual_time` varchar(20) DEFAULT NULL,
`files` varchar(3000) DEFAULT NULL,
PRIMARY KEY (`id`)
In "files" i have all this:
['python22.dll', ''], ['python27.dll', ''], ['channel.inf', ''], ['devil.dll', ''],['is_hack.exe', '']
I just want to select the is_hack.exe .. like: print is_hack.exe
I'm not sure if this is what you want. But i think this might help:
Replace the filename_field with your col name.
query("SELECT files FROM public.info WHERE player = 'Maria' AND filename_field = 'is_hack.exe' ORDER BY actual_time DESC LIMIT 0,1;")
UPDATE
I don't know if this I'm right but you should not save data like this. You can create a files table and specify which user own each row. Like:
CREATE TABLE `playerfiles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`player` int(11) DEFAULT NULL,
`filename` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`)
);
Then you can retrieve data with JOIN.
SELECT * FROM `info`
JOIN `playerfiles` ON `playerfiles.player` = `info.id`
WHERE `info.player` = 'Maria' and `playerfiles.filename` = 'THE NAME'
I have the same problem, and do not understand the answer #5343141 titled "The proper way to insert a NULL into a database with CodeIgniter". I tried all given answers, without success.
This is my MySQL table:
The 2 last columns:
`id_etablissement_commanditaire` int(10) unsigned DEFAULT NULL,
`id_etablissement_payeur` int(10) unsigned DEFAULT NULL,
that makes problem, do have "NULL", so they accept NULL value (tried with phpMyAdmin, it works).
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE IF NOT EXISTS `contacts` (
`id_contact` int(10) unsigned NOT NULL AUTO_INCREMENT,
`service` varchar(50) NOT NULL,
`civilite` set('Monsieur','Madame') NOT NULL,
`prenom` varchar(50) NOT NULL,
`nom` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`tel_fixe` varchar(14) NOT NULL,
`tel_mobile` varchar(14) NOT NULL,
`fax` varchar(14) NOT NULL,
`id_etablissement_commanditaire` int(10) unsigned DEFAULT NULL,
`id_etablissement_payeur` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id_contact`),
KEY `id_etablissement_payeur` (`id_etablissement_payeur`),
KEY `id_etablissement_commanditaire` (`id_etablissement_commanditaire`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=28 ;
ALTER TABLE `contacts`
ADD CONSTRAINT `contacts_ibfk_2` FOREIGN KEY (`id_etablissement_payeur`) REFERENCES `etablissements_payeurs` (`id_etablissement_payeur`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `contacts_ibfk_3` FOREIGN KEY (`id_etablissement_commanditaire`) REFERENCES `etablissements_commanditaires` (`id_etablissement_commanditaire`) ON DELETE NO ACTION ON UPDATE NO ACTION;
This is my CodeIgniter controller code :
1) I need to initialize $_POSTs to '' if they do not exist.
if ( ! isset( $_POST['action'] ) ) $_POST['action'] = '';
if ( ! isset( $_POST['id_contact'] ) ) $_POST['id_contact'] = '';
if ( ! isset( $_POST['service'] ) ) $_POST['service'] = '';
if ( ! isset( $_POST['civilite'] ) ) $_POST['civilite'] = '';
if ( ! isset( $_POST['prenom'] ) ) $_POST['prenom'] = '';
if ( ! isset( $_POST['nom'] ) ) $_POST['nom'] = '';
if ( ! isset( $_POST['email'] ) ) $_POST['email'] = '';
if ( ! isset( $_POST['tel_fixe'] ) ) $_POST['tel_fixe'] = '';
if ( ! isset( $_POST['tel_mobile'] ) ) $_POST['tel_mobile'] = '';
if ( ! isset( $_POST['fax'] ) ) $_POST['fax'] = '';
if ( ! isset( $_POST['id_etablissement_commanditaire'] ) ) $_POST['id_etablissement_commanditaire'] = NULL;
if ( ! isset( $_POST['id_etablissement_payeur'] ) ) $_POST['id_etablissement_payeur'] = NULL;
For the last 2 if, I tried to initialize to '' and NULL, but none works.
2) Insert into database :
$donnees = array(
'service'=> $_POST['service'],
'civilite'=> $_POST['civilite'],
'prenom'=> $_POST['prenom'],
'nom'=> $_POST['nom'],
'email'=> $_POST['email'],
'tel_fixe'=> $_POST['tel_fixe'],
'tel_mobile'=> $_POST['tel_mobile'],
'fax'=> $_POST['fax'],
'id_etablissement_commanditaire'=> $_POST['id_etablissement_commanditaire'] ,
'id_etablissement_payeur'=> $_POST['id_etablissement_payeur']);
$this->db->insert('contacts', $donnees);
If I add an empty record CodeIgniter says :
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (`france-medical-transport`.`contacts`, CONSTRAINT `contacts_ibfk_2` FOREIGN KEY (`id_etablissement_payeur`) REFERENCES `etablissements_payeurs` (`id_etablissement_payeur`) ON DELETE NO ACTION )
INSERT INTO `contacts` (`service`, `civilite`, `prenom`, `nom`, `email`, `tel_fixe`, `tel_mobile`, `fax`, `id_etablissement_commanditaire`, `id_etablissement_payeur`) VALUES ('', '', '', '', '', '', '', '', '', '')
Try changing your DB insert script to not include the columns at all if they have no value:
$donnees = array(
'service'=> $_POST['service'],
'civilite'=> $_POST['civilite'],
'prenom'=> $_POST['prenom'],
'nom'=> $_POST['nom'],
'email'=> $_POST['email'],
'tel_fixe'=> $_POST['tel_fixe'],
'tel_mobile'=> $_POST['tel_mobile'],
'fax'=> $_POST['fax']);
if ($this->input->post('id_etablissement_commanditaire')) $donnees['id_etablissement_commanditaire'] = $this->input->post('id_etablissement_commanditaire');
if ($this->input->post('id_etablissement_payeur')) $donnees['id_etablissement_payeur'] = $this->input->post('id_etablissement_payeur');
$this->db->insert('contacts', $donnees);
This will allow MySQL to insert the default NULL value without getting confused by any values you're trying to pass in, like an explicit NULL or an empty string.
A good way to do this is :
$this->db->set('id_etablissement_commanditaire' , $_POST['id_etablissement_commanditaire'] , FALSE );
$this->db->set('id_etablissement_payeur' , $_POST['id_etablissement_payeur'] , FALSE );
$this->db->insert('contacts');
This way, the last argument FALSE, means, NULL values won't be escaped, then I have NULL and not 'NULL'.
It works.
Nota : FALSE remove '', but it removes `` as well.
Hi I have the following piece of code which is supposed to upload a csv file and add the information to the database. even though it gets to the echo import complete no data is being added to the table.
copy($_FILES["fileCSV"]["tmp_name"],"shotdev/".$_FILES["fileCSV"]["name"]); // Copy/Upload CSV
$objCSV = fopen("shotdev/".$_FILES["fileCSV"]["name"], "r");
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$strSQL = "INSERT INTO customer_list ";
$strSQL .="(company_name,website,owner,email_addredd,client_id) ";
$strSQL .="VALUES ";
$strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."' ";
$strSQL .=",'".$objArr[3]."','".$objArr[4]."','".$objArr[5]."') ";
$objQuery = mysql_query($strSQL);
}
fclose($objCSV);
echo "Import completed.";
This is my table structure
customer_list` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`company_name` varchar(100) NOT NULL,
`website` varchar(100) NOT NULL,
`owner` varchar(100) NOT NULL,
`email_addredd` varchar(200) NOT NULL,
`client_id` int(10) NOT NULL,
PRIMARY KEY (`id`)
)
I am not exactly sure what is wrong, but you should be testing the value of $objQuery. If it is false, you should echo mysql_error() and die. That is the standard way to call mysql_query().
why I'm getting this?
Error: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'favorites' in field list is ambiguous
SQL Query: UPDATE `twitter`.`tractions` AS `Traction` LEFT JOIN `twitter`.`profiles` AS `Profile` ON (`Traction`.`profile_id` = `Profile`.`id`) SET `Traction`.`favorites` = `favorites` + 1, `Traction`.`errors` = `errors` + 0 WHERE `Traction`.`profile_id` = 4 AND `Traction`.`date` = '2013-01-11'
CakePHP code:
$this->Traction->updateAll(array(
"Traction.favorites"=>"`favorites` + $favorites",
"Traction.errors"=>"`errors` + $errors"
), array(
'Traction.profile_id'=>$profile['Profile']['id'],
'Traction.date'=>date('Y-m-d')
));
-- Table structure for table tractions
CREATE TABLE IF NOT EXISTS `tractions` (
`id` int(10) NOT NULL auto_increment,
`date` date default NULL,
`profile_id` int(10) default NULL,
`followings` int(10) default '0',
`unfollowings` int(10) default '0',
`favorites` int(10) default '0',
`retweets` int(10) default '0',
`thanks` int(10) default '0',
`errors` int(10) default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
you need to specify the tableName, since multiple tables contains the same column name,
"Traction.favorites"=>"Traction.`favorites` + $favorites"
Apparently both of the tables contain the column favorites, might also contain the column errors
As you are joining a second table you probably want to set Traction.favorites = Profile.favorites + $favorites and Traction.errors = Profile.errors + $errors
As said by JW. you need to use a full identifier
if u Use in WhereHas
Example
enter code here $courses=Course::where('status',1)
->with('categories')
->whereHas('categories',function ($query)use ($request){
return $query->where('categories.id',$request->categories_id);
})->get();
if u have filter Course by Categoriy_id
use 'relationname'=> for example categories
u must call categories.id instead of id
I'm trying to SELECT, and get a unique result set, from a MySQL database, as shown below. My problem is, I think, I don't understand LEFT Joins well enough. Or, maybe I need to use a different Join approach.
Here's a description of the database.
tbAdult (Adults) have x number of tbchild (Children) , and uses a cross-ref table called tbadultchildxref. This table has an f-key to both Adult and Child. I have to use an x-ref table, because there's a many-to-many relationship between these two tables, and there's other data that's keep in the x-ref, which I have removed for simplicity.
In turn, each Child belongs to a Program (tblprogram).
Each Program has x number of Cameras (tblCamera). Again, I have to use an x-ref table between tblProgram and tblCamera due to a many-to-many relationship, and other reasons.
What I am trying to get at, is a unique list of Cameras for a given Parent.
For example, Parent 675 has three children, Child ID's 789,788, and 789. Those three children, in turn, belong to Program ID's 4, 5, and 6.
Program ID 4 has Camera ID's 1,2,3
Program ID 5 has Camera ID's 4,5,6
Program ID 6 has Camera ID's 1,6,7,8
What I would like the result set to be is 1,2,3,4,5,6,7,8
I have tried different combinations of SELECT DISTINCT, LEFT JOINS on the various x-ref tables, etc. but I just can't seem to get it.
My other problem, along the way, is I need to check the "Active" fields in Adult, Child, and Program to equal = 1 (true) for the result set.
Thanks in advance.
CREATE TABLE `tbladult` (
`pkAdultID` int(11) NOT NULL AUTO_INCREMENT,
`fldAdultActive` tinyint(1) DEFAULT '1',
`fldAdultLogin` varchar(30) DEFAULT NULL,
`fldAdultPassword` varchar(45) DEFAULT NULL,
`fldAdultFirstName` varchar(60) DEFAULT NULL,
`fldAdultLastName` varchar(60) DEFAULT NULL,
PRIMARY KEY (`pkAdultID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
/*Table structure for table `tblchild` */
CREATE TABLE `tblchild` (
`pkChildID` int(11) NOT NULL AUTO_INCREMENT,
`fldChildActive` tinyint(4) DEFAULT NULL,
`fldChildFirstName` varchar(45) DEFAULT NULL,
`fldChildLastName` varchar(45) DEFAULT NULL,
`fkChildProgram` int(1) DEFAULT NULL,
PRIMARY KEY (`pkChildID`),
KEY `FK_tblchild` (`fkChildProgram`),
CONSTRAINT `FK_tblchild` FOREIGN KEY (`fkChildProgram`) REFERENCES `tblprogram` (`pkProgramID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
/*Table structure for table `tbladultchildxref` */
CREATE TABLE `tbladultchildxref` (
`pkAdultChildxRefID` int(11) NOT NULL AUTO_INCREMENT,
`fldAdultChildxRefActive` tinyint(1) DEFAULT '1',
`fkAdultID` int(11) DEFAULT NULL,
`fkChildID` int(11) DEFAULT NULL,
PRIMARY KEY (`pkAdultChildxRefID`),
KEY `FK_tbladultchildxref` (`fkAdultID`),
KEY `FK_tbladultchildxref2` (`fkChildID`),
CONSTRAINT `FK_tbladultchildxref` FOREIGN KEY (`fkAdultID`) REFERENCES `tbladult` (`pkAdultID`),
CONSTRAINT `FK_tbladultchildxref2` FOREIGN KEY (`fkChildID`) REFERENCES `tblchild` (`pkChildID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
/*Table structure for table `tblprogram` */
CREATE TABLE `tblprogram` (
`pkProgramID` int(11) NOT NULL AUTO_INCREMENT,
`fldProgamActive` tinyint(1) DEFAULT '1',
`fldProgramName` varchar(50) DEFAULT NULL,
PRIMARY KEY (`pkProgramID`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
/*Table structure for table `tblcamera` */
CREATE TABLE `tblcamera` (
`pkCameraID` int(11) NOT NULL AUTO_INCREMENT,
`fldCameraName` varchar(50) DEFAULT NULL,
`fldCameralocation` varchar(50) DEFAULT NULL,
`fldCameraURL` varchar(250) DEFAULT NULL,
PRIMARY KEY (`pkCameraID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
/*Table structure for table `tblprogramcameraxref` */
CREATE TABLE `tblprogramcameraxref` (
`pkProgramCameraXrefID` int(11) NOT NULL AUTO_INCREMENT,
`fkProgramID` int(11) DEFAULT NULL,
`fkCameraID` int(11) DEFAULT NULL,
PRIMARY KEY (`pkProgramCameraXrefID`),
KEY `FK_tblprogramcameraxref` (`fkProgramID`),
KEY `FK_camerasforprograms` (`fkCameraID`),
CONSTRAINT `FK_camerasforprograms` FOREIGN KEY (`fkCameraID`) REFERENCES `tblcamera` (`pkCameraID`),
CONSTRAINT `FK_tblprogramcameraxref` FOREIGN KEY (`fkProgramID`) REFERENCES `tblprogram` (`pkProgramID`)
No LEFT JOINs necessary:
SELECT DISTINCT tblprogramcameraxref.fkcameraid
FROM tblprogramcameraxref
JOIN tblprogram ON tblprogramcameraxref.fkprogramid = tblprogram.pkprogramid
AND tblprobram.fldProgramActive = 1
JOIN tblchild ON tblprogramcameraxref.fkprogramid = tblchild.fkchildprogram
AND tblchild.fldChildActive = 1
JOIN tbladultchildxref ON tblchild.pkchildid = tbladultchildxref.fkchildid
AND tbladultchildxref.fldAdultChildxRefActive = 1
WHERE tbladultchildxref.fkadultid = 675
Also, you may want to check the fkChildProgram int(1) DEFAULT NULL, in tblchild - the column it references is defined as int(11)
At this point you shouldn't really need to check if Adult is active (since that's the search criteria you started with), but if you must - just add this to the end of the join list:
JOIN tbladult ON tbladultchildxref.fkadultid = tbladult.pkadultid
AND tbladult.fldAdultActive = 1
It is a long description. If I have understood the question correctly this query should help you -
SELECT DISTINCT pcref.fkCameraID
FROM tbladult adult,
tblchild child,
tbladultchildxref acref,
tblprogram prog,
tblcamera camera,
tblprogramcameraxref pcref
WHERE adult.pkAdultID = 675
AND adult.fldAdultActive = TRUE
AND adult.pkAdultID = acref.fkAdultID
AND acref.fkChildID = child.pkChildID
AND child.fldChildActive = TRUE
AND child.fkChildProgram = prog.pkProgramID
AND prog.fldProgamActive = TRUE
AND prog.pkProgramID = pcref.fkProgramID