+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
Web Development Thread, Creating a new database in php and mysql in Coding and Web Development; I'm trying to pass a variable from a form, to create a new database. something like this... $firstname = $_post['firstname']; ...
  1. #1
    StewartKnight's Avatar
    Join Date
    Jun 2005
    Posts
    1,587
    Thank Post
    2
    Thanked 27 Times in 21 Posts
    Rep Power
    30

    Creating a new database in php and mysql

    I'm trying to pass a variable from a form, to create a new database.
    something like this...


    $firstname = $_post['firstname'];

    $query = 'create database $firstname';

    mysql_query($query);
    I'm using the latest versions of mysql and php.
    Can anyone tell me how to create a new database using a variable???

  2. #2

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364

    Re: Creating a new database in php and mysql

    Quote Originally Posted by StewartKnight
    I'm trying to pass a variable from a form, to create a new database.
    something like this...


    $firstname = $_post['firstname'];

    $query = 'create database $firstname';

    mysql_query($query);
    I'm using the latest versions of mysql and php.
    Can anyone tell me how to create a new database using a variable???

    Just looked at this site

    http://www.php-mysql-tutorial.com/cr...e-with-php.php

    From what that is saying it looks like you would have to do something like so :

    $query = "CREATE DATABASE " & $firstname;
    $result = mysql_query($query);

    '---------

    you obviously dont have to assign the query to $result but that is how they have dont it so I just left it as is

  3. #3

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,403
    Thank Post
    637
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    319

    Re: Creating a new database in php and mysql

    If you use single quotes, you must escape them to include variables.

    Code:
    $query = 'create database ' . $firstname;
    This would also work:

    Code:
    $query = "create database $firstname";
    You might want to look into some validation and security routines for the POST variable (protect against SQL injection etc) as it could potentially allow people to inject rogue queries.

  4. #4

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364

    Re: Creating a new database in php and mysql

    @ webman

    Never knew you could include variables inside of a string in php as per

    $query = "create database $firstname";

    Maybe I did it wrong above :-S *shrugs*

  5. #5
    Friez's Avatar
    Join Date
    Dec 2006
    Posts
    839
    Thank Post
    22
    Thanked 22 Times in 21 Posts
    Rep Power
    23

    Re: Creating a new database in php and mysql

    It would generally be a BAD idea to create anything that makes databases off the hoof, HECK there shouldn't even be a need to dynamically create tables (you can end up with some nasty nasty issues). A properly structured database should have perfectly normalised tables.

    However, if you're just wanting to create structures first time round and be done with it forever more (possibly with a few tweaks later on), you should check out PHPMyAdmin http://www.phpmyadmin.net/home_page/index.php

    It'll let you set up databases, tables, and all that malarky and also let you directly enter SQL.

  6. #6
    StewartKnight's Avatar
    Join Date
    Jun 2005
    Posts
    1,587
    Thank Post
    2
    Thanked 27 Times in 21 Posts
    Rep Power
    30

    Re: Creating a new database in php and mysql

    Webman & gecko, neither of your methods worked.

    What I want is this;

    The ICT teacher want the kids to produce a form that collects data and stores it in a database.

    I want a form where the kids create a database with their name as the db name. The fields for the db are setup by the php script once the db has been created.

    I'm tearing my hair out on what I thought would be a simple thing to achive. I can use the php to create a statically named db, but not a variable.

  7. #7

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,403
    Thank Post
    637
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    319

    Re: Creating a new database in php and mysql

    Try echoing the SQL query before running it and see if it is producing the right results.

  8. #8

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364

    Re: Creating a new database in php and mysql

    does it have to be in php ?

    Just I have used classic ASP a lot more then I have with php, cant try anything out now but will have a go with php if your insistant on using php.

  9. #9
    StewartKnight's Avatar
    Join Date
    Jun 2005
    Posts
    1,587
    Thank Post
    2
    Thanked 27 Times in 21 Posts
    Rep Power
    30

    Re: Creating a new database in php and mysql

    anything that works really!

  10. #10

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364

    Re: Creating a new database in php and mysql

    Quote Originally Posted by StewartKnight
    Webman & gecko, neither of your methods worked.

    What I want is this;

    The ICT teacher want the kids to produce a form that collects data and stores it in a database.

    I want a form where the kids create a database with their name as the db name. The fields for the db are setup by the php script once the db has been created.

    I'm tearing my hair out on what I thought would be a simple thing to achive. I can use the php to create a statically named db, but not a variable.
    What database format are you wanting to use ?

    MS Access , SQL or something else ??

    Just each database format has and uses a different connection string, also with ms access it locks you out after so many people use it unless you do some weird stuff ( which I have never done before ) in which case it creates virtual tables and it slows down the more people that access it.

    Kind of like sleuth lol.

    If you want to use SQL then I wont be able to help with creating of the sql database but I can use php to write to and access the info from the sql database, provided I know the design of the tables ie what fields are in the table and in what order ie if you have tblCustomer with fields

    Customer_ID
    Forename
    Surname
    Age
    Date_Of_Birth

    Then I need to know those fields in the precise order that they are in.

  11. #11

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,403
    Thank Post
    637
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    319

    Re: Creating a new database in php and mysql

    StewartKnight: Could you post the code to my pastebin (omitting MySQL username/password) to have a look at it and see where the problem is?

  12. #12
    Friez's Avatar
    Join Date
    Dec 2006
    Posts
    839
    Thank Post
    22
    Thanked 22 Times in 21 Posts
    Rep Power
    23

    Re: Creating a new database in php and mysql

    StewartKnight: Make tables!!, not only is there no point in creating (what hundreds? thousands? of) DATABASES but theres no need to even make multiple tables on the fly. Absolute mess! And besides, your SQL engine will probably cry.

    Create some table structures. Here's some food for thought:


    Table: tblUsers // a list of your lovely students
    UserID (autoincrement BIGINT) [Primary Key]
    Username
    Firstname
    Lastname
    // yada yada

    Table: tblStudent_Tables // describes student tables/forms
    TableID (autoincrement BIGINT) [Primary Key]
    TableName (string)
    UserID // the User that this table is associated with

    // this is the magic bit -- linking up fields to a table
    Table: tblStudent_Table_Link // links in individual fields to their table
    TableID (unique) // links to the relevant table
    FieldID (unique) // links to the relevant field

    Table: tblStudent_Fields // this table will describe the fields of a students table
    FieldID (autoincrement BIGINT)[Primary Key]
    FieldTitle (string)
    FieldDefaultValue (string) // possibly allow for a default value to be entered

    // THUSFAR we have only described the students table.
    // now lets think about storing that data

    Table: tblFieldStorage // basically data that people entered in each field
    FieldID (unique) // links to the field it is relevant to
    FieldValue (string) // the data said person entered into that field
    SessionID (unique) // the overall data input session this field is assocaited with

    Table tblStudent_Form_Sessions
    SessionID (autoincrement BIGINT) [primary key]
    // plus anything else that you may want to autocollect like names and IP's that are not dynamically created by students HERE

    So lets re-view:

    // we describe the students custom form/table structure like so:

    tblStudent_Tables describes the table
    tblStudent_Fields describes all the fields in their table
    tblStudent_Table_Link links fields with tables

    // we store arbitary data that they create like so:

    tblFieldStorage stores the values of a field and links it to a specific data entry session
    tblStudent_Form_Sessions stores a list of sessions


    You should NEVER have the need to create databases or tables on-the-fly structured query language isn't meant for that! Create some structures and you're away. That should be able to store all the data that you want.

    Of course, you'll have to code a fair bit of PHP to support this kind of stuff. But you're asking for that with such a dynamic system to begin with.

    PHP / mySQL is a good choice, your life could become hell with other techs.

  13. #13

    Join Date
    Jan 2007
    Location
    Lowestoft, Suffolk
    Posts
    84
    Thank Post
    6
    Thanked 4 Times in 4 Posts
    Rep Power
    16

    Re: Creating a new database in php and mysql

    I agree with Friez and others who've suggested tables rather than databases. I'd say the teacher used the word database as a generic term, but didn't actually mean a unique database per student.

    Find out what the teacher want them to store and set up tables ready to accept that info.

    The other problem with creating a database in granting permissions to access it. That's probably why you can't create one programmatically. If you don't run the server, your ISP may have blocked you from creating a database.

    Also worth checking the mysql_error() after trying to create the database.

  14. #14

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364

    Re: Creating a new database in php and mysql

    I agree with the above, you might want to consider creating tables on the fly with each table having there own name and surname so that way if there are 2 or more students with the name Smith for example then you dont want one student called smith over writing the other smiths table.

  15. #15

    Join Date
    Jan 2007
    Location
    Lowestoft, Suffolk
    Posts
    84
    Thank Post
    6
    Thanked 4 Times in 4 Posts
    Rep Power
    16

    Re: Creating a new database in php and mysql

    That's a good point. I'd use LDAP* and use their username as the table name if individual tables are required. But really a pre defined table or tables would be the best way to go.

    I've just finished the first version of a survey system written is PHP and mySQL, it uses LDAP to find their username and store their results.

    The first refinement will be to look at group membership and only allow certain group members to take the survey.


    *LDAP is a bit of devil to get working, download this LDAP Browser if you're going to give it a shot, it's the best way to find out all the parameters for your AD.

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. MYSQL Help
    By danIT in forum General Chat
    Replies: 2
    Last Post: 18th December 2007, 09:52 PM
  2. Replies: 4
    Last Post: 28th February 2007, 09:37 AM
  3. using mysql & php on a W2K SP2 server
    By beeswax in forum Web Development
    Replies: 44
    Last Post: 29th June 2006, 08:54 AM
  4. Replies: 2
    Last Post: 28th March 2006, 12:15 PM
  5. php MySql
    By iking in forum Books and Manuals
    Replies: 0
    Last Post: 11th July 2005, 03:35 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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