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 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts

    how to take the name from form?

    Hi Awsome Coders. Please bear with me as i am newbie.
    My question is can we name our database from taking the input from form.
    For example
    Code:
    <html>
    <head>
    </head>
    <body>
    <form name="createDatabase" action="create.php" method="post">
    Username:<input type="text" name="user" />
    Password:<input type="password" name="password"/>
    <input type="submit" name="submit" value="Register" />
    </form>
    </body>
    </html>
    PHP 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 DATABASE my_db",$con))
      {
      echo 
    "Database created";
      }
    else
      {
      echo 
    "Error creating database: " mysql_error();
      }

    mysql_close($con);
    ?>
    All i want is name of the database should be the one that is enterd as 'username'. How can we achieve this?
    Last edited by muzammil; 04-12-2011 at 10:42 AM.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    The value entered in "user" will be received by create.php in the superglobal $_POST['user']

    After you have validated it, you can use that value wherever you need to in create.php

  • Users who have thanked bullant for this post:

    muzammil (04-12-2011)

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    How can we achieve this?
    Before that, I'd seriously recommend you to learn about normalisation
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    muzammil (04-12-2011)

  • #4
    New Coder
    Join Date
    Dec 2010
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thanks bullant for bearing with me.
    so the create command will look like this
    Code:
    if (mysql_query("CREATE DATABASE '$u'",$con))
    is it?
    @abduraooft Thanks I will read it. It seems like you read my mind from my post.
    Thanks.

  • #5
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by muzammil View Post
    so the create command will look like this
    Code:
    if (mysql_query("CREATE DATABASE '$u'",$con))
    is it?
    What happened when you ran it? Did it create the database for you?

  • #6
    New Coder
    Join Date
    Dec 2010
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts
    I ran it and following error message came up.

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''98745'' at line 1.

  • #7
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Echo out the actual query you are running to create the database and compare it to the correct syntax.

    The error in your code should then be clear.

  • #8
    New Coder
    Join Date
    Apr 2011
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    I think, you don't need using ' mark around the variable. Try it like that:
    PHP Code:
    if (mysql_query("CREATE DATABASE $u",$con)) 


  •  

    Posting Permissions

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