Page 1 of 1
help to create user specified mysql table. help
Posted: Thu Jun 17, 2010 7:21 am
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!!";
?>
Re: help to create user specified mysql table. help
Posted: Thu Jun 17, 2010 11:47 am
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
)";
Re: help to create user specified mysql table. help
Posted: Thu Jun 17, 2010 12:09 pm
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
)";