Jump to content

Question about create dynamic table in mysql!


anargi

Recommended Posts

Hi!

 

I have a question about (dynamic)create table with php script.I don't know if (dynamic) is correct word.

 

I want to create a mysql table but the name of table if it is possible the user to give via html form.

 

my example:

This is simple html form where the user he can to insert two fields(id,name)

<html>
<head>
</head>
<body>

<meta http-equiv="Content-Language"content="en-us-gr">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<form method="POST"  action="categ.php">
<table border="1">
<tr><td>

<div align="right">
ID:
<input type="text" name="id" size="19">
</div>

<div align="right">
Name:
<input type="text" name="name" size="19">
</div>

<div align="right">
<input type="Submit" name="Create" value="create">
</div>

</tr></tr>
</table>

</body>
</head>

These data send in my database via below code:

 

<?php
mysql_connect("localhost","root","MYPASSWORD") or die(mysql_error());
if(mysql_select_db("blog"));
{
mysql_query("SET NAMES 'utf8'");
}
$sql="INSERT INTO menu(id,name)
VALUES ('$_POST[id]','$_POST[name]')";
mysql_query($sql);
{
echo "ok!";
}
/////////////////////////////////////////////////////////////////////////////////////

//[b]And here i want to create a table but the name of table if it is possible to be the name where the user gave via above html form.[/b]

$qry=mysql_query("CREATE TABLE /*The name of the table via above html form*/   (mycode...mpla...mpla)");

if(!$qry)
{
die("Table not created\n".mysql_error());	
}
else
{
echo "Table Created\n";
}
?>
<meta http-equiv="refresh" content="3;url=admin_home.php">

Is there some way to success this result?

 

Thanks!

Link to comment
Share on other sites

Hi

 

Creating / modifying tables in that way is fairly easy. Just use a create statement as you have suggested, but changing the table name.

 

However from a design point of view it is a really bad idea and will result in a quite horrible database, making it very difficult to extract data from it.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.