can anyone please assist me, that whats wrong i m doing. There is no syntax error in the php code, but output is- connection successful
Table customers NOT created. Error: 1064
Table stats NOT created. Error : 1005. I am using XAMPP, with PHP 7
<?php
$conn = mysqli_connect("localhost","root","","test");
if(mysqli_connect_errno()){
echo "Connect failed%\n".mysqli_connect_errno();
exit();
}
else {
echo "connection successful";
}
$sql1 = "CREATE TABLE CUSTOMERS (
P_Id int(10) NOT NULL AUTO_INCREMENT,
Firstname(100) varchar NOT NULL,
Lastname(100) varchar NOT NULL,
Address varchar(500) NOT NULL,
City varchar(100) NOT NULL,
PRIMARY KEY(P_Id)
)";
if(mysqli_query($conn,$sql1)==1){
echo "Table customers created successfully";
}
else
{
echo "<br/>"."Table customers NOT created. Error: ".mysqli_errno($conn);
}
$sql2 = 'CREATE TABLE STATS (
O_Id int(10) NOT NULL AUTO_INCREMENT,
Offers varchar(1000) NOT NULL,
Resorts varchar(1000) NOT NULL,
ArrivalDate date NOT NULL,
P_Id int(10) NOT NULL,
PRIMARY KEY(O_Id),
FOREIGN KEY(P_Id) REFERENCES CUSTOMERS(P_Id)
)';
if(mysqli_query($conn,$sql2)==1){
echo "Table stats created successfully";
}
else
{
echo "<br/>"."Table stats NOT created. Error : ".mysqli_errno($conn);
}
?>
You need to specify the character limit for VARCHAR after the VARCHARkeyword. So, for example, in the query for table CUSTOMERS, the Firstname clause will look like:
Firstname VARCHAR(100) NOT NULL
The error is how you declare your varchar.
Firstname(100) varchar NOT NULL,
Must be :
Firstname varchar(100) NOT NULL,
So change firstname and lastname syntax and all will work right.
Hope this help.
I think your problem is column declarations:
You should use: <name> <type>(length) NOT NULL
Related
I'm trying to create following table . query works in phpmyadmin but doesn't work in php script. pls point out mistake in my code. Thank You
$sql = "CREATE TABLE IF NOT EXISTS `portal` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`message_sid` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`number` varchar(255) NOT NULL,
`message` text NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`status` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1";
if (mysqli_query($conn, $sql)) {
echo "Table portal created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
I'm getting this error:
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 '`id` int(255) NOT NULL AUTO_INCREMENT,
`message_sid` varchar(255) NOT NULL' at line 2
I have done this by checking if table exists in php, on false create a new table
creating table would be best to export an already existing one with phpmyadmin and then just use it as query syntax
Take the single quotes off of your table and row names
also if you make the id a primary key auto-increment, do not make it not null, this will cause a error.
I am unsure why the down votes....
CREATE TABLE IF NOT EXISTS portal (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
message_id VARCHAR(30) NOT NULL,
name VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
number VARCHAR(50) NOT NULL,
message text NOT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(50) NOT NULL )ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
This will work
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 5 years ago.
Do you have any idea whats wrong with my code? I can't really figure it out.
Connection failed: SQLSTATE[42000]: Syntax error or access violation: 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 'VARCHAR(50) NOT NULL, url TEXT(65535) NOT NULL, ip VARCHAR(150) NOT NULL)' at line 3
<?php
$host = "localhost";
$dbname = "nope";
$username = "nope";
$password = "nope";
if(isset($_GET["s"])){
try{
$pdo = new PDO("mysql:host=".$host.";dbname=".$dbname,$username,$password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$table = "links";
$sql ="CREATE TABLE IF NOT EXISTS $table(
ID INT(11) AUTO_INCREMENT PRIMARY KEY,
key VARCHAR(50) NOT NULL,
url TEXT(65535) NOT NULL,
ip VARCHAR(150) NOT NULL)";
$pdo->exec($sql);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
/*$statement = $pdo->prepare("select url from $table where key = :key");
$statement->execute(array(':key' => $_GET["s"]));
$result = $statement->fetch();
echo $result;*/
}
else{
echo "error";
}
?>
Your column name key is a reserved keyword in mysql
The solution is to quote the column name in backticks
`key` VARCHAR(50) NOT NULL,
or just change the name of the column to whatever you like which is not a reserved word.
my_key VARCHAR(50) NOT NULL,
So your statement should be
$sql ="CREATE TABLE IF NOT EXISTS $table(
ID INT(11) AUTO_INCREMENT PRIMARY KEY,
`key` VARCHAR(50) NOT NULL,
url TEXT(65535) NOT NULL,
ip VARCHAR(150) NOT NULL)";
or
$sql ="CREATE TABLE IF NOT EXISTS $table(
ID INT(11) AUTO_INCREMENT PRIMARY KEY,
table_key VARCHAR(50) NOT NULL,
url TEXT(65535) NOT NULL,
ip VARCHAR(150) NOT NULL)";
Try the following query instead:
CREATE TABLE IF NOT EXISTS $table(
`ID` INT(11) AUTO_INCREMENT PRIMARY KEY,
`key` VARCHAR(50) NOT NULL,
`url` TEXT NOT NULL,
`ip` VARCHAR(150) NOT NULL)
Key is a Reserved Word in MariaDB. And thus needs to be enclosed in ``. And there is no need to define length for type TEXT
I did this earlier and it worked just fine and I even brought in another table and it was created just fine. I am stuck.
Here is the table that is not working.
<?php
include_once('dbconx.php');
$tbl_pages = "CREATE TABLE IF NOT EXISTS pages (
id INT(11) NOT NULL AUTO_INCREMENT,
label VARCHAR(20) NOT NULL,
title VARCHAR(50) NOT NULL,
body TEXT NOT NULL,
slug VARCHAR(50) NOT NULL,
create TIMESTAMP NOT NULL,
updated TIMESTAMP NULL,
PRIMARY KEY(id),
)";
$query = mysqli_query($dbcon, $tbl_pages);
if ($query === TRUE) {
echo "<h3>Pages table created OK :) </h3>";
} else {
echo "<h3>Pages table NOT created :( </h3>";
}
?>
One of your field names is a MySQL reserved words create. Try changing the field name to something like created. See MySQL Keywords and Reserved Words
You also have an extra comma after the primary key definition.
Try this query:
$tbl_pages = "CREATE TABLE IF NOT EXISTS pages (
id INT(11) NOT NULL AUTO_INCREMENT,
label VARCHAR(20) NOT NULL,
title VARCHAR(50) NOT NULL,
body TEXT NOT NULL,
slug VARCHAR(50) NOT NULL,
created TIMESTAMP NOT NULL,
updated TIMESTAMP NULL,
PRIMARY KEY(id)
)";
You can also echo mysqli_error if it fails so you can see details of the error.
I have below code I am trying to run..Connection is successfully created but still mysqli_query does not create a table.What I am missing...
here is the script I am executing...
error_reporting(E_ALL);
ini_set('display_errors', '1');
$con=mysqli_connect("localhost","xxxxxx","xxxxx","xxx");
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
} else {
echo "Successful database connection";
}
$tbl_users = "CREATE TABLE IF NOT EXISTS users (
id INT(11) NOT NULL AUTO_INCREMENT,
firstname VARCHAR(255) NOT NULL,
lastname VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
gender ENUM('m','f') NULL,
state VARCHAR(255) NULL,
country VARCHAR(255) NULL,
userlevel ENUM('admin','user') NOT NULL DEFAULT 'user',
ip VARCHAR(255) NOT NULL,
signup DATETIME NOT NULL,
lastlogin DATETIME NOT NULL,
activated ENUM('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (email)
)";
$query = mysqli_query($con, $tbl_users);
if ($query === TRUE) {
echo "<h3>user table created OK :) </h3>";
} else {
echo "<h3>user table NOT created :( </h3>";
}
You need to change your Primary key to the auto_increment column, remove it from email, you can set this (email) to unique if it should be
CREATE TABLE IF NOT EXISTS users (
id INT(11) NOT NULL // >>> PRIMARY KEY //<<< AUTO_INCREMENT,
firstname VARCHAR(255) NOT NULL,
[Err] 1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
PRIMARY KEY (email) is the issue. Make id your primary key.
In my php program i want to check if table is exist or not. if table not exist want to create a table...
the code i've been tried
<?php
if( isset($_POST['uniqueno']))
{
$uniqueno=$_POST['uniqueno'];
$user=$_SESSION['userid'];
$date=date('Y-m-d H:i:s');
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$user."'"))==1)
{ echo "table exists"; }
else
{
$create=mysql_query("create table '$user'( uniqueno varchar(10) NOT NULL,
elementno varchar(30),
name varchar(50),
process varchar(30),
date datetime(30),
PRIMARY KEY (uniqueno)
)");
if(!$create)
{
echo mysql_error();
}
}
}
?>
When i try this code displays following 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 ''sai'( uniqueno varchar(10) NOT NULL, elementno varchar(30), ' at line 1 ".
thank you..
Take out single quote for table name and the query should be something as
"create table $user( uniqueno varchar(10) NOT NULL,
elementno varchar(30),
name varchar(50),
process varchar(30),
date datetime,
PRIMARY KEY (uniqueno)
)"
It should be
$create=mysql_query("create table $user ( uniqueno varchar(10) NOT NULL,
elementno varchar(30),
name varchar(50),
process varchar(30),
date datetime,
PRIMARY KEY (uniqueno)
)");
The single quote around $user is not needed.
The field length is not needed for datetime field.
Try this
$create=mysql_query("create table $user ( uniqueno varchar(10) NOT NULL,
elementno varchar(30),
name varchar(50),
process varchar(30),
date datetime(30),
PRIMARY KEY (uniqueno)
)");
<?php
session_start();
//your db connection code here
if( isset($_POST['uniqueno']))
{
$uniqueno=$_POST['uniqueno'];
$userid=$_POST['userid'];
$user=$_SESSION[$userid];
$date=date('Y-m-d H:i:s');
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$user."'"))==1)
{ echo "table exists"; }
else
{
$create=mysql_query("create table ".$user."(`uniqueno` varchar(10) NOT NULL,`elementno` varchar(30),`name` varchar(50),`process` varchar(30),`date` datetime, PRIMARY KEY(`uniqueno`))");
if(!$create)
{
echo mysql_error();
}
}
}
?>