View Full Version : can i get table name from FORM?

04-12-2011, 02:20 PM
<form name="register" action="create.php" method="post">
username:<input type="text" name="user" />
e-mail:<input type="text" name="email" />
<input type="submit" name="submit" value="register" />


I want to create a table in my database and the name of the table should be fetched from the user name. Is it Possible?? I m trying following code its not working for me

<?php //create.php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
die('Could not connect: ' . mysql_error());

if (mysql_query("CREATE TABLE '$u'",$con))
echo "Database created";
echo "Error creating database: " . mysql_error();


So instead of using table name i am using '$u'.
Can we do that? Please help

Old Pedant
04-12-2011, 08:32 PM
It's probably a really really bad idea. Unless you expect to have no more than, say, 10 users or so, the concept of one table per user is NOT something you should do. And I'm not convinced it's a good idea even for as few as a handful of users.

But can it be done? Of course.

But you have to specify *ALL* the fields in the table when you create it.

And you can *NOT* put apostrophes around the table name. You must use back-ticks (the ` character is usually on the same key as the ~ character).


$sql = "CREATE TABLE `$u` (id INT, name VARCHAR(50), email VARCHAR(50), ... etc. ... )";

*OR* you can use an existing table and copy its structure:

$sql = "CREATE TABLE `$u` LIKE users_table";

But you really really should think hard about whether this is a good idea!