Get ready for BLACK FRIDAY shopping starting in

Forums

PHP

This topic is locked

Dynamic Database Table

Posted 04 Nov 2006 10:59:04
1
has voted
04 Nov 2006 10:59:04 Brijesh Srivastava posted:
I am creating my website where i need to create a dynamic table in the database.
I will be very gladefull to the person who will tell me how can i do that, Because i am a fresh Programmer with less experiance.

Thank you,
Brijesh

Replies

Replied 06 Nov 2006 13:53:55
06 Nov 2006 13:53:55 Roddy Dairion replied:
dynamic table???
Replied 07 Nov 2006 03:58:08
07 Nov 2006 03:58:08 david knaq replied:
Do you mean you would like to populate a table from the database?
Replied 07 Nov 2006 07:55:05
07 Nov 2006 07:55:05 Brijesh Srivastava replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
dynamic table???
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Ya, Dyanamic table, Mean I want to create table at Run time...
Replied 07 Nov 2006 07:55:52
07 Nov 2006 07:55:52 Brijesh Srivastava replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
Do you mean you would like to populate a table from the database?
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

No, I want to create a new table in database at runtime
Replied 07 Nov 2006 10:37:57
07 Nov 2006 10:37:57 Roddy Dairion replied:
database at runtime??? Do u have access to create databases?? MAke you don't mix database and table. If you have access it can be done just by making you php script execute an sql statement for creating databases.
Replied 07 Nov 2006 10:47:59
07 Nov 2006 10:47:59 Brijesh Srivastava replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
database at runtime??? Do u have access to create databases?? MAke you don't mix database and table. If you have access it can be done just by making you php script execute an sql statement for creating databases.
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Ya, I have access to database, that I also know but Problem is how can i write PHP code for making table

I have written code like this:

&lt;?
//-------------------------------------developed by: Brijesh Srivastava---------------------//


