Hello and welcome to our community! Is this your first visit?
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
    Thanked 0 Times in 0 Posts

    can i get table name from FORM?

    <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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,747 Times in 4,709 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).

    $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!
    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