Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts

    can i get table name from FORM?

    Code:
    <html>
    <head>
    </head>
    <body>
    <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" />
    </form>
    
    </body>
    </html>

    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

    Code:
    <?php  //create.php 
    $u=$_POST['user']; 
    $con = mysql_connect("localhost","peter","abc123"); 
    if (!$con) 
      { 
      die('Could not connect: ' . mysql_error()); 
      } 
    
    if (mysql_query("CREATE TABLE '$u'",$con)) 
      { 
      echo "Database created"; 
      } 
    else 
      { 
      echo "Error creating database: " . mysql_error(); 
      } 
    
    mysql_close($con); 
    ?>
    So instead of using table name i am using '$u'.
    Can we do that? Please help

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    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).

    So:
    Code:
    $sql = "CREATE TABLE `$u` (id INT, name VARCHAR(50), email VARCHAR(50), ... etc. ... )";
    *OR* you can use an existing table and copy its structure:
    Code:
    $sql = "CREATE TABLE `$u` LIKE users_table";
    But you really really should think hard about whether this is a good idea!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •