I have been creating Wordpress plugin for a while. This is example of mysql table:
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id INT(11) NOT NULL AUTO_INCREMENT,
email VARCHAR(100) DEFAULT NULL,
telephone VARCHAR(15) DEFAULT NULL,
PRIMARY KEY(id),
UNIQUE (email, telephone)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='WP plugin sesa_players db' AUTO_INCREMENT=1 ;
";
Email should be unique, right? phpMyAdmin says it it.
This is wordpress code that inserts data into that table:
$err = $wpdb->insert($wpdb->prefix.$table_name, $data, $format);
var_dump($err);
It works, even more than it should. Assume email is m#m.com. First insert goes well. Second try fails because of duplicate entry as it should. var_dump is false.
BUT if I refresh wp page, third try with same email passes flawlessly, var_dump 1. Any repeated wp refresh opens db for duplicate entry.
Why? What am I doing wrong?
No, email is not UNIQUE here. Pair of email and telephone is UNIQUE in your table definition.
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id INT(11) NOT NULL AUTO_INCREMENT,
email VARCHAR(100) DEFAULT NULL,
telephone VARCHAR(15) DEFAULT NULL,
PRIMARY KEY(id),
UNIQUE (email),
UNIQUE (telephone)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='WP plugin sesa_players db' AUTO_INCREMENT=1 ;
";
Probably this is what you want.
Related
I am creating a visitor counter for each my pages. According to PageId and visitor Ip, I created two tables such as pageView and pageTotalView. But my code is not working. I found some codes in php. How do I convert these codes to Codeigniter in working?
Database Tables:
CREATE TABLE `pageView` (
`pageViewId` int(11) NOT NULL AUTO_INCREMENT,
`pageViewPageId` int(11) NULL,
`pageViewUserIP` text NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
// totalview
CREATE TABLE `pageTotalView` (
`pageTotalViewId` int(11) NOT NULL AUTO_INCREMENT,
`pageTotalViewPId` int(11) NULL,
`pageTotalVisit` text NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
PHP Codes:
// gets the user IP Address
$pageViewUserIP = $_SERVER['REMOTE_ADDR'];
$check_ip = mysql_query("select pageViewUserIP from pageViewwhere pageViewPageId = 'I dont know How I can get viewing Page Id' and pageViewUserIP = '$pageViewUserIP'");
if(mysql_num_rows($check_ip)>=1)
{
}
else
{
$insertview = mysql_query("insert into pageView values('','I dont know How I can get viewing Page Id','$pageViewUserIP')");
$updateview = mysql_query("update pageTotalView set pageTotalVisit = pageTotalVisit+1 where pageViewPageId ='I dont know How I can get viewing Page Id' ");
}
To sum up, these codes maybe working on php, but How can I use these codes in Codeigniter?
I have a simple ACL system in PHP and MYSQL started. I need help finishing it though...
I have 2 Database tables shown below...
user_link_permissions : Holds a record for every user, on every entity/link that permissions apply to...
--
-- Table structure for table `user_link_permissions`
--
CREATE TABLE IF NOT EXISTS `user_link_permissions` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`user_id` int(30) NOT NULL,
`link_id` int(30) NOT NULL,
`permission` int(2) NOT NULL DEFAULT '0',
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2055 ;
intranet_links : Is basically the entity that the permission gives or revokes user access to
--
-- Table structure for table `intranet_links`
--
CREATE TABLE IF NOT EXISTS `intranet_links` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`description` text NOT NULL,
`url` varchar(255) DEFAULT NULL,
`notes` text,
`user_login` varchar(255) DEFAULT NULL,
`user_pw` varchar(255) DEFAULT NULL,
`active` int(2) NOT NULL DEFAULT '1',
`sort_order` int(11) DEFAULT NULL,
`parent` int(10) NOT NULL DEFAULT '1',
`local_route` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
UNIQUE KEY `local_route` (`local_route`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=34 ;
To save these permissions settings I have a matrix style grid like this below where each checkbox is a record in the user_link_permissions table...
I need help creating a simple ACL function in PHP which can check if a user has permission or not to view a link/entity based on the database results.
On page load I am thinking I can query the user_link_permissions DB table for all records with a matching user ID of the logged in user and store them to a session array variable.
A function could then use that array to check for a link/entity permission using that array value on the entity key.
I just can't visualize how it might look at the moment in PHP.
Any help please?
function aclCanAccess($user_id, $entity_id){
}
$entity_id = 123;
if(aclCanAccess(1, $entity_id){
// yes user can see this item
}else{
// NO user permission denied
}
I will leave writing the code to you for fun.
Assume you are storing all the previously queried permissions in a variable called $_SESSION['acl']
Your ACL function should:
check the session if you already queried that entity
if it is not set, read it from the db
in short
function..... {
if(!isset($_SESSION['acl'][$entity_id])) {
$_SESSION['acl'][$entity_id] = query here to return to you if he has access or not
}
return $_SESSION['acl'][$entity_id];
}
You can also read the entire array when you log in the user. That might also be appropriate. In that case you should be able to just
return $_SESSION['acl'][$entity_id];
But I would then try and catch an exception in case it is not set.
I have a weird issue, well, weird in my eyes anyway.
I've got a database with ID, username, email, password etc...
The ID is the Primary key, and both the username and email have the UNIQUE key assigned.
Now the strange thing is, when I submit, lets say the following values;
username: ActionHank
email: ah#ah.com
it is added.
Now when I try adding the same values again, I get an error that it is not allowed since it would be a duplicate entry. This works great.
But, when I put in the following next;
username: ActionHank2
email: ah#ah.com
It just adds it again, so 2 users have the same e-mail address while I've given the email row the UNIQUE key. I could also just change the e-mail address instead of the username and it will be added. So somehow the MySQL only registers one of the UNIQUE keys or something, I'm confused.
Could someone help me on this matter? Thanks
Edit: I've used phpMyAdmin to create the table and exported it to SQL, here's how the create table looks:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(25) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`date_registered` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ip_address` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`,`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=54 ;
So I think it has a composite as Michael and Kingkero suggested. How can I solve this within phpMyAdmin?
You have defined the combination of username and email to be unique. If you want each to be unique, you need to define separate unique constraints:
create table . . .
unique username,
unique email
)
Or, because these are single columns, you can just do:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` varchar(25) NOT NULL UNIQUE,
`email` varchar(255) NOT NULL UNIQUE,
`password` varchar(255) NOT NULL,
`date_registered` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ip_address` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=54 ;
Try this:
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
Im using phpmyadmin and I have a table categories with 4 fields:
-> id
-> id_father_category (I put this with predefined: NULL) and I also put a checkbox Null
-> name
-> content
I want to give null values to my id_father_category field.
But Im having this error:
Warning: #1366 Incorrect integer value: '' for column 'id_father_category field' at row 1
And my field id_father_category stays automatically with value 0, and I dont want that.
Somebody there knows how I can solve this problem?
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_father_category` int(11) DEFAULT NULL,
`name` varchar(64) NOT NULL,
`content` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Hope this will solve your problem...
This is an addition to #Nirjhor's answer.
ALTER TABLE categories MODIFY id_father_category INT(11) DEFAULT NULL;
or you can recreate the whole thing:
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_father_category` int(11) DEFAULT NULL,
`name` varchar(64) NOT NULL,
`content` varchar(256) NOT NULL,
PRIMARY KEY (`id`) )
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Setting the default value to NULL will handle your issue as well as set the field to NULL. One thing to note here is to make sure you don't have some weird index on that field cause that can also create an issue.
I have been struggling since this morning, trying to write an install.php file that will insert three empty tables (users, sessions, posts) into a database. The SQL code I'm using is valid when I inject it with PHPMyAdmin, but apparently the way I'm handling it in PHP is wrong, because when I run my install.php file my database remains empty. Here's my code:
<?php
try {
$con = new PDO('mysql:host=omitted;dbname=omitted','omitted','');
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$q="CREATE TABLE posts (
title varchar(150) NOT NULL,
body text NOT NULL,
created varchar(100) NOT NULL,
user varchar(40) NOT NULL,
id int(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE sessions (
session_id varchar(40) NOT NULL,
data text NOT NULL,
last_activity int(11) NOT NULL,
PRIMARY KEY (session_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(40) NOT NULL,
password varbinary(250) NOT NULL,
email varchar(40) NOT NULL,
salt varchar(20) NOT NULL,
name varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (id),
UNIQUE KEY email (email),
UNIQUE KEY username (username),
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ";
$stmt = $con->prepare($q);
$stmt->execute();
echo "success";
} catch (PDOException $e) {
$e->getMessage();
}
?>
I am assuming that it's some idiotic mistake on my part (since my last 3 headaches were also quite silly) but at this moment I really can't figure it out. Any ideas?
You can prepare only one statement per prepare call. To execute multiple statements you have to use PDO::exec method.
For example:
$con = new PDO('mysql:host=localhost;dbname=db','user','pass');
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$q="CREATE TABLE posts (
title varchar(150) NOT NULL,
body text NOT NULL,
created varchar(100) NOT NULL,
user varchar(40) NOT NULL,
id int(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE sessions (
session_id varchar(40) NOT NULL,
data text NOT NULL,
last_activity int(11) NOT NULL,
PRIMARY KEY (session_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(40) NOT NULL,
password varbinary(250) NOT NULL,
email varchar(40) NOT NULL,
salt varchar(20) NOT NULL,
name varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (id),
UNIQUE KEY email (email),
UNIQUE KEY username (username),
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ";
try {
$con->exec($q) or die(print_r($db->errorInfo(), true));;
echo "Success";
} catch (PDOException $e) {
$e->getMessage();
}
This is not a query but rather set of queries.
Just run them one by one with separate calls.