Mysql queries works fine in local host but not in server - php

I am trying to upload some images to my database with some data, when I do this in localhost it's all work fine. But on the server, only some queries work fine. but others are not working.
This is my database pages table and papertag table
CREATE TABLE `pages`
(
`pageId` int(10) NOT NULL,
`paperId` varchar(255) NOT NULL,
`filePath` varchar(255) NOT NULL,
`fileType` varchar(20) NOT NULL,
`status` enum('0','1') NOT NULL,
`dateAndTime` datetime NOT NULL,
`description` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `pages`
--
ALTER TABLE `pages`
ADD PRIMARY KEY (`pageId`),
ADD KEY `paperId` (`paperId`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `pages`
--
ALTER TABLE `pages`
MODIFY `pageId` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=324;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `pages`
--
ALTER TABLE `pages`
ADD CONSTRAINT `pages_ibfk_1` FOREIGN KEY (`paperId`) REFERENCES `papers` (`paperId`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
-----paper tag------
CREATE TABLE `papertag`
(
`tagId` int(11) NOT NULL,
`paperId` varchar(255) NOT NULL,
`tagName` varchar(255) NOT NULL,
`des` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `papertag`
--
ALTER TABLE `papertag`
ADD PRIMARY KEY (`tagId`),
ADD KEY `paperId` (`paperId`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `papertag`
--
ALTER TABLE `papertag`
MODIFY `tagId` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=460;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `papertag`
--
ALTER TABLE `papertag`
ADD CONSTRAINT `papertag_ibfk_1` FOREIGN KEY (`paperId`) REFERENCES `papers` (`paperId`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
and this is my SQL query code in PHP file
$up22 = mysqli_query($connection, "INSERT INTO papertag(`paperId`, `tagName`) VALUES ('{$paperId}', '{$nme}')");
$up = mysqli_query($connection, "INSERT INTO pages(`paperId`, `filePath`, `fileType`, `status`, `dateAndTime`) VALUES ('{$paperId}', '{$img_dir}', '{$type}',2 , '{$date}' )");

I think you have to insert all columns with not null Constraint. you did not insert des column for example.
you need this code :
$up22 = mysqli_query($connection, "INSERT INTO papertag(`paperId`, `tagName`,`des`) VALUES ('{$paperId}', '{$nme}'),'{$des}'");

Related

Show all user posts in one page but query is too slow

I try to show the videos and pictures shared by the members in one page. The query I wrote works fine but is very slow. I don't know why this happened. That's why I need your help. You can also see the conditions I wrote in the following query.
Here is the sqlFiddle
$morequery="";
if($lastpostid) {
$morequery=" AND P.user_post_id<'".$lastpostid."' ";
}
$GetAllPostQuery = mysqli_query($this->db,"SELECT
P.user_post_id,
P.user_id_fk,P.post_type,
P.who_can_see_post,
P.post_image_id,P.post_video_id,
P.post_video_name,
U.user_name, U.user_fullname,U.influencer_status
FROM user_posts P
INNER JOIN users U
ON P.user_id_fk = U.user_id
WHERE
U.user_status='1' AND
U.influencer_status = '1' AND
(P.who_can_see_post IN('everyone','influencer','friends')) AND
(P.post_type IN('image','video')) $morequery
ORDER BY
P.user_post_id
DESC LIMIT " .$this->perpage) or die(mysqli_error($this->db));
//Store the result
while($row=mysqli_fetch_array($GetAllPostQuery)) {
// Store the result into array
$data[]=$row;
}
if(!empty($data)) {
// Store the result into array
return $data;
}
The Users Table Here:
-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: localhost:8889
-- Generation Time: Sep 10, 2019 at 11:56 AM
-- Server version: 5.7.23
-- PHP Version: 7.2.10
CREATE TABLE `users` (
`user_id` int(11) NOT NULL,
`user_name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`user_status` enum('0','1','2','3') NOT NULL DEFAULT '0',
`user_fullname` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`influencer_status` enum('0','1') NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
And The User Posts Table Here:
CREATE TABLE `user_posts` (
`user_post_id` int(11) NOT NULL,
`user_id_fk` int(11) DEFAULT NULL,
`post_type` enum('text','image','link’,’video','audio','avatar','cover','gif','location','watermark','which','page','event','blog','group','product','bfaf','inf') NOT NULL DEFAULT 'text',
`post_created_time` int(11) NOT NULL DEFAULT '1524910573',
`who_can_see_post` enum('everyone','onlyme','friends','influencer') NOT NULL DEFAULT 'everyone',
`post_video_id` varchar(255) DEFAULT NULL,
`post_video_name` varchar(255) DEFAULT NULL,
`post_audio_id` varchar(255) DEFAULT NULL,
`post_image_id` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for table `user_posts`
--
ALTER TABLE `user_posts`
ADD PRIMARY KEY (`user_post_id`),
ADD KEY `ex_posts` (`user_id_fk`);
--
-- AUTO_INCREMENT for table `user_posts`
--
ALTER TABLE `user_posts`
MODIFY `user_post_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
--
-- Constraints for table `user_posts`
--
ALTER TABLE `user_posts`
ADD CONSTRAINT `ex_posts` FOREIGN KEY (`user_id_fk`) REFERENCES `users` (`user_id`);
What should I do to make the query work faster? Can you help me.
The fiddle contains too few rows to reproduce the problem but these 2 indexes might help.it really depends on your data distribution.
ALTER TABLE users ADD KEY(user_status,influencer_status);
ALTER TABLE user_posts ADD KEY(who_can_see_post,post_type);

SQL SELECT id WHERE emails are the same

I do not know if it's possible at all, but I have two tables, userBasic and carPlateConfidence, in carPlateConfidence I would like to insert id of userBasic where emails are matched.
$query .= "INSERT IGNORE INTO userBasic (id_uM, userNameG, userEmailG) values ((SELECT id_uM FROM userMore WHERE userEmailG='$userEmailG'),'$userNameG', '$userEmailG');";
$query .= "INSERT IGNORE INTO carPlateConfidence (emailConfid, id_uB,plateNumber, confidencePlate, plateNumberUn) values ('$userEmailG', (SELECT id_uB FROM userBasic WHERE userEmailG='(SELECT max(emailConfid) FROM carPlateConfidence)'), '$plateNumber','$confidencePlate', '$plateNumberUn');";
So if I have:
userBasic:
id_uM = 555;
userNameG = BlaBla;
userEmailG = blabla#blabla.com
And in this table I would like
carPlateConfidence:
emailConfid = blabla#blabla.com;
id_uB = 555
plateNumber = 1111
confidencePlate = 70
plateNumberUn = 2222
AND if email do not matched:
emailConfid = blabla2#blabla.com;
id_uB = NULL
plateNumber = 1111
confidencePlate = 70
plateNumberUn = 222
P>S> Currently I have tried this, to select id from userBasic:
(SELECT id_uB FROM userBasic WHERE userEmailG='(SELECT max(emailConfid) FROM carPlateConfidence)')
id_uB in carPlateConfidence is set as foreign key;
Tables:
--
-- Table structure for table `carPlateConfidence`
--
DROP TABLE IF EXISTS `carPlateConfidence`;
CREATE TABLE IF NOT EXISTS `carPlateConfidence` (
`id_cof` int(11) NOT NULL AUTO_INCREMENT,
`id_uB` int(11) NOT NULL,
`emailConfid` varchar(50) NOT NULL,
`plateNumber` varchar(10) NOT NULL,
`confidencePlate` varchar(10) DEFAULT NULL,
`plateNumberUn` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id_cof`),
KEY `id_uB` (`id_uB`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
-- --------------------------------------------------------
--
-- Table structure for table `userBasic`
--
DROP TABLE IF EXISTS `userBasic`;
CREATE TABLE IF NOT EXISTS `userBasic` (
`id_uB` int(11) NOT NULL AUTO_INCREMENT,
`id_uM` int(11) NOT NULL,
`userNameG` varchar(50) NOT NULL,
`userEmailG` varchar(50) NOT NULL,
PRIMARY KEY (`id_uB`),
UNIQUE KEY `userEmailG` (`userEmailG`),
KEY `id_uM` (`id_uM`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=119 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `carPlateConfidence`
--
ALTER TABLE `carPlateConfidence`
ADD CONSTRAINT `carPlateConfidence_ibfk_1` FOREIGN KEY (`id_uB`) REFERENCES `userBasic` (`id_uB`);
--
-- Constraints for table `userBasic`
--
ALTER TABLE `userBasic`
ADD CONSTRAINT `userBasic_ibfk_1` FOREIGN KEY (`id_uM`) REFERENCES `userMore` (`id_uM`);
So you want an update, not an insert :
UPDATE carPlateConfidence t
SET t.id_uB = (SELECT distinct s.id_uM FROM userBasic s
WHERE s.userEmailG = t.emailConfid)
This will work only if there can be only 1 match, if there can be more then one match you should specify which one you want, if it doesn't matter, either use MAX() or limit :
UPDATE carPlateConfidence t
SET t.id_uB = (SELECT max(s.id_uM) FROM userBasic s
WHERE s.userEmailG = t.emailConfid)

SQL insert and update doesn't update or insert all the records, only a few record

My php code:
for($i=0;$i<num_rows($sql);$i++)
if(isset($_POST['knight'.$i]))
{
$data=
[
$pm_id=$_POST['pm_id'.$i],
$knight_id=$_POST['knight'.$i],
];
mysql_query("update `project_waiting` set `chosen`=(b'1') where `pm_id`='$pm_id' and `knight_id`='$knight_id';");
mysql_query("insert into `project_working`(`pm_id`,`knight_id`) values('$pm_id','$knight_id');");
}
when I use echo instead of mysql_query it returns:
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K1';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K1');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K2';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K2');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K3';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K3');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K4';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K4');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_2' and `knight_id`='K1';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_2','K1');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_2' and `knight_id`='K4';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_2','K4');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_3' and `knight_id`='K2';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_3','K2');
It runs perfectly on my phpmyadmin, but when I change echo to mysql_query it only affect 5 rows (both tables)
P1_1-K1
P1_1-K4
P1_1-K3
P1_1-K2
P1_2-K1
My full database
-- phpMyAdmin SQL Dump
-- version 4.3.11
-- http://www.phpmyadmin.net
-- Host: 127.0.0.1
-- Generation Time: Jul 23, 2015 at 12:16 PM
-- Server version: 5.6.24
-- PHP Version: 5.6.8
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: knightit
CREATE DATABASE IF NOT EXISTS knightit DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE knightit;
--
-- Table structure for table admin
DROP TABLE IF EXISTS admin;
CREATE TABLE IF NOT EXISTS admin (
id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
password binary(60) NOT NULL,
level int(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table employers
DROP TABLE IF EXISTS employers;
CREATE TABLE IF NOT EXISTS employers (
emp_id varchar(16) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
password longtext COLLATE utf8_unicode_ci NOT NULL,
email varchar(32) COLLATE utf8_unicode_ci NOT NULL,
emp_name text COLLATE utf8_unicode_ci,
coins bigint(20) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table knights
DROP TABLE IF EXISTS knights;
CREATE TABLE IF NOT EXISTS knights (
knight_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
password longtext COLLATE utf8_unicode_ci NOT NULL,
knight_name text COLLATE utf8_unicode_ci,
email varchar(32) COLLATE utf8_unicode_ci NOT NULL,
s_id varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
points int(11) DEFAULT '0',
coins int(32) NOT NULL DEFAULT '0',
status bit(2) DEFAULT b'1',
bio longtext COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table message
DROP TABLE IF EXISTS message;
CREATE TABLE IF NOT EXISTS message (
m_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
content text COLLATE utf8_unicode_ci NOT NULL,
send_date date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table message_send
DROP TABLE IF EXISTS message_send;
CREATE TABLE IF NOT EXISTS message_send (
m_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
receiver_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
status bit(1) NOT NULL DEFAULT b'0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table projects
DROP TABLE IF EXISTS projects;
CREATE TABLE IF NOT EXISTS projects (
pro_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
pro_name text COLLATE utf8_unicode_ci NOT NULL,
emp_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
p_describe text COLLATE utf8_unicode_ci NOT NULL,
price int(32) NOT NULL,
cut int(32) NOT NULL,
s_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
status bit(2) NOT NULL DEFAULT b'1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table project_milestone
DROP TABLE IF EXISTS project_milestone;
CREATE TABLE IF NOT EXISTS project_milestone (
pm_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
pro_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
milestone int(2) NOT NULL,
job text COLLATE utf8_unicode_ci,
deadline int(3) DEFAULT NULL,
knight_amt int(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table project_waiting
DROP TABLE IF EXISTS project_waiting;
CREATE TABLE IF NOT EXISTS project_waiting (
pm_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
knight_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
chosen bit(1) NOT NULL DEFAULT b'0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table project_working
DROP TABLE IF EXISTS project_working;
CREATE TABLE IF NOT EXISTS project_working (
pm_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
knight_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
submit date DEFAULT NULL,
approve tinyint(1) DEFAULT NULL,
paid tinyint(1) DEFAULT NULL,
coins_paid int(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table security
DROP TABLE IF EXISTS security;
CREATE TABLE IF NOT EXISTS security (
name varchar(100) COLLATE utf8_unicode_ci NOT NULL,
value longtext COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table skills
DROP TABLE IF EXISTS skills;
CREATE TABLE IF NOT EXISTS skills (
s_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
skill_name text COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
-- Indexes for table admin
ALTER TABLE admin
ADD PRIMARY KEY (id);
--
-- Indexes for table employers
ALTER TABLE employers
ADD PRIMARY KEY (emp_id);
--
-- Indexes for table knights
ALTER TABLE knights
ADD PRIMARY KEY (knight_id), ADD KEY s_id (s_id);
--
-- Indexes for table message
ALTER TABLE message
ADD PRIMARY KEY (m_id);
--
-- Indexes for table message_send
ALTER TABLE message_send
ADD KEY m_id (m_id);
--
-- Indexes for table projects
ALTER TABLE projects
ADD PRIMARY KEY (pro_id), ADD KEY emp_id (emp_id), ADD KEY s_id (s_id);
--
-- Indexes for table project_milestone
ALTER TABLE project_milestone
ADD PRIMARY KEY (pm_id), ADD KEY pm_id (pm_id,pro_id,milestone), ADD KEY project_milestone_ibfk_1 (pro_id);
--
-- Indexes for table project_waiting
ALTER TABLE project_waiting
ADD PRIMARY KEY (knight_id,pm_id), ADD KEY knight_id (knight_id), ADD KEY pm_id (pm_id);
--
-- Indexes for table project_working
ALTER TABLE project_working
ADD PRIMARY KEY (pm_id,knight_id), ADD KEY pm_id (pm_id,knight_id), ADD KEY knight_id (knight_id);
--
-- Indexes for table security
ALTER TABLE security
ADD PRIMARY KEY (name);
--
-- Indexes for table skills
ALTER TABLE skills
ADD PRIMARY KEY (s_id);
--
-- Constraints for dumped tables
--
-- Constraints for table knights
ALTER TABLE knights
ADD CONSTRAINT knights_ibfk_1 FOREIGN KEY (s_id) REFERENCES skills (s_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table message_send
ALTER TABLE message_send
ADD CONSTRAINT message_send_ibfk_1 FOREIGN KEY (m_id) REFERENCES message (m_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table projects
ALTER TABLE projects
ADD CONSTRAINT projects_ibfk_1 FOREIGN KEY (emp_id) REFERENCES employers (emp_id) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT projects_ibfk_2 FOREIGN KEY (s_id) REFERENCES skills (s_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table project_milestone
ALTER TABLE project_milestone
ADD CONSTRAINT project_milestone_ibfk_1 FOREIGN KEY (pro_id) REFERENCES projects (pro_id);
--
-- Constraints for table project_waiting
ALTER TABLE project_waiting
ADD CONSTRAINT project_waiting_ibfk_1 FOREIGN KEY (pm_id) REFERENCES project_milestone (pm_id) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT project_waiting_ibfk_2 FOREIGN KEY (knight_id) REFERENCES knights (knight_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table project_working
ALTER TABLE project_working
ADD CONSTRAINT project_working_ibfk_1 FOREIGN KEY (pm_id) REFERENCES project_milestone (pm_id) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT project_working_ibfk_2 FOREIGN KEY (knight_id) REFERENCES knights (knight_id) ON DELETE CASCADE ON UPDATE CASCADE;
I have solved the problem with combine all of them into one query:
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K1'),('valueX','valueY');
update set `chosen`=true where (`pm_id`='P1_3' and `knight_id`='K2') or (`pm_id`='P1_1' and `knight_id`='K1') or(...);

How can i create entities in Doctrine for my already build two tables

Table 1:-
--
-- Table structure for table page
--
DROP TABLE IF EXISTS `page`;
CREATE TABLE IF NOT EXISTS `page` (
`id` int(11) NOT NULL,
`site_id` int(11) NOT NULL,
`keyword_id` int(11) NOT NULL,
`state` char(2) DEFAULT NULL,
`location_group_id` int(11) DEFAULT NULL,
`location_id` int(11) DEFAULT NULL,
`uri` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7194 ;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `page`
--
ALTER TABLE `page`
ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `site_id` (`site_id`,`uri`),
ADD KEY `location_group_id` (`location_group_id`),
ADD KEY `keyword_id` (`keyword_id`), ADD KEY `uri` (`uri`),
ADD KEY `state` (`state`), ADD KEY `location_id` (`location_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `page`
--
ALTER TABLE `page`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7194;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `page`
--
ALTER TABLE `page`
ADD CONSTRAINT `page_ibfk_1` FOREIGN KEY (`site_id`) REFERENCES `site` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `page_ibfk_2` FOREIGN KEY (`keyword_id`) REFERENCES `keyword` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
**ADD CONSTRAINT `page_ibfk_3` FOREIGN KEY (`state`) REFERENCES `state` (`abbreviation`) ON DELETE CASCADE ON UPDATE CASCADE,**
ADD CONSTRAINT `page_ibfk_4` FOREIGN KEY (`location_group_id`) REFERENCES `location_group` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `page_ibfk_5` FOREIGN KEY (`location_id`) REFERENCES `location` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Table 2:-
--
-- Table structure for table `state`
--
CREATE TABLE IF NOT EXISTS `state` (
`id` int(11) NOT NULL,
`abbreviation` char(2) NOT NULL,
`name` varchar(255) NOT NULL,
`territory` tinyint(1) DEFAULT '0',
`capital_city` varchar(255) DEFAULT NULL,
`nickname` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=56 ;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `state`
--
ALTER TABLE `state`
ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `abbreviation` (`abbreviation`),
ADD UNIQUE KEY `name` (`name`), ADD KEY `abbreviation_2` (`abbreviation`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `state`
--
ALTER TABLE `state`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=56;
Please check the unique key relationship between the state table and page table and help me how can i identify this relationship during create my new Entity for this type of DB structure.

Problem creating a database with PHP PDO

I'm having a problem with a SQL query in my PHP Application. When the user access it for the first time, the app executes this query to create all the database:
CREATE TABLE `databases` (
`id` bigint(20) NOT NULL auto_increment,
`driver` varchar(45) NOT NULL,
`server` text NOT NULL,
`user` text NOT NULL,
`password` text NOT NULL,
`database` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
-- --------------------------------------------------------
--
-- Table structure for table `modules`
--
CREATE TABLE `modules` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`title` varchar(100) NOT NULL,
`type` varchar(150) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;
-- --------------------------------------------------------
--
-- Table structure for table `modules_data`
--
CREATE TABLE `modules_data` (
`id` bigint(20) NOT NULL auto_increment,
`module_id` bigint(20) unsigned NOT NULL,
`key` varchar(150) NOT NULL,
`value` tinytext,
PRIMARY KEY (`id`),
KEY `fk_modules_data_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=184 ;
-- --------------------------------------------------------
--
-- Table structure for table `modules_position`
--
CREATE TABLE `modules_position` (
`user_id` bigint(20) unsigned NOT NULL,
`tab_id` bigint(20) unsigned NOT NULL,
`module_id` bigint(20) unsigned NOT NULL,
`column` smallint(1) default NULL,
`line` smallint(1) default NULL,
PRIMARY KEY (`user_id`,`tab_id`,`module_id`),
KEY `fk_modules_order_users` (`user_id`),
KEY `fk_modules_order_tabs` (`tab_id`),
KEY `fk_modules_order_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `tabs`
--
CREATE TABLE `tabs` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`title` varchar(60) NOT NULL,
`columns` smallint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
-- --------------------------------------------------------
--
-- Table structure for table `tabs_has_modules`
--
CREATE TABLE `tabs_has_modules` (
`tab_id` bigint(20) unsigned NOT NULL,
`module_id` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`tab_id`,`module_id`),
KEY `fk_tabs_has_modules_tabs` (`tab_id`),
KEY `fk_tabs_has_modules_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`login` varchar(60) NOT NULL,
`password` varchar(64) NOT NULL,
`email` varchar(100) NOT NULL,
`name` varchar(250) default NULL,
`user_level` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_users_user_levels` (`user_level`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
-- --------------------------------------------------------
--
-- Table structure for table `users_has_tabs`
--
CREATE TABLE `users_has_tabs` (
`user_id` bigint(20) unsigned NOT NULL,
`tab_id` bigint(20) unsigned NOT NULL,
`order` smallint(2) NOT NULL,
`columns_width` varchar(255) default NULL,
PRIMARY KEY (`user_id`,`tab_id`),
KEY `fk_users_has_tabs_users` (`user_id`),
KEY `fk_users_has_tabs_tabs` (`tab_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `user_levels`
--
CREATE TABLE `user_levels` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`level` smallint(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
-- Table structure for table `user_meta`
--
CREATE TABLE `user_meta` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`user_id` bigint(20) unsigned default NULL,
`key` varchar(150) NOT NULL,
`value` longtext NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_user_meta_users` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `modules_data`
--
ALTER TABLE `modules_data`
ADD CONSTRAINT `fk_modules_data_modules` FOREIGN KEY (`module_id`) REFERENCES `modules` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
--
-- Constraints for table `modules_position`
--
ALTER TABLE `modules_position`
ADD CONSTRAINT `fk_modules_order_modules` FOREIGN KEY (`module_id`) REFERENCES `modules` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_modules_order_tabs` FOREIGN KEY (`tab_id`) REFERENCES `tabs` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_modules_order_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
--
-- Constraints for table `users`
--
ALTER TABLE `users`
ADD CONSTRAINT `fk_users_user_levels` FOREIGN KEY (`user_level`) REFERENCES `user_levels` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `user_meta`
--
ALTER TABLE `user_meta`
ADD CONSTRAINT `fk_user_meta_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
INSERT INTO `user_levels` VALUES(1, 10);
INSERT INTO `user_levels` VALUES(2, 1);
INSERT INTO `users` VALUES(1, 'admin', 'password', 'changethis#testing.com', NULL, 1);
INSERT INTO `user_meta` VALUES (NULL, 1, 'last_tab', 1);
In some environments i get this error:
SQLSTATE[HY000]: General error: 1005
Can't create table 'dms.databases'
(errno: 150)
I tried everything that I could find on Google but nothing works.
The strange part is that if I run this query in PhpMyAdmin he creates my database, without any error.
The problem is likely to be in the constraints part of your queries.
The error 150 is mentioned in the InnoDB Documentation:
If you re-create a table that was
dropped, it must have a definition
that conforms to the foreign key
constraints referencing it. It must
have the right column names and types,
and it must have indexes on the
referenced keys, as stated earlier. If
these are not satisfied, MySQL returns
error number 1005 and refers to error
150 in the error message.
If MySQL reports an error number 1005 from a CREATE TABLE statement,
and the error message refers to error
150, table creation failed because a
foreign key constraint was not
correctly formed. Similarly, if an
ALTER TABLE fails and it refers to
error 150, that means a foreign key
definition would be incorrectly formed
for the altered table. You can use
SHOW ENGINE INNODB STATUS to display a
detailed explanation of the most
recent InnoDB foreign key error in the
server.
Source: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
It will also occur if the definitions of the references are not identical (i.e. one is signed, one is unsigned bigint) or mixing unique indexes over several columns with them.

Categories