Creating database table when installing a wordpress plugin - php

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

Related

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.

PDO update query runs changes no rows. No errors

So I'm running a PDO update working, and for some reason it won't update the table...
$business_id = 9874128;
$hidden = 1;
$query = "UPDATE business_property_overrides SET hidden=? WHERE business_id=?";
try {
$stmt = $pdo->prepare($query);
$stmt->execute(array($business_id, $hidden));
}
For some reason this won't update, even though I get no errors. The existing tables schema looks like this, and the data is:
There is an existing data set with business_id = 9874128 and hidden set to 0, but it won't update when I run the above code.
CREATE TABLE `business_property_overrides` (
`business_id` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(512) NOT NULL,
`apt_type` varchar(25) DEFAULT NULL,
`apt_num` varchar(9) DEFAULT NULL,
`street_address` varchar(255) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL,
`state` varchar(255) DEFAULT NULL,
`zip` varchar(25) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`url` varchar(512) DEFAULT NULL,
`hours` varchar(100) DEFAULT NULL,
`openhours` varchar(100) DEFAULT NULL,
`location` point DEFAULT NULL,
`yelp` varchar(512) DEFAULT '0',
`twitter` varchar(512) DEFAULT '0',
`hidden` tinyint(1) DEFAULT '0',
`merged` int(11) DEFAULT NULL,
`closed` tinyint(1) DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `business_id` (`business_id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9874134 DEFAULT CHARSET=utf8;
The hidden is TINYINT 1 characters long, you are assigning it business_id which is 7 characters long, that is the error.
Change
$stmt->execute(array($business_id, $hidden));
To:
$stmt->execute(array($hidden,$business_id))
As I've already commented over here, or you can simply use the placeholders of taking no care about the occurence like as
$query = "UPDATE business_property_overrides SET hidden = :hidden WHERE business_id = :business_id";
try {
$stmt = $pdo->prepare($query);
$stmt->execute(array(":business_id" => $business_id, ":hidden" => $hidden));
}

Create tables in database from queries in a text file

I have the following PHP page to create a table using a text file.
table_create.php
<?php
include $db;
$query_file = "sql.txt";
$fp = fopen($query_file, 'r');
$sql = fread($fp, filesize($query_file));
fclose($fp);
$retval = mysql_query($sql);
if(! $retval )
{
die("Could not create the tables<br>");
}
echo "Table created successfully<br>";
?>
sql.txt
CREATE TABLE ht_account (
id int(11) NOT NULL AUTO_INCREMENT,
date date NOT NULL,
type varchar(50) NOT NULL,
mode varchar(50) NOT NULL,
party varchar(50) NOT NULL,
payee varchar(50) NOT NULL,
rate decimal(13,2) NOT NULL,
box int(11) NOT NULL,
amount decimal(13,2) NOT NULL,
token varchar(50) NOT NULL,
remarks varchar(50) NOT NULL,
user varchar(50) NOT NULL,
user_confirm varchar(50) NOT NULL,
status varchar(50) NOT NULL);
CREATE TABLE ht_bank (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
ac_no varchar(50) NOT NULL,
address varchar(50) NOT NULL);
CREATE TABLE ht_user_role (
id int(11) NOT NULL AUTO_INCREMENT,
value varchar(50) NOT NULL);
When I try to create a single table in the sql.txt file, the code works perfectly.
For example:
CREATE TABLE ht_account (
id int(11) NOT NULL AUTO_INCREMENT,
date date NOT NULL,
type varchar(50) NOT NULL,
mode varchar(50) NOT NULL,
party varchar(50) NOT NULL,
payee varchar(50) NOT NULL,
rate decimal(13,2) NOT NULL,
box int(11) NOT NULL,
amount decimal(13,2) NOT NULL,
token varchar(50) NOT NULL,
remarks varchar(50) NOT NULL,
user varchar(50) NOT NULL,
user_confirm varchar(50) NOT NULL,
status varchar(50) NOT NULL);
But when I try to create multiple tables, It does not create any table. I doubt that the format in the sql.txt may be incorrect.
The format is, almost sure, correct but mysql_query doesn't work with multiple queries:
mysql_query() sends a unique query (multiple queries are not
supported) to the currently active database on the server that's
associated with the specified link_identifier.
It's better to use mysqli functions because mysql ones are deprecated for PHP 5.5 and mysqli has the function mysqli_multi_query that you need.
If you still want to use mysql functions you could do something like:
$sql_array=explode(';',$sql);
foreach ($sql_array as $s) {
if(! mysql_query($s)){
echo mysql_error()."<br>";
}
}

MySQL delete troubleshooting

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.

Error fetching row from database with PHP

I am trying to fetch a row from a table in my database, and everything is retrieved successfully except for one column (page_content), whose data comes in partially. Here is the table creation code:
CREATE TABLE 'usol_site_page' (
'page_id' int(11) NOT NULL auto_increment,
'page_type_id' int(11) NOT NULL,
'page_menu_id' int(11) NOT NULL,
'page_name' varchar(100) NOT NULL,
'page_link' varchar(100) NOT NULL,
'page_heading' varchar(255) default '',
'page_content' text,
'page_title' varchar(255) default NULL,
'meta_keywords' mediumtext,
'keyword_description' text,
'pic_small' varchar(255) default NULL,
'pic_main' varchar(255) default NULL,
'pic_size' varchar(50) default NULL,
'pic_type' varchar(50) default NULL,
'display_order' int(11) NOT NULL,
'parent_page_id' int(11) NOT NULL,
'status' varchar(20) NOT NULL,
'creation_date' date NOT NULL,
'last_update_date' date NOT NULL,
PRIMARY KEY ('page_id'),
KEY 'Refusol_page_menu60' ('page_menu_id'),
KEY 'Refusol_page_type44' ('page_type_id'),
CONSTRAINT 'Refusol_page_menu60' FOREIGN KEY ('page_menu_id') REFERENCES 'usol_page_menu' ('page_menu_id') ON DELETE CASCADE,
CONSTRAINT 'Refusol_page_type44' FOREIGN KEY ('page_type_id') REFERENCES 'usol_page_type' ('page_type_id') ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=67 DEFAULT CHARSET=latin1
Does anyone know why page_content is not being fully retrieved?
here is the code the query :
$extendQry = "";
if($mode != "admin"){
$extendQry = "AND p.status='enable' ";
}
$qry = "SELECT p.page_id, p.page_link, p.page_name, p.page_heading, p.page_content, p.status, p.page_title,
p.meta_keywords, p.keyword_description, p.pic_main, p.pic_small, p.parent_page_id,
t.page_type_id, t.page_type, m.page_menu_id, m.menu_type
FROM usol_site_page p, usol_page_type t , usol_page_menu m
WHERE p.page_id = ".$pageId." $extendQry
AND t.page_type_id = p.page_type_id
AND m.page_menu_id = p.page_menu_id";
$result = mysql_query($qry);
return mysql_fetch_row($result);
fellas thank you very much for your quick responce sorry for my very unclear question, i got it solved by changing datatype of the column (page_content) from 'text' (64KB) to mediumtext(16MB). and it is working perfectly now. thank you all for your kind support you guyz are the best.

Categories