Unable to create mysql table Dynamically - mysql

I am trying to create a page where a user inputs data for an advertisement and after taking inputs it creates a table where data can be stored . Data being passed correctly and a way to always ensure the table name generated will be unique.
But I am unable to get the query executed and having no idea that why its not working.
I am a beginner and hence any advice at all would be much appreciated.
My code is given below
$Tablename = $Title.$Mobileno;
echo $Tablename;
$sqltable = "CREATE TABLE $Tablename ( `ID` INT NOT NULL AUTO_INCREMENT , `Title` VARCHAR(30) NOT NULL , `Sname` VARCHAR(30) NOT NULL , `email` VARCHAR(50) NOT NULL , `descrip` VARCHAR(200) NOT NULL , `mno` INT(30) NOT NULL , `base` INT(30) NOT NULL , `cutoff` INT(30) NOT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB";
mysqli_query($con,$sqltable);
if(!mysqli_query($con,$sqltable))
{echo "TABLE NOT MADE ";}
$sql = "INSERT INTO $Tablename(Title,Sname,email,descrip,mno,base,cutoff)VALUES('$Title','$Sname','$email','$Description','$Mobileno','$Base','$Cutoff')";
mysqli_query($con,$sqltable);
if(!mysqli_query($con,$sql))
{echo'not inserted';}
else {echo'inserted';}
?>

Try enciosing with quote the tablename
"CREATE TABLE '$Tablename' (
`ID` INT NOT NULL AUTO_INCREMENT
, `Title` VARCHAR(30) NOT NULL
, `Sname` VARCHAR(30) NOT NULL
, `email` VARCHAR(50) NOT NULL
, `descrip` VARCHAR(200) NOT NULL
, `mno` INT(30) NOT NULL
, `base` INT(30) NOT NULL
, `cutoff` INT(30) NOT NULL
, PRIMARY KEY (`ID`)) ENGINE = InnoDB";
or use string concatenation
"CREATE TABLE '" . $Tablename."' (
`ID` INT NOT NULL AUTO_INCREMENT
, `Title` VARCHAR(30) NOT NULL
, `Sname` VARCHAR(30) NOT NULL
, `email` VARCHAR(50) NOT NULL
, `descrip` VARCHAR(200) NOT NULL
, `mno` INT(30) NOT NULL
, `base` INT(30) NOT NULL
, `cutoff` INT(30) NOT NULL
, PRIMARY KEY (`ID`)) ENGINE = InnoDB";
be sure you have proper sanitize the $Tablename vars

Related

How to resolve an error near NULL when creating a table