include("./common/config.php";
include("$LIB_DIR/functions.php";

$INT=$_REQUEST['int'];
$INT1=$_REQUEST['int1'];


if($INT==1)
{
$TABLE_NAME=$_REQUEST['table_name'];
$ENTITY_NUMBER=$_REQUEST['no_entity'];

for ($i=1; $i&lt;=$ENTITY_NUMBER; $i++)
{
$PRINT_TABLE.= "&lt;tr&gt;
&lt;td&gt;
&lt;div align=left&gt;
&lt;input name=entity_name$i type=text id=entity_name size=20&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align=center&gt;
&lt;select name=entity_type$i id=entity_type&gt;
&lt;option&gt;Select&lt;/option&gt;
&lt;option value=bigint&gt;bigint&lt;/option&gt;
&lt;option value=blob&gt;blob&lt;/option&gt;
&lt;option value=char&gt;char&lt;/option&gt;
&lt;option value=double&gt;double&lt;/option&gt;
&lt;option value=date&gt;date&lt;/option&gt;
&lt;option value=datetime&gt;datetime&lt;/option&gt;
&lt;option value=decimal&gt;decimal&lt;/option&gt;
&lt;option value=enum&gt;enum&lt;/option&gt;
&lt;option value=float&gt;float&lt;/option&gt;
&lt;option value=integer&gt;integer&lt;/option&gt;
&lt;option value=longblob&gt;longblob&lt;/option&gt;
&lt;option value=longtext&gt;longtext&lt;/option&gt;
&lt;option value=mediumblob&gt;mediumblob&lt;/option&gt;
&lt;option value=mediumint&gt;mediumint&lt;/option&gt;
&lt;option value=mediumtext&gt;mediumtext&lt;/option&gt;
&lt;option value=set&gt;set&lt;/option&gt;
&lt;option value=smallint&gt;smallint&lt;/option&gt;
&lt;option value=text&gt;text&lt;/option&gt;
&lt;option value=timestamp&gt;timestamp&lt;/option&gt;
&lt;option value=tinyblob&gt;tinyblob&lt;/option&gt;
&lt;option value=tinyint&gt;tinyint&lt;/option&gt;
&lt;option value=tinytext&gt;tinytext&lt;/option&gt;
&lt;option value=varchar selected&gt;varchar&lt;/option&gt;
&lt;option value=year&gt;year&lt;/option&gt;
&lt;option value=geometry&gt;geometry&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align=center&gt;
&lt;input name=entity_size$i type=text id=entity_size size=10&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align=center&gt;
&lt;input name=entity_dec$i type=text id=entity_dec size=5&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align=center&gt;
&lt;select name=value_null$i id=value_null&gt;
&lt;option value=Null selected&gt;Null&lt;/option&gt;
&lt;option value=Not Null&gt;Not Null&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align=center&gt;
&lt;select name=primary$i id=primary&gt;
&lt;option value=Yes&gt;Yes&lt;/option&gt;
&lt;option value=No selected&gt;No&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align=center&gt;
&lt;select name=extra$i id=extra&gt;
&lt;option value=&gt;Select&lt;/option&gt;
&lt;option value=auto-increment&gt;auto-increment&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;
&lt;div align=right&gt;
&lt;input name=default_value$i type=text id=default_value size=5&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;";

}



$TOPBAR = ReadTemplate("$TEMPLATE_DIR/common/header.html";
$MIDDLEPANEL = ReadTemplate("$TEMPLATE_DIR/user_registration/enter_table_entities.html";
$FOOTER = ReadTemplate("$TEMPLATE_DIR/common/footer.html";
$SUBTEMPLATE = ReadTemplate("$TEMPLATE_DIR/common/subtemplate.html";
$TEMPLATE = ReadTemplate("$TEMPLATE_DIR/common/template.html";

ReplaceContent(Array("TOPBAR","MIDDLEPANEL","FOOTER","SUBTEMPLATE","TEMPLATE");
print $TEMPLATE;
flush();
exit;
}




if($INT1==2)
{
$FINAL_TABLE_NAME=$_REQUEST['table_name'];
$TOTAL_ENTITY=$_REQUEST['entity_no'];

for ($i=1; $i&lt;=$TOTAL_ENTITY; $i++)
{ $SN=1;
$ENTITY_NAME.$SN=$_REQUEST[entity_name.$i];
$ENTITY_TYPE.$SN=$_REQUEST[entity_type.$i];
$SIZE.$SN=$_REQUEST[entity_size.$i];
$DECIMAL.$SN=$_REQUEST[entity_dec.$i];
$NULL.$SN=$_REQUEST[value_null.$i];
$PRIM_KEY.$SN=$_REQUEST[primary.$i];
$EXTRA.$SN=$_REQUEST[extra.$i];
$DEFAULT_VALUE.$SN=$_REQUEST[default_value.$i];
$SN=$SN+1;
}

<font color=red>$query="CREATE TABLE $FINAL_TABLE_NAME (
admin_user_name varchar(50) default NULL,
PRIMARY KEY (admin_user_name)
) TYPE=MyISAM";
$res=mysql_query($query,$db);</font id=red>


$TOPBAR = ReadTemplate("$TEMPLATE_DIR/common/header.html";
$MIDDLEPANEL = ReadTemplate("$TEMPLATE_DIR/user_registration/create_database.html";
$FOOTER = ReadTemplate("$TEMPLATE_DIR/common/footer.html";
$SUBTEMPLATE = ReadTemplate("$TEMPLATE_DIR/common/subtemplate.html";
$TEMPLATE = ReadTemplate("$TEMPLATE_DIR/common/template.html";

ReplaceContent(Array("TOPBAR","MIDDLEPANEL","FOOTER","SUBTEMPLATE","TEMPLATE");
print $TEMPLATE;
flush();
exit;
}

$TOPBAR = ReadTemplate("$TEMPLATE_DIR/common/header.html";
$MIDDLEPANEL = ReadTemplate("$TEMPLATE_DIR/user_registration/create_database.html";
$FOOTER = ReadTemplate("$TEMPLATE_DIR/common/footer.html";
$SUBTEMPLATE = ReadTemplate("$TEMPLATE_DIR/common/subtemplate.html";
$TEMPLATE = ReadTemplate("$TEMPLATE_DIR/common/template.html";

ReplaceContent(Array("TOPBAR","MIDDLEPANEL","FOOTER","SUBTEMPLATE","TEMPLATE");
print $TEMPLATE;
flush();
exit;

?&gt;
<b></b><font color=red></font id=red>
Replied 07 Nov 2006 10:51:57
07 Nov 2006 10:51:57 Brijesh Srivastava replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
database at runtime??? Do u have access to create databases?? MAke you don't mix database and table. If you have access it can be done just by making you php script execute an sql statement for creating databases.
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Now u see red code in the above code every thing is working fine till that red code, but will creating a new table I am unable to create entities of the table.

so, can you suggest me can i write code like that...
If yes then how can I do that...
Replied 07 Nov 2006 10:57:56
07 Nov 2006 10:57:56 Roddy Dairion replied:
using an sql statement to create a table. But do u have a renaming system for that though???? For e.g. if i create a table using php i need to be able to rename table and not creating the same table over n over again. Now when you say create a table in the database at runtime, what name will you give the table?
Replied 08 Nov 2006 06:19:19
08 Nov 2006 06:19:19 Brijesh Srivastava replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
using an sql statement to create a table. But do u have a renaming system for that though???? For e.g. if i create a table using php i need to be able to rename table and not creating the same table over n over again. Now when you say create a table in the database at runtime, what name will you give the table?
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

well I am taking that name and number of columns of the table first then I am taking the entities name and there type and storing in a variable.

After this I am unable to do the coding.

Main thing is SQL statement for creating table. when I am checking the query direct in database then it create the table but when I am doing that through coding it is not making the table.

now I am giving coding for HTML also I request you to run this file and than u come to know how is the work flow.

First HTML CODING::

<font color=red>Name this html page :- create_database.html</font id=red>

&lt;table width="750" border="0" align="center" cellpadding="0" cellspacing="0"&gt;
&lt;form action="create_database.php?int=1" method="post" name="create_database"&gt;
&lt;tr&gt;
&lt;td colspan="2"&gt;&lt;div align="center"&gt;&lt;strong&gt;CREATING DATABASE TABLE &lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="2"&gt;&nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="2"&gt;&nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="140"&gt;&lt;strong&gt;Table Name&lt;/strong&gt;&lt;/td&gt;
&lt;td width="610"&gt;&lt;input name="table_name" type="text" id="table_name"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Number of Entities &lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;input name="no_entity" type="text" id="no_entity"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="2"&gt;&nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="2"&gt;&lt;div align="center"&gt;
&lt;input type="submit" name="Submit" value="GO"&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/form&gt;
&lt;/table&gt;



Second HTML CODING::

<font color=red> Name this HTML page:- enter_table_entities.html </font id=red>

&lt;table width="750" border="0" align="center" cellpadding="0" cellspacing="0"&gt;
&lt;form action="create_database.php?int1=2" method="post" name="create_database1"&gt;
&lt;input type="hidden" name="entity_no" value="__ENTITY_NUMBER__"&gt;
&lt;input type="hidden" name="table_name" value="__TABLE_NAME__"&gt;
&lt;tr&gt;
&lt;td width="139"&gt;&lt;div align="center"&gt;&lt;strong&gt;Entity Name&lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;
&lt;td width="148"&gt;&lt;div align="center"&gt;&lt;strong&gt;Type&lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;
&lt;td width="79"&gt;&lt;div align="center"&gt;&lt;strong&gt;Size&lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;
&lt;td width="51"&gt;&lt;div align="center"&gt;&lt;strong&gt;Decimal&lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;
&lt;td width="48"&gt;&lt;div align="center"&gt;&lt;strong&gt;Null&lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;
&lt;td width="72"&gt;&lt;div align="center"&gt;&lt;strong&gt;Primary Key &lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;
&lt;td width="121"&gt;&lt;div align="center"&gt;&lt;strong&gt;Extra&lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;
&lt;td width="92"&gt;&lt;div align="center"&gt;&lt;strong&gt;Default Value &lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="8"&gt;__PRINT_TABLE__&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="8"&gt;&nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="8"&gt;&lt;div align="center"&gt;
&lt;input type="submit" name="Submit" value="Submit"&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/form&gt;
&lt;/table&gt;


<font color=red>Check the PHP red coding that is a dummy SQL statement I am just checking that it make the table in database or not...</font id=red>
Replied 10 Nov 2006 10:31:29
10 Nov 2006 10:31:29 Nurudin Jauhari replied:
Dynamic Table =&gt; Dynamics data ??

Life for Others

Reply to this topic