1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to create Mysql Table and insert resords dynamically?

Discussion in 'PHP' started by as raj, May 2, 2013.

  1. #1
    i want to add dynamically create a table and add a certain value in runtime. The user want to add a dynamic table and add a value.. Give a suggestion about the topic and samples..
     
    as raj, May 2, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Have you worked through any basic mysql & PHP tutorials? There should be some old "guestbook" type examples floating about.

    If you want to add ajax effects to it then there is more to learn but you can pick up the basics really quickly. After that feel free to ask specific questions but what you are trying to do is pretty simple.
     
    sarahk, May 2, 2013 IP
  3. david_of_makurl

    david_of_makurl Greenhorn

    Messages:
    3
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    8
    #3
    here's some code i wrote a while back that showcases dynamic database creation <-- it's the very basics so you should be able to pick it up and advance it to meet your needs --> lets begin

    the html:

    <!DOCTYPE html> // standard html5 page header
    <html> // denotes start of a html page
    <head> // declares the header section
    <title>A Title</title> // title section
    </head> // closes header
    <body> // beginning of body section
     
    <div style="background: #DDD;"> // div to hold the form
    <label>enter a name</label>// used to create a dynamic table in php file
    <form id="game_form" action="mysql_prac.php" method="POST"> // form that posts to the php file
    <input type="submit" name="submit" value="Start" />//standard submit button
    </form> // closes the form
    </div> // closes div
    </body> // closes body
    </html> // closes html document
     
     
    the php:
     
    <?php
    if($_POST['submit']) // proceed if the user submits the form
    {
    $enter_the_mysql = mysql_connect("localhost", "root", ""); // connect to database
     
    if($enter_the_mysql) // if the query was successful
    {
    $db_create = mysql_query("CREATE DATABASE forfun_db"); //creates a database on the fly
     
    $select_db = mysql_select_db("forfun_db"); // checks to see if the database was created
    if($select_db) // if the query is successful
    {
    $get_game_count = mysql_query("SELECT game_count FROM game_count WHERE user='prac_user'"); // irrelevent to this example but there's another table that counts how many tables have been created
    if($get_game_count)// if query is successful
    {
     
    $info = mysql_fetch_assoc($get_game_count); // not really sure how to explain fetch_assoc but if you're familiar with mysql you get it * basically it fetches the data from the mysql table
     
    $i = $info['game_count'] + 1; //increment the number of tables
    $dy_table = "GAME$i"; //creates a table name by adding 1 to the count of created table
    echo $dy_table; // echos it out to the user for future reference
    echo "<br />"; // standard line break
     
    $dynamic_table =mysql_query(" //creates the dynamic table
    CREATE TABLE $dy_table // note the $dy_table variable is the dynamic name for the variable
    (id int NOT NULL AUTO_INCREMENT, // standard table variables
    PRIMARY KEY(id),
    value_one varchar(200), //additional values * may be irrelevant to you
    value_two varchar(200) //additional values * may be irrelevant to you
    )"); // standard closing braces
     
    if($dynamic_table) // if the dynamic table is created
    {
    $game_count_update = mysql_query("UPDATE game_count SET game_count=game_count + 1 WHERE user='prac_user'"); // increment the number of tables created
    if($game_count_update)
    {
    echo "game-table created";
    }else echo mysql_error(); // standard closing braces with mysql_error check
    }else echo mysql_error(); // standard closing braces with mysql_error check
    // }//end of while
    }else echo mysql_error();// standard closing braces with mysql_error check
    }else echo mysql_error();// standard closing braces with mysql_error check
    //}else echo mysql_error();// standard closing braces with mysql_error check
    }else echo mysql_error();// standard closing braces with mysql_error check
    }else echo "inderect entry"; // if the post submit event didn't trigger the file
    ?>
    Code (markup):
     
    Last edited by a moderator: May 2, 2013
    david_of_makurl, May 2, 2013 IP
    Alexbizz and sarahk like this.