Whenever I try to create a table
CREATE TABLE registration` (`id` INT NOT NULL , `name` VARCHAR(30) NOT NULL , `email` VARCHAR(20) NOT NULL , `password` VARCHAR(15) NOT NULL , `DOB` DATE NOT NULL , `age` INT NOT NULL , `number` BIGINT NOT NULL , `religion` VARCHAR(10) NOT NULL , `education` VARCHAR(20) NOT NULL , `profession` VARCHAR(20) NOT NULL , `gender` ENUM NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
The following error occurs
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NOT NULL , PRIMARY KEY (id)) ENGINE = InnoDB' at line 1
ENUM needs values so you need to define the values they can get
CREATE TABLE registration (
`id` INT NOT NULL
, `name` VARCHAR(30) NOT NULL
, `email` VARCHAR(20) NOT NULL
, `password` VARCHAR(15) NOT NULL
, `DOB` DATE NOT NULL
, `age` INT NOT NULL
, `number` BIGINT NOT NULL
, `religion` VARCHAR(10) NOT NULL
, `education` VARCHAR(20) NOT NULL
, `profession` VARCHAR(20) NOT NULL
, `gender` ENUM ('male','female')
, PRIMARY KEY (`id`)
) ENGINE = InnoDB;
When you declare a ENUM column (your gender column) you have to assign all possible values for the enumeration (See here for syntax)

MySQL error #1089 while creating database

I am creating a database it keeps on giving me error:
CREATE TABLE `pgdavplacments`.`drive` ( `drive_id` INT(5) NOT NULL AUTO_INCREMENT ,
`drive_name` VARCHAR(200) NOT NULL ,
`drive_date` DATE NOT NULL ,
`drive_time` TIME NOT NULL ,
`drive_info` VARCHAR(8000) NOT NULL ,
`eligibility1` INT(5) NOT NULL ,
`eligibility2` INT(5) NOT NULL ,
`eligibility3` INT(5) NOT NULL ,
PRIMARY KEY (`drive_id`(1000)))
ENGINE = InnoDB;
What is the problem here?
CREATE TABLE `pgdavplacments`.`drive` (
`drive_id` INT(5) NOT NULL AUTO_INCREMENT,
`drive_name` VARCHAR(200) NOT NULL,
`drive_date` DATE NOT NULL,
`drive_time` TIME NOT NULL,
`drive_info` VARCHAR(8000) NOT NULL,
`eligibility1` INT(5) NOT NULL,
`eligibility2` INT(5) NOT NULL,
`eligibility3` INT(5) NOT NULL,
PRIMARY KEY (`drive_id` )
) ENGINE=INNODB;
You can try above query.

What does the engine and char set means after the ()?

DROP DATABASE IF EXISTS `Database` ;
CREATE DATABASE `Database` ;
DROP TABLE IF EXISTS `Registration Form` ;
CREATE TABLE `Registration Form`
(
`ID` int(11) NOT NULL AUTO_INCREMENT ,
`Username` varchar(50) NOT NULL ,
`Password` varchar(50) NOT NULL ,
`Display Name` varchar(255) NOT NULL ,
`Question` varchar(255) NOT NULL ,
`Answer` varchar(255) NOT NULL ,
`Title` varchar(10) NOT NULL ,
`Name` varchar(255) NOT NULL ,
`Date Of Birth` date NOT NULL ,
`Gender` varchar(10) NOT NULL ,
`Address` varchar(255) NOT NULL ,
`Postal Code` int(11) NOT NULL ,
`City / Town` varchar(255) NOT NULL ,
`Federal Territory / State` varchar(255) NOT NULL ,
`Citizenship` varchar(255) NOT NULL ,
`E-mail` varchar(255) NOT NULL ,
`Contact Number` int(20) NOT NULL ,
`Registration Date` datetime NOT NULL ,
PRIMARY KEY ( `ID` )
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
I'm just starting to work arounnd mysql and php ... so bear with me as im a newbie ...
What does ALL the code after the CREATE() mean ? Why is there a ENGINE and DEFAULT what do they do ? Is CHARSET same as collation , if yes why not put it inside the row instead ? Do you need to give a length/value for date and daetime ?
CREATE TABLE is the sql clause which is required to creating a table.
After that you have to give a table name i.e "Registration Form" .but avoid spaces in between table name
Next what ever you gave those are column name along with their datatype and size.NOT NULL means while inserting data those column can't be blank.
For more details check the below link
http://www.tutorialspoint.com/mysql/mysql-create-tables.htm

SQL Error SQL DB function failed with error number 1064

I have prblem with sql when i try to check if table extist.
Am making component for joomla and when make install.sql i have sql syntax error.
check this and tell me what i do wrong:
DROP TABLE IF EXIST `#__ik_property_item`;
DROP TABLE IF EXIST `#__ik_property_categories`;
DROP TABLE IF EXIST `#__ik_property_type`;
DROP TABLE IF EXIST `#__ik_property_price_type`;
DROP TABLE IF EXIST `#__ik_property_agnts`;
DROP TABLE IF EXIST `#__ik_property_companies`;
DROP TABLE IF EXIST `#__ik_property_agnts`;
CREATE TABLE `#__ik_property_item` (
`id` INT NOT NULL AUTO_INCREMENT ,
`category_id` INT NULL ,
`property_type_id` INT NULL ,
`price_type_id` INT NULL ,
`company_id` INT NOT NULL ,
`agent_id` INT NOT NULL ,
`title` VARCHAR(45) NULL ,
`street` VARCHAR(45) NULL ,
`status` INT NULL ,
`featured` INT NULL ,
`yt_video` TEXT NULL ,
`map_lattitude` INT NULL ,
`map_longitude` VARCHAR(45) NULL ,
`beds` INT NULL ,
`baths` VARCHAR(45) NULL ,
`garage` VARCHAR(45) NULL ,
`flat_size` VARCHAR(45) NULL ,
`kitchens` VARCHAR(45) NULL ,
`year_build` VARCHAR(45) NULL ,
`floor` VARCHAR(45) NULL ,
`price` INT NULL ,
`image_1` VARCHAR(45) NULL ,
`image_2` VARCHAR(45) NULL ,
`image_3` VARCHAR(45) NULL ,
`image_4` VARCHAR(45) NULL ,
`image_5` VARCHAR(45) NULL ,
`meta_title` VARCHAR(45) NULL ,
`meta_description` TEXT NULL ,
`intro_text` TEXT NULL ,
`full_text` TEXT NULL ,
PRIMARY KEY (`id`) );
CREATE TABLE `#__ik_property_categories` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
CREATE TABLE `#__ik_property_type` (
`id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
CREATE TABLE `#__ik_property_price_type` (
`id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
CREATE TABLE `#__ik_property_agnts` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
`last_name` VARCHAR(45) NULL ,
`first_name` VARCHAR(45) NULL ,
`email` VARCHAR(45) NULL ,
`website` VARCHAR(45) NULL ,
`phone` VARCHAR(45) NULL ,
`company` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
CREATE TABLE `#__ik_property_companies` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
`logo` VARCHAR(45) NULL ,
`email` VARCHAR(45) NULL ,
`phone` VARCHAR(45) NULL ,
`website` VARCHAR(45) NULL ,
`street` VARCHAR(45) NULL ,
`fax` VARCHAR(45) NULL ,
`intro_text` VARCHAR(45) NULL ,
`full_text` VARCHAR(45) NULL ,
`image_1` VARCHAR(45) NULL ,
`image_2` VARCHAR(45) NULL ,
`image_3` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
When i remove
DROP TABLE IF EXIST `#__ik_property_item`;
DROP TABLE IF EXIST `#__ik_property_categories`;
DROP TABLE IF EXIST `#__ik_property_type`;
DROP TABLE IF EXIST `#__ik_property_price_type`;
DROP TABLE IF EXIST `#__ik_property_agnts`;
DROP TABLE IF EXIST `#__ik_property_companies`;
DROP TABLE IF EXIST `#__ik_property_agnts`;
All is success executed and when i call uninstall.sql code inside is like above all work good. But i need to check on installation if that tables exist if exist i must delete and save new.
Thanks

MYSQL error in creating foreign key constraint

I am using MySQL to create a small database. and facing some issues in foreign keys and primary key. I really don't understand as it seems a simple problem.
CREATE TABLE IF NOT EXISTS `db_trimms`.`urban_area` (
`area_id` INT(10) NOT NULL ,
`city` VARCHAR(60) NOT NULL ,
`state` VARCHAR(60) NOT NULL ,
`urban_area` VARCHAR(60) NOT NULL ,
`census_region` VARCHAR(60) NOT NULL ,
`area_no` VARCHAR(60) NOT NULL ,
`freeway_speed` VARCHAR(60) NOT NULL ,
`arterial_speed` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`area_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
its running successfully. But while create another table and creating foreign key referring to the area_id of the above table...its creating issues...
#1005 - Can't create table 'db_trimms.emiss_others_offpeak' (errno: 150) (Details...)
and here is the query
CREATE TABLE IF NOT EXISTS `db_trimms`.`emiss_others_offpeak` (
`area_id` INT(10) UNSIGNED NOT NULL ,
`ammonia` VARCHAR(60) NOT NULL ,
`atm_carbon_dio` VARCHAR(60) NOT NULL ,
`carbon_dio_equiv` VARCHAR(60) NOT NULL ,
`carbon_mono` VARCHAR(60) NOT NULL ,
`methane` VARCHAR(60) NOT NULL ,
`nitrogen_dio` VARCHAR(60) NOT NULL ,
`nitrogen_oxide` VARCHAR(60) NOT NULL ,
`nitrous_oxide` VARCHAR(60) NOT NULL ,
`non_meth_hydrocarbs` VARCHAR(60) NOT NULL ,
`oxides_of_nitrogen` VARCHAR(60) NOT NULL ,
`particulate_matter_pm10` VARCHAR(60) NOT NULL ,
`particulate_matter_pm2_5` VARCHAR(60) NOT NULL ,
`sulfate` VARCHAR(60) NOT NULL ,
` sulfur_dioxide` VARCHAR(60) NOT NULL ,
`total_hydrocarbon` VARCHAR(60) NOT NULL ,
`vol_org_comp` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`area_id`) ,
CONSTRAINT `fk_others_offpeak_urban`
FOREIGN KEY (`area_id` )
REFERENCES `db_trimms`.`urban_area` (`area_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
The data types are incompatible with each other. Make column area_id from table urban_area also UNSIGNED.
`area_id` INT(10) UNSIGNED NOT NULL ,
SQLFiddle Demo