help to create user specified mysql table. help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dentrite
Forum Newbie
Posts: 16
Joined: Thu Jun 17, 2010 7:10 am

help to create user specified mysql table. help

Post by dentrite »

i want to create a table which my site user will specify. for example a site user enters table name rockson and request is sent through GET method then it should create the table with this name in my database. i have created a php page as following but it is not working.( included files are correct and tested ) please help. please some1 post the code for it.

Code: Select all

 <?php

session_start();
include("database.php");
include("logins.php");
if($logged_in)
{


// i think this part has error :crazy: 
$sql = "CREATE TABLE $_GET['tbn']
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

// Execute query
mysql_query($sql,$con);

mysql_close($con);

echo"created!!";
?> 
Kurby
Forum Commoner
Posts: 63
Joined: Tue Feb 23, 2010 10:51 am

Re: help to create user specified mysql table. help

Post by Kurby »

This seems like a terrible idea. You shouldn't let users create tables. Unless you have a good reason for this, I just can't imagine what that would be. There is probably an error in your database structure, or how you imagine a database should be created. Perhaps you should look at how a relational database works.

As far as what you really asked for, your syntax error is here. Remove the single quotes around tbn. When you use an array in magic quotes you don't need the single quotes.

Code: Select all

$sql = "CREATE TABLE $_GET[tbn]
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: help to create user specified mysql table. help

Post by AbraCadaver »

Kurby wrote:This seems like a terrible idea. You shouldn't let users create tables. Unless you have a good reason for this, I just can't imagine what that would be. There is probably an error in your database structure, or how you imagine a database should be created. Perhaps you should look at how a relational database works.

As far as what you really asked for, your syntax error is here. Remove the single quotes around tbn. When you use an array in magic quotes you don't need the single quotes.

Code: Select all

$sql = "CREATE TABLE $_GET[tbn]
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

Code: Select all

$table = $_GET['tbn'];

if(get_magic_quotes_gpc()) {
   $table = stripslashes($table);
}
$table = mysql_real_escape_string($table);

$sql = "CREATE TABLE $table
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply