Jump to content

Creating tables with PHP and MySQL


Dug
 Share

Recommended Posts

Hey,Ok finally there's something I'm stumped at, so I'm having to ask for advice.I'm writing a script for a Content Management System that will insert articles in to a database. The user should also be able to create a new table for a new category if they wish.Here is my code so far:

<?php include("../includes/DBConnecter.php"); ?><?phpif($_POST['title'] == '') {	echo "You did not enter a title<br>";} else {	$FirstName = $_POST['f_name']; }	if($_POST['category'] = '') {	echo "You did not enter a category<br>";} else {	$Category = $_POST['category']; }	if($_POST['article'] = '') {	echo "You did not enter an article<br>";} else {	$Article = $_POST['article']; }	$sql = 'CREATE TABLE $Category ('		. ' `Title` TEXT NOT NULL, '		. ' `Article` TEXT NOT NULL, '		. ' `Time` TIMESTAMP NOT NULL'		. ' )';		mysql_query($sql,$con);			 ?>

The problem is, with the sql create query - when I try to use "$Category" to create the table name from the form, it creates the table but calles it $Category. Would there be any solution to this?Thanks!Dug

Link to comment
Share on other sites

Here is a fixed version

<?php include("../includes/DBConnecter.php"); ?>if($_POST['title'] == '') {	echo "You did not enter a title<br>";}else{	$FirstName = $_POST['f_name'];}	if($_POST['category'] = '') {	echo "You did not enter a category<br>";}else{	$Category = $_POST['category'];}if($_POST['article'] = '') {	echo "You did not enter an article<br>";}else{	$Article = $_POST['article']; }$sql = "CREATE TABLE ".$Category." (		`Title` TEXT NOT NULL, 		`Article` TEXT NOT NULL, 		`Time` TIMESTAMP NOT NULL		 )";		mysql_query($sql,$con); ?>

Link to comment
Share on other sites

Hi!First, the code from mihalism works and there's no problems with it.But (as a "note") you could skip the "concating operators" (the dots) and just use the variable right in the string (as you had done), but only when using double-quotes.Take a look at this example:

$Hello = 'Hey';echo $Hello.', what's up?'."\n";echo '$Hello what's up?'."\n";echo $Hello.", what's up?\n";echo "$Hello, what's up?\n";// This is what that would output:Hey, what's up?$Hello, what's up?Hey, what's up?Hey, what's up?

Second, Why do you create on table for each category. I wouldn't do like that.I would have two tables one for the categories and one for the articles.In the article-table you have a column with the id of the catagory.Then simply cross-refference the two tables or just use the categorie id from ex. GET or POST.One problem I can see with your design is that you use a name as reference which, among other things, can be misspelled.Then you would have problems with listing the categories, I don''t know a way to loop thru tables, for instance...God Luck and Don't Panic!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...