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
Related
I am creating a database that will store 'graduate details'. I have been able to set the ID to auto increment, Unique and also my primary key that start from 1...
I also want to make the certificate number field unique and auto-increment value in an increasing order that will start from 00001.
Please any help on how to go about this will be very appreciated. Thank you.
This is the MYSQL code for the database below;
CREATE TABLE IF NOT EXISTS `graduates` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`f_name` varchar(25) NOT NULL,
`l_name` varchar(25) NOT NULL,
`gender` varchar(6) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
`city` varchar(15) DEFAULT NULL,
`region` varchar(30) DEFAULT NULL,
`phone` varchar(15) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`certificate_number` int(50) NOT NULL AUTO_INCREMENT,
`programme` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
`marks` int(10) NOT NULL,
`college_name` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
`date_of_birth` date DEFAULT NULL,
`created_by` int(10) UNSIGNED NOT NULL DEFAULT 0,
`created_at` date DEFAULT NULL,
`updated_by` int(10) UNSIGNED NOT NULL DEFAULT 0,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `certificate_number` (`certificate_number`)
) ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=utf8;
I am working on creating table in to my database with mysql and here is my code:
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `review` (
`clients_id` int(15) NOT NULL AUTO_INCREMENT,
`client_id` varchar(150) NOT NULL DEFAULT '',
`rating` tinyint(2) NOT NULL DEFAULT '0',
`proj_date` date NOT NULL DEFAULT '0000-00-00',
`proj_desc` text NOT NULL DEFAULT '',
`photoname` text NOT NULL,
`companyname` text NOT NULL,
`feedback` text NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '0',
`emailid` varchar(100) NOT NULL DEFAULT '',
`customratings` varchar(100) NOT NULL DEFAULT '',
`photo_option` varchar(100) NOT NULL DEFAULT '',
`title` varchar(100) NOT NULL DEFAULT '',
`citation` varchar(100) NOT NULL DEFAULT '',
`date_option` varchar(100) NOT NULL DEFAULT '',
`rating_option` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`clients_id`),
FULLTEXT KEY `feedback` (`feedback`)
) ENGINE=MyISAM AUTO_INCREMENT=1") or mysqli_error($link);
But this is not reflecting into my database ? Why were I may be going wrong ?
but I tried creating other table with the following code
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `setting` (
`id` int(11) NOT NULL auto_increment,
`script_url` text NOT NULL,
`date` varchar(4) NOT NULL,
`rateing` varchar(4) NOT NULL,
`photo` varchar(4) NOT NULL,
`dateformat` varchar(4) NOT NULL,
`page_limit` int(4) NOT NULL,
`proj_desc` varchar(4) NOT NULL,
`companyname` varchar(4) NOT NULL,
`text_color` varchar(255) NOT NULL,
`citation_color` varchar(255) NOT NULL,
`bg_color` varchar(255) NOT NULL,
`border_color` varchar(255) NOT NULL,
`ratingsformat` varchar(250) NOT NULL,
`rating` varchar(250) NOT NULL,
`customratings` varchar(250) NOT NULL,
`speed` varchar(250) NOT NULL,
`pagination` varchar(250) NOT NULL,
`version` varchar(250) NOT NULL,
`global_option` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;
") or mysqli_error($link);
it is being created correctly and both the tables are in the same file
No you cannot create table using an insert statement.
There are four types of SQL statements:
DML (DATA MANIPULATION LANGUAGE)
DDL (DATA DEFINITION LANGUAGE)
DCL (DATA CONTROL LANGUAGE)
TCL (TRANSACTION CONTROL LANGUAGE)
DML is your SELECT, INSERT, UPDATE and DELETE statements...
DDL is you your CREATE, ALTER and DROP statements.
See more info about types of SQL statements
In order to insert data in your table, first you need to create it.
How to create sql table from php
No. "INSERT INTO" used to insert the data to existing table only.You should create the table with respective column before try to insert the values.
Or you can check the table exist or not before going insert."If exists" u can insert the value else just create and insert the values.
This worked for me but I don't know how?
I just removed DEFAULT '' present while creating review table and table just got created
Here is edited code
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `review` (
`clients_id` int(15) NOT NULL AUTO_INCREMENT,
`client_id` varchar(150) NOT NULL,
`rating` tinyint(2) NOT NULL,
`proj_date` date NOT NULL,
`proj_desc` text NOT NULL,
`photoname` text NOT NULL,
`companyname` text NOT NULL,
`feedback` text NOT NULL,
`status` tinyint(1) NOT NULL,
`emailid` varchar(100) NOT NULL,
`customratings` varchar(100) NOT NULL,
`photo_option` varchar(100) NOT NULL,
`title` varchar(100) NOT NULL,
`citation` varchar(100) NOT NULL,
`date_option` varchar(100) NOT NULL,
`rating_option` varchar(100) NOT NULL,
PRIMARY KEY (`clients_id`),
FULLTEXT KEY `feedback` (`feedback`)
) ENGINE=MyISAM AUTO_INCREMENT=1") or mysqli_error($link);
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.
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.
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.