Mysql error #1064 - php

I am trying to create some tables on phpMyAdmin, but when I use the code below I am getting this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(32) NOT NULL,
activated enum(0,1) NOT NULL,
PRIMARY KEY (id)
) E' at line 8
Here is my code:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`sign_up_date` date(32) NOT NULL,
`activated` enum(`0`,`1`) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=1atin1 AUTO_INCREMENT=1 ;

Try following code. I have resolved your mistakes in code
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`sign_up_date` date NOT NULL,
`activated` enum('0','1') NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

DATE data type takes no length
`sign_up_date` DATE NOT NULL

Related

how to insert a row with default value in mysql table

after form submission, the data is stored in database. This is the query i already have:
CREATE TABLE IF NOT EXISTS `registered_users` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`gender` varchar(20) NOT NULL,
`date_birth` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`postal_code` varchar(255) NOT NULL,
`place` varchar(255) NOT NULL,
`school_name` varchar(255) NOT NULL,
`email` varchar(55) NOT NULL,
`tel` varchar(55) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
This is how i insert the data into the DB:
Now i want to insert a new row in the DB which can have only 2 values: "active" or "inactive" for each user. Default on "inactive". That should be inserted automatically for each user after submit. How can i do that?
When you create a table in a mysql db you can specify a default value for attributes. So if you want a boolean "active" / "inactive" flag you might want to try adding an active attribute to your table. See the added last column here:
CREATE TABLE IF NOT EXISTS `registered_users` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`gender` varchar(20) NOT NULL,
`date_birth` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`postal_code` varchar(255) NOT NULL,
`place` varchar(255) NOT NULL,
`school_name` varchar(255) NOT NULL,
`email` varchar(55) NOT NULL,
`tel` varchar(55) NOT NULL,
`active` BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Ref.: https://dev.mysql.com/doc/refman/5.7/en/create-table.html

execute sql query in php doesn't work ,but the query work in phpmyadmin

