Hey all,
I have just begun learning and scripting in PHP, and for my first project I am trying to create a very simple CMS. Essentially, for now, I just want to be able to add table to the database, and data into those tables.
Well, the latter works. If a table already exists, I can create entries no problem.
However, actually creating a table is proving very tricky. I keep getting a MySQL syntax error, but I have no idea what's up. I think I need some help debugging this.
Alright, so I have the user type in the name of the new table they want to create. That name is then passed via POST to the actual script, which is essentially this:
mysql_select_db("my_homebase", $connection);
$f = fopen("query.txt","w+");
$thefile = "query.txt";
$header = "CREATE TABLE ";
$string = "
(
cid int NOT NULL AUTO_INCREMENT,
entry longtext NOT NULL,
summary longtext NOT NULL,
title text NOT NULL,
date date NOT NULL,
PRIMARY KEY(cid)
)";
file_put_contents($thefile, $header . $name . $string);
$sql = fread($f, filesize($thefile));
fclose($f);
echo $name;
mysql_query($sql,$connection);
if (!mysql_query($sql,$connection))
{
die('Error: ' . mysql_error());
}
echo "a category has been added";
So basically I have the script write the parts of a query to a file, and then the contents of the file are stored as a string (I assume) in the $sql variable, which is then passed on to the actual query. The resulting file looks like this:
CREATE TABLE update
(
ID int NOT NULL AUTO_INCREMENT,
entry longtext NOT NULL,
summary longtext NOT NULL,
title text NOT NULL,
date date NOT NULL,
PRIMARY KEY(ID)
)
And the error itself simply looks like 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 'update ( cid int NOT NULL AUTO_INCREMENT, entry longtext NOT NULL, summary longt' at line 1
I've checked with several tutorials, and I can't seem to find what the issue is. Perhaps some of you more expert folks, with expert eyes, can help me spot it? I really appreciate the help in advance.