This piece of code creates the first table with no problems, but then throws "Error Querying Database 2" which is the error for the second CREATE TABLE query. Syntax looks right, but I'm relatively new to this...
$dbc = mysqli_connect("localhost", "$dbuser", "$dbpass") or die('Error connecting to MySQL server.');
mysqli_select_db($dbc, "autoim9_codeaic");
$content = "CREATE TABLE content (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), page varchar(255), html longtext)";
mysqli_query($dbc, $content) or die('Error querying database1');
$events = "CREATE TABLE events (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name varchar(255), date varchar(255), time varchar(255), location varchar(255), html longtext, img varchar(255), link varchar(255), target varchar(255), order int)";
mysqli_query($dbc, $events) or die('Error querying database2');
$spotlight = "CREATE TABLE spotlight (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), title varchar(255), img varchar(255), link varchar(255), target varchar(255), order int)";
mysqli_query($dbc, $spotlight) or die('Error querying database3');
date, time, and order are reserved keywords in MySQL. You either need to change their names or use backticks (`) to escape them.
$events = "CREATE TABLE events (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name varchar(255), `date` varchar(255), `time` varchar(255), location varchar(255), html longtext, img varchar(255), link varchar(255), target varchar(255), `order` int)";
FYI, if you're using varchar(255) for every field you probably are doing it wrong. Date and time fields have their own data types and should be used accordingly.
"Order" is a reserved word, so you need to enclose in back-ticks (or preferably, choose a slightly different field name).
So it's actually an error in your SQL.
Related
I am creating a MySQL database and this doesn't seem to work, i was able to create a review table but now i'm trying to drop that table and create a reviews table but it doesn't seem to work. Please can someone take a look at this and help me check to see what's wrong here?
$reviewsTable = "CREATE TABLE reviews (
ID int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL,
Website varchar(100) NOT NULL,
Review varchar(100) NOT NULL,
TimeOfYear varchar(50),
DayOfYear varchar(50),
PRIMARY KEY (website)
)";
$drop = "DROP TABLE review";
mysqli_query($connect,$drop);
mysqli_query($connect,$reviewsTable);
Just use if exists to drop the table if there is one then create your table.
Id has to be primary key because of the auto increment. all auto increments have to be primary key. You can index website though. but i set id as primary key below this should help.
$reviewsTable = "
DROP TABLE IF EXISTS review;
CREATE TABLE reviews (
ID int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL,
Website varchar(100) NOT NULL,
Review varchar(100) NOT NULL,
TimeOfYear varchar(50),
DayOfYear varchar(50),
PRIMARY KEY (ID)
)";
I am trying to create a table whenever the user submits his/her domain name. It is definitely going to be a .com or a .net or a .something
The problem is that my code does not create a table when it contains a .anything
it works fine for names and characters without a .something
$domain=$_POST['domain_name'];//it is dramatainment.com
$table = mysqli_query($connection, "CREATE TABLE $domain (
user_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(100) NOT NULL,
user_domain VARCHAR(50) NOT NULL,
user_email VARCHAR(50),
user_password VARCHAR(50),
user_date date
) ");
if(!$table)
{
die('Could not create table: ' . mysqli_error($connection));
}
Does the creat table command have this limitation? Can this be solved?
You could try to replace the dot by an underscore.
Here you can find which character is allowed in a table name.
Additionally, it is possible to query using this syntax, which would conflict with your table name :
SELECT * FROM dbname.table_name;
UPDATE : it is possible using backticks to enclose table name :
$sql = 'CREATE TABLE `$tableName` (
user_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(100) NOT NULL,
user_domain VARCHAR(50) NOT NULL,
user_email VARCHAR(50),
user_password VARCHAR(50),
user_date date
)';
Some special characters are invalid for identifiers such as table names.
See http://dev.mysql.com/doc/refman/5.7/en/identifiers.html
I have set up four additional tables for my plugin to use what I am trying to do is take a name and assign it a ID then use this data to populate drop down menus with a name and the same for class and position I am unsure as to how to do this correctly this is what i have so far.
$sql = "CREATE TABLE $tableName (
recordID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(recordID),
driverID int,
driverName varchar(30),
classID int,
driverClass varchar(20),
posID,
driverPosition varchar(6),
trackName varchar(30),
raceDate date
);";
$sql = "CREATE TABLE $driverTableName (
driverID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(driverID),
driverName varchar(30)
);";
$sql = "CREATE TABLE $classTableName (
classID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(classID),
className varchar (20)
);";
$sql = "CREATE TABLE $posTableName (
posID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(posID),
posName varchar(6)
);";
The bottom three tables will store the data I want to populate the drop down boxes to create a record with I am unsure as to how to link them to the top table where this record will be stored.
This is pretty much a indexing issue. If you are going to access the database separately to the standard calls that Wordpress provides, you should at the very least use http://codex.wordpress.org/Class_Reference/wpdb as it will save you some coding time.
The rest of it is a MySQL question. (Assuming you are using MySQL) In how to properly index the data together and then parsing the data as it comes in.
I got this create table statement;
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100),
`old_term` varchar(255),
`new_term` varchar(100),
`same_as` varchar(100),
`last_access` datetime)";
Is there a way to put comments into this statement to the same effect as follows?
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100), //sample value services.media
`old_term` varchar(255), // something like "services.media>category:perspective"
`new_term` varchar(100), // something like "opinion"
`same_as` varchar(100), // the seed id of another record or another "old_term" from this table
`last_update` datetime)"; // when the last update took place
Try following SQL comment syntax, but be careful with " in your text
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100), -- sample value services.media
`old_term` varchar(255), -- something like "services.media>category:perspective"
`new_term` varchar(100), -- something like "opinion"
`same_as` varchar(100), -- the seed id of another record or another "old_term" from this table
`last_update` datetime)"; // when the last update took place
Read more...
you would have to use sql comments in the lines your still in the sql statement. for mysql this would be:
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100), /* sample value services.media */
`old_term` varchar(255), /* something like "services.media>category:perspective" */
`new_term` varchar(100), /* something like "opinion" */
`same_as` varchar(100), /* the seed id of another record or another "old_term" from this table */
`last_update` datetime)"; // when the last update took place
Consider the following tables for a LMS:
Item (
ID BIGINT NOT NULL UNIQUE AUTO_INCREMENT,
ConnectLog BIGINT NOT NULL,
ItemClass BIGINT NOT NULL,
ItemType BIGINT NOT NULL,
AcqDate TIMESTAMP NOT NULL DEFAULT NOW(),
Vendor BIGINT NOT NULL,
Cost DECIMAL(64,2) NOT NULL DEFAULT '0.00',
Image VARCHAR(255),
Access INTEGER NOT NULL,
Notes VARCHAR(255),
PRIMARY KEY (ID)
)
Book (
ID BIGINT NOT NULL UNIQUE AUTO_INCREMENT,
Item BIGINT NOT NULL UNIQUE,
ISBN BIGINT,
Title VARCHAR(255) NOT NULL,
Authors VARCHAR(255),
Publisher VARCHAR(255),
DDC VARCHAR(255),
PubDate DATETIME,
Edition VARCHAR(255),
BookCase VARCHAR(255),
Shelf VARCHAR(255),
PRIMARY KEY (ID)
)
Now when a user makes an entry for Book, first an entry for Item has to be created first. But i need to find the ID for the Item entry that was created so i can use that value for Item in the Book table...
How? :/
Use mysql_insert_id()
// Create Entry
$sql = "INSERT INTO TABLE () VALUES()";
mysql_query($sql);
$id = mysql_insert_id();
// Create Book
$sql = "INSERT INTO TABLE (`Item_ID`) VALUES(".$id.")";
mysql_query($sql);
I bet there is a MySQL Command you could use to do it in a single query. But, this works.
According to this link, you could do an SQL query like:
INSERT INTO foo (auto,text) VALUES(NULL,'text'); # generate ID by inserting NULL
INSERT INTO foo2 (id,text) VALUES(LAST_INSERT_ID(),'text'); # use ID in second table
I haven't tested it, but it seems as though LAST_INSERT_ID() contains the last inserted ID, no matter what table it was inserted into.
If you're worried about heavy loads, and LAST_INSERT_ID() not containing the appropriate entry's ID, you could wrap these SQL statements in a transaction.