0

here is the code ,i select the database for allow the user to create their database with all subject name as a table in created database.i am having problem for creating a new database form inside the another database. in order to do that i have to deselect the first selected database. anybody can help me? thanks in advance.

    <?php

    session_start();

    $con=mysql_connect("localhost","root","");

    $dept=$_SESSION['dept'];

    mysql_select_db("$dept",$con);

// getting all data from create html page

    $year=$_POST["adyear"];

    $table=$_POST["year"];

    $sem=$_POST["sem"];

    $databasename=$_POST["tablename"];

    $table1=$_POST["sub1name"];

    $table2=$_POST["sub2name"];

    $table3=$_POST["sub3name"];

    $table4=$_POST["sub4name"];

    $table5=$_POST["sub5name"];

    $table6=$_POST["sub6name"];

    if($year==""||$table1==""||$table2==""||$table3==""||$table4==""||$table5==""||$table6=="")

{


    echo"<script language=javascript>alert('oops..! you cannot leave subject field empty')</script>";


    echo"<META http-equiv='refresh'; content='0; URL=create.php'>";

}

    else

    {


    $sql="create database $dabasename";
        $s=mysql_query($sql);
        if($s)
        {


        $sql="insert into $table(".$year.",".$sem.",".$databasename.")";
            mysql_close();
            $con=mysql_connect("localhost","root","");
            mysql_select_db("$databasename",$con);
`       $sql="create table $table1(REGNO int, NAME varchar(50),Ist_Internal `int,II_Internal int,AVG int,ATTENACE int,FINAL_MARK int)";``

            mysql_query($sql);

            $sql="create table $table2(REGNO int, NAME varchar(50),Ist_Internal int,II_Internal int,AVG int,ATTENACE int,FINAL_MARK int)";


            mysql_query($sql);

    `       $sql="create table $table3(REGNO int, NAME varchar(50),Ist_Internal `int,II_Internal int,AVG int,ATTENACE int,FINAL_MARK int)";


            mysql_query($sql);



            $sql="create table $table4(REGNO int, NAME varchar(50),Ist_Internal int,II_Internal int,AVG int,ATTENACE int,FINAL_MARK int)";


            mysql_query($sql);

            $sql="create table $table5(REGNO int, NAME varchar(50),Ist_Internal int,II_Internal int,AVG int,ATTENACE int,FINAL_MARK int)";


            mysql_query($sql);

            $sql="create table $table6(REGNO int, NAME varchar(50),Ist_Internal int,II_Internal int,AVG int,ATTENACE int,FINAL_MARK int)";


            mysql_query($sql);

            echo"<script language=javascript>alert('table created now you can enter the student details')</script>";


            echo"<META http-equiv='refresh'; content='0; URL=menu.php'>";
        }


    else


    {


        echo"<script language=javascript>alert('oops...! table name already present. Give different tablename')</script>";




    echo"<META http-equiv='refresh'; content='0; URL=create.php'>";
        }

    }

    ?>
funtime
  • 652
  • 1
  • 5
  • 20
Saravanan
  • 337
  • 2
  • 6
  • 17

3 Answers3

0

to close the first database-:

  mysql_close($con);
suspectus
  • 16,548
  • 8
  • 49
  • 57
0

Use mysql_create_db to create the new database and then switch to it by calling mysql_select_db.

Specifically:

if(mysql_create_db($dabasename,$con))
{    
    mysql_select_db($dabasename,$con);
    $sql="insert into $table(".$year.",".$sem.",".$databasename.")";
matt
  • 359
  • 1
  • 7
0

Your idea of the application architecture is wrong.

there should be only one database with one set of tables, all contains user id to distinguish particular user's data.

That's the only way.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345