mysqli multi query not executing the queries - php

I am trying to execute 3 queries to create tables within a database but it will not execute the query giving me a syntax error. Can anyone look at this code and tell me what I typed wrong to make this work? I have tried everything and I can not get the queries to execute!
$dh = mysqli_connect($_POST['hostname'], $_POST['username'], $_POST['password'], $_POST['database']);
if(! $dh )
{
die('Could not connect: ' . mysql_error());
}
echo "Connected successfully...<br /><br />";
$query = "CREATE TABLE IF NOT EXISTS `content` (
`content_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`section_id` INT(11) NOT NULL,
`header` VARCHAR(255) NOT NULL,
`sub_header` VARCHAR(255) NOT NULL,
`date_range` VARCHAR(64) NOT NULL,
`content_body` TEXT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`section_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR(255) NOT NULL,
`position` INT(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(32) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`first_name` VARCHAR(32) NOT NULL,
`last_name` VARCHAR(32) NOT NULL,
`email` VARCHAR(1024) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$retval = mysqli_multi_query($dh, $query);

You don't have semi colons between the queries and you are joining them all together and running at once.
If you are running multiple queries like that you need semicolons at the end of each query
$query = "CREATE TABLE IF NOT EXISTS `content` (
`content_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`section_id` INT(11) NOT NULL,
`header` VARCHAR(255) NOT NULL,
`sub_header` VARCHAR(255) NOT NULL,
`date_range` VARCHAR(64) NOT NULL,
`content_body` TEXT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`section_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR(255) NOT NULL,
`position` INT(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(32) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`first_name` VARCHAR(32) NOT NULL,
`last_name` VARCHAR(32) NOT NULL,
`email` VARCHAR(1024) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";

You are missing semicolons between queries.
So you are trying to execute something like this: <...> ENGINE=InnoDB DEFAULT CHARSET=latin1CREATE TABLE <...>

Related

"a symbol name was expected" error trying to create a table in phpMyAdmin

I am trying to create a table in a database in phpMyAdmin and I keep getting the "a symbol name was expected" error and I cannot figure out what is going on. Is my syntax wrong? I am new to this and I'm at a loss.
Column names, table names should be surrounded by backticks(``)
CREATE TABLE IF NOT EXISTS `sales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`item` varchar(50) NOT NULL,
`date` varchar(50) NOT NULL,
`amount` int(11) NOT NULL,
PRIMARY KEY (`id`)
)
Or you can go without backticks as well:
CREATE TABLE IF NOT EXISTS sales (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
item varchar(50) NOT NULL,
date varchar(50) NOT NULL,
amount int(11) NOT NULL,
PRIMARY KEY (id)
)
Your code is wrong , So here is the correct Code :
CREATE TABLE IF NOT EXISTS `sales` (
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(50) CHARACTER SET utf8 NOT NULL,
`item` VARCHAR(50) CHARACTER SET utf8 NOT NULL,
`date` VARCHAR(50) NOT NULL,
`amount` int(11) NOT NULL
)
You used ' ' sign in your column properties. But mySQL allow you to use `` sign.
CREATE TABLE IF NOT EXISTS `sales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`item` varchar(50) NOT NULL,
`date` varchar(50) NOT NULL,
`amount` int(11) NOT NULL,
PRIMARY KEY (`id`)
);

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

creating triggers with MySQL

Two tables: users and messages
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL,
`password` varchar(10) NOT NULL,
`email` varchar(100) NOT NULL,
`verifystring` varchar(20) NOT NULL,
`active` tinyint(4) NOT NULL,
`usertype` tinyint(4) NOT NULL,
`img` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
CREATE TABLE IF NOT EXISTS `messages` (
`id` tinyint(4) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`user_id` int(11) NOT NULL,
`topic_id` int(11) NOT NULL,
`subject` varchar(100) NOT NULL,
`body` text NOT NULL,
`img` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `topic_id` (`topic_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;
trigger:
CREATE TRIGGER `updateimg` AFTER UPDATE ON `users`
FOR EACH ROW begin
update messages set img = new.img where messages.user_id = users.id;
end
img field is keeping the name of the image user has uploaded.
How can i copy the value img from table users to table messages when it is updated?
Thats should do it:
CREATE TRIGGER update AFTER UPDATE ON table1
for each ROW
begin
update table2 set y = new.y where table2.user_id = new.id;
end

Mysql error #1064

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

Error excuting SQL in mysql_query but not in PHPMyAdmin

I want to create some table through PHP but it fails everytime, but the same code excutes perfectly when excuted through MYsql console or PHPMyAdmin
The SQLs are
$sql = <<<SQL_CODE
CREATE TABLE IF NOT EXISTS `bid` (
`aid` int(11) unsigned NOT NULL,
`uid` int(11) unsigned NOT NULL,
`name` varchar(20) NOT NULL,
`amount` smallint(6) unsigned NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS `item` (
`aid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`description` varchar(120) NOT NULL,
`img` int(11) unsigned NOT NULL,
`amount` smallint(6) unsigned NOT NULL,
`strtdate` date NOT NULL,
`enddate` date NOT NULL,
`uid` int(11) unsigned NOT NULL,
`uname` varchar(20) NOT NULL,
`uamount` int(11) unsigned NOT NULL,
PRIMARY KEY (`aid`)
);
CREATE TABLE IF NOT EXISTS `user` (
`uid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(40) NOT NULL,
`password` varchar(40) NOT NULL,
`nameF` varchar(20) NOT NULL,
`nameL` varchar(20) NOT NULL,
`sex` varchar(1) NOT NULL,
`img` int(11) unsigned NOT NULL,
`country` varchar(10) NOT NULL,
`state` varchar(20) NOT NULL,
`address` varchar(120) NOT NULL,
`code` varchar(8) NOT NULL,
`isAdmin` varchar(1) NOT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `email` (`email`)
);
SQL_CODE;
$sql2 = 'CREATE DATABASE `'.$db.'`;';
$sql3 = 'USE `'.$db.'`; ';
$sql4 = 'drop Database `'.$db.'``;';
if (!mysql_query($sql2)) echo mysql_error();
if (!mysql_query($sql3)) echo mysql_error();
//sleep(1);
if (!mysql_query($sql)) echo mysql_error();
echo $sql2.' '.$sql3.' '.$sql;
Error I'm getting
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 item ( aid int(11) unsigned NOT NULL AUTO_INCREMENT,' at line 1CREATE DATABASE auction; USE auction; CREATE TABLE IF NOT EXISTS bid ( aid int(11) unsigned NOT NULL, uid int(11) unsigned NOT NULL, name varchar(20) NOT NULL, amount smallint(6) unsigned NOT NULL, time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); CREATE TABLE IF NOT EXISTS item ( aid int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(40) NOT NULL, description varchar(120) NOT NULL, img int(11) unsigned NOT NULL, amount smallint(6) unsigned NOT NULL, strtdate date NOT NULL, enddate date NOT NULL, uid int(11) unsigned NOT NULL, uname varchar(20) NOT NULL, uamount int(11) unsigned NOT NULL, PRIMARY KEY (aid));CREATE TABLE IF NOT EXISTS user ( uid int(11) unsigned NOT NULL AUTO_INCREMENT, email varchar(40) NOT NULL, password varchar(40) NOT NULL, nameF varchar(20) NOT NULL, nameL varchar(20) NOT NULL, sex varchar(1) NOT NULL, img int(11) unsigned NOT NULL, country varchar(10) NOT NULL, state varchar(20) NOT NULL, address varchar(120) NOT NULL, code varchar(8) NOT NULL, isAdmin varchar(1) NOT NULL, PRIMARY KEY (uid), UNIQUE KEY email (email));
From the mysql_query manual;
mysql_query() sends a unique query (multiple queries are not
supported)...
You need to split your triple CREATE TABLE statement into 3 separate statements and it will work.
I think that is a problem with multiple queries. I think that is solution.
http://php.net/manual/en/function.mysql-query.php
*mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier*
Just split them up and it will work.

Categories