i have my database sql query file and i wanna run it in php by the code below
$query = file_get_contents('./sqlquery.txt');
print $query;
$conn->query($query);
but it return this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `ads` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ur' at line 2
i copied the print output in to the phpmyadmin and everything work well, what's wrong here?
my sql query is the this
DROP TABLE IF EXISTS `ads`;
CREATE TABLE IF NOT EXISTS `ads` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(300) NOT NULL,
`path` varchar(200) NOT NULL,
`width` int(11) NOT NULL,
`height` int(11) NOT NULL,
`priority` int(11) NOT NULL,
`adsalter` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`adstitle` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
DROP TABLE IF EXISTS `comment`;
CREATE TABLE IF NOT EXISTS `comment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(120) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`comment` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`contentid` int(11) NOT NULL,
`parentid` int(11) NOT NULL DEFAULT '0',
`date` varchar(250) NOT NULL,
`haschild` int(1) NOT NULL DEFAULT '0',
`visible` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
DROP TABLE IF EXISTS `files`;
CREATE TABLE IF NOT EXISTS `files` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`size` int(14) NOT NULL,
`type` varchar(50) NOT NULL,
`newsid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
DROP TABLE IF EXISTS `frgpss`;
CREATE TABLE IF NOT EXISTS `frgpss` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`ip` varchar(31) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
`token` varchar(27) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`date` int(15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
DROP TABLE IF EXISTS `news`;
CREATE TABLE IF NOT EXISTS `news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`titrimage` varchar(100) NOT NULL,
`titr` varchar(100) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`titralter` varchar(160) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`newsshurt` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`text` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`keywords` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`description` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`author` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`branch` int(11) NOT NULL,
`date` varchar(160) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`visible` int(1) NOT NULL DEFAULT '0',
`visited` int(11) NOT NULL DEFAULT '0',
`titrtitle` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
DROP TABLE IF EXISTS `signup`;
CREATE TABLE IF NOT EXISTS `signup` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`family` varchar(50) NOT NULL,
`email` varchar(80) NOT NULL,
`gender` varchar(50) NOT NULL,
`username` varchar(80) NOT NULL,
`picture` varchar(80) NOT NULL,
`password` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(60) NOT NULL,
`lastname` varchar(60) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(40) NOT NULL,
`userregistereddate` varchar(60) NOT NULL,
`key` varchar(60) NOT NULL,
`type` varchar(20) NOT NULL,
`userphoto` varchar(50) NOT NULL,
`usergroup` varchar(300) NOT NULL,
`ipaddress` text NOT NULL,
`telnumber` varchar(14) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
you're executing multiple queries here, it won't work simply, I'm pretty sure that in query function($conn->query($query);) you're using mysql_query() or mysqli_query(), but you'll have to do is to use mysqli_multi_query() instead. Have a look here at docx
$con = mysqli_connect($host, $user, $pass, $db) OR die(mysqli_error($con));
$query = file_get_contents('./sqlquery.txt');
mysqli_multi_query($con, $query);
mysqli_close($con);
Hope this will help you
Note: Assuming that all queries are working fine while executing in PHPMyAdmin.

MySQL #1064 error during table import

I have been having this reoccurring problem when attempting to import a .sql file into MySQL, Here is what I get
ERROR
SQL Query:
CREATE TABLE `ADMINS` (
`id` int(11) NOT NULL auto_increment,
`client_id` varchar(80) NOT NULL default '',
`client_pw` varchar(16) default NULL,
`client_school` varchar(16) default NULL,
`client_expdate` date default '0000-00-00',
`client_fullname` varchar(50) default NULL,
`webinfo` mediumtext,
`webinfodate` date default NULL,
`LastUpdate` timestamp(14) NOT NULL,
`user_activation_key` varchar(60) default NULL,
`user_email` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
MySQL Said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(14) NOT NULL,
`user_activation_key` varchar(60) default NULL,
`user_email' at line 10
Timestamps don't have lengths. Change:
timestamp(14)
to
timestamp
The syntax is not correct, as pointed by John you do not need to the length for timestamp datatype. In addition type=myisam is deprecated and its engine=myisam now.
CREATE TABLE `ADMINS` (
`id` int(11) NOT NULL auto_increment,
`client_id` varchar(80) NOT NULL default '',
`client_pw` varchar(16) default NULL,
`client_school` varchar(16) default NULL,
`client_expdate` date default '0000-00-00',
`client_fullname` varchar(50) default NULL,
`webinfo` mediumtext,
`webinfodate` date default NULL,
`LastUpdate` timestamp NOT NULL,
`user_activation_key` varchar(60) default NULL,
`user_email` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) engine=MyISAM AUTO_INCREMENT=2 ;

cakephp manual sql query in one table

I have this table structure
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`realname` varchar(50) NOT NULL,
`password` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`bos1` varchar(30) DEFAULT NULL,
`emailbos1` varchar(50) DEFAULT NULL,
`role` varchar(20) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;
I got an issue here :
Two persons (eg: username=steve, and balmer) has bos1 name (eg: bill)
When I login as steve and/or balmer I can see my own detail / data,
but when I login as bill, I want to show steve's and balmer's detail as well.
How do i suppose to do it ?
Thank you.

sql syntax error while executing my query

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS administracia( userID INT(255) NOT NULL AUTO_INCREME' at line 12
This is the query more or less also more readable version
$data = "
CREATE TABLE IF NOT EXISTS zamestnanci(
userID INT(255) NOT NULL AUTO_INCREMENT,
osobnecislo VARCHAR(50) NOT NULL,
meno VARCHAR(50) NOT NULL,
priezvisko VARCHAR(50) NOT NULL,
nick VARCHAR(50) NOT NULL,
heslo CHAR(128) NOT NULL,
previerka VARCHAR(15) NOT NULL,
PRIMARY KEY (userID,nick))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS administracia(
userID INT(255) NOT NULL AUTO_INCREMENT,
osobnecislo VARCHAR(50) NOT NULL,
meno VARCHAR(50) NOT NULL,
priezvisko VARCHAR(50) NOT NULL,
nick VARCHAR(50) NOT NULL,
heslo CHAR(128) NOT NULL,
previerka VARCHAR(15) NOT NULL,
PRIMARY KEY (userID,nick))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS navrhy(
navrhID INT(255) NOT NULL AUTO_INCREMENT,
nazov VARCHAR(50) NOT NULL,
text VARCHAR(505) NOT NULL,
schvalenie VARCHAR(15) NOT NULL,
datum VARCHAR(50) NOT NULL,
pridal VARCHAR(50) NOT NULL,
hlasovali VARCHAR(8000) NOT NULL,
kategoria VARCHAR(50) NOT NULL,
stroj VARCHAR(50) NOT NULL,
ano VARCHAR(1000) NOT NULL,
nie VARCHAR(1000) NOT NULL default '0',
PRIMARY KEY (navrhID))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS problemovyartikel(
artikelID INT(255) NOT NULL AUTO_INCREMENT,
artikelcisloVARCHAR(50) NOT NULL,
text VARCHAR(255) NOT NULL,
datum VARCHAR(50) NOT NULL,
pridal tinytext NOT NULL,
problem_cislo VARCHAR(100) NOT NULL,
PRIMARY KEY (artikelID))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS kategorie(
kategoriaID INT(255) NOT NULL AUTO_INCREMENT,
Nkategoria VARCHAR(50) NOT NULL,
Pkategoria VARCHAR(50) NOT NULL,
PRIMARY KEY (kategoriaID))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS novinky(
novinkaID INT(255) NOT NULL AUTO_INCREMENT,
novinka mediumtext NOT NULL,
PRIMARY KEY (novinkaID))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS stroje(
strojID INT(255) NOT NULL AUTO_INCREMENT,
stroj text NOT NULL,
PRIMARY KEY (strojID))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS kontakt(
kontaktID INT(255) NOT NULL AUTO_INCREMENT,
pole1 text NOT NULL,
pole2 text NOT NULL,
PRIMARY KEY (kontaktID))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS problem_cislo(
problem_cisloID INT(255) NOT NULL AUTO_INCREMENT,
problem_cislo VARCHAR(100) NOT NULL,
PRIMARY KEY (problem_cisloID))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS stranky(
strankaid INT(255) NOT NULL AUTO_INCREMENT,
stranka mediumtext NOT NULL,
stranka_obsah longtext NOT NULL,
subor mediumtext NOT NULL,
PRIMARY KEY (strankaid))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS chyby(
strankaid INT(255) NOT NULL AUTO_INCREMENT,
stranka CHAR(128) NOT NULL,
stranka_obsah VARCHAR(15) NOT NULL,
PRIMARY KEY (strankaid))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
INSERT INTO administracia (userID,osobnecislo,meno,priezvisko,nick,heslo,previerka) VALUES('1','admin','admin','admin','admin','59db9e61d015935901442385d992dd34346dbe3439174fee700b56f27ad445ee42e052c0c014892a70cea63b912c7a3ee55c7c42e9e36e0f169ab7e6b886fd21','admin');
INSERT INTO kategorie (kategoriaID,Nkategoria) VALUES('1','kategoria1');
INSERT INTO novinky (novinkaID,novinka) VALUES('1','novinka1<br>novinka2');
INSERT INTO stroje (strojID,stroj) VALUES('1','stroj1');
INSERT INTO kontakt (kontaktID,pole1,pole2) VALUES('1','testificate','0912 345 678');
INSERT INTO problem_cislo (problem_cisloID,problem_cislo) VALUES('1','problem1');
INSERT INTO chyby (strankaid,stranka_obsah,stranka) VALUES('1','Registrácia prebehla úspešne!','úspech'),('2','Návrh bol úspešne pridaný!','úspech'),('3','Problémový artikel bol úspešne pridaný!','úspech'),('4','Aktivácia prebehne čo najskôr alebo ste zadali nesprávne údaje!,'totok')";
Edit:
Using the following function to split and execute the queries:
$queries = preg_split("/;+(?=([^'|^\\\']*['|\\\'][^'|^\\\']*['|\\\'])*[^'|^\\\']*[^'|^\\\']$)/",$data);
foreach ($queries as $query)
if(strlen(trim($query)) > 0)
mysql_query($query); {
$spracovat = mysql_query($data,$prepojenie) or die(mysql_error());
You appear to be stuffing this whole thing into a PHP variable. Most PHP MySQL APIs will only accept one statement per execution, so you cannot combine all these into a single statement unless you are using something like MySQLi::multi_query().
Instead of packing all of these into a single variable and attempting to parse out individual queries with preg_split(), I recommend creating an array of queries and looping over it to execute them all. That makes for much cleaner and less error-prone code.
$queries = array(
'CREATE TABLE IF NOT EXISTS zamestnanci(
userID INT(255) NOT NULL AUTO_INCREMENT,
osobnecislo VARCHAR(50) NOT NULL,
meno VARCHAR(50) NOT NULL,
priezvisko VARCHAR(50) NOT NULL,
nick VARCHAR(50) NOT NULL,
heslo CHAR(128) NOT NULL,
previerka VARCHAR(15) NOT NULL,
PRIMARY KEY (userID,nick))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;',
'CREATE TABLE IF NOT EXISTS administracia(
userID INT(255) NOT NULL AUTO_INCREMENT,
osobnecislo VARCHAR(50) NOT NULL,
meno VARCHAR(50) NOT NULL,
priezvisko VARCHAR(50) NOT NULL,
nick VARCHAR(50) NOT NULL,
heslo CHAR(128) NOT NULL,
previerka VARCHAR(15) NOT NULL,
PRIMARY KEY (userID,nick))
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;',
...
);
foreach ($queries as $query) {
// Execute the query, test for errors, etc.
}
You are likely issuing this with mysql_query, which can only send one query, not a string containing 18.

Categories