Page 1 of 1
Using variables for table names
Posted: Sat Mar 19, 2005 12:28 pm
by Hammer136
Im new to php and i am unsure if it is possible to use variable names for table names. i have a form set up so that you enter your name into the form and then i want to create a table with the name of watever is entered into that form. Any help appreciated.
Hammer
Posted: Sat Mar 19, 2005 12:43 pm
by John Cartwright
table, as in mysql table?
Posted: Sat Mar 19, 2005 12:46 pm
by Hammer136
yes as in mysql. soz should of mentioned that.
Posted: Sat Mar 19, 2005 1:12 pm
by John Cartwright
Yes it is very possible. Well, firstly, your going to have to learn how to append the $_POST variable. So lets say you have
Code: Select all
<form name="e;create_db"e; action="e;create_db.php"e; method="e;POST"e;>
<input type="e;text"e; name="e;"e; value="e;"e;>
<input type="e;submit"e; name="e;submit"e; value="e;submitted"e;>
</form>
Now we have to check if the form has been submitted.
Note: You should not check to see if the submit button has been pressed, because ENTER will also submit the form, without the button.
Code: Select all
<?
//to show the form values (debugging)
print_r($_POST);
//shoudl always check for expected values
if (!empty($_POST['db_name']))
{
//create the table
}
?>
You probably going to need more inputs fields so you may choose to create different kinds of table and the column types perhaps.
To learn how to create a table visit
http://dev.mysql.com/doc/mysql/en/create-table.html
Posted: Sat Mar 19, 2005 1:53 pm
by Hammer136
Youve lost me. Currently my code looks like this:
Code: Select all
<?php
include("config.php");
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
// check if the username is taken
$check = "select id from $table where username = '".$_POST['username']."';";
$qry = mysql_query($check)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=register.html>Try again</a>";
exit;
} else {
// insert the data
$insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."', '".$_POST['email']."')")
or die("Could not insert data because ".mysql_error());
$create = "create table [username] (
id smallint(5) NOT NULL auto_increment,
username varchar(30) NOT NULL default '',
email varchar(32) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY username (username)
);";
mysql_query($create)
or die ("Could not create tables because ".mysql_error());
echo "Complete.";
// print a success message
echo "Your user account has been created!<br>";
echo "Now you can <a href=login.html>log in</a>";
}
?>
How do i modify this to allow me to create a table using the username as the table name?
Posted: Sat Mar 19, 2005 1:57 pm
by John Cartwright
Code: Select all
<?
$create = "create table [".$_POST['username']."] (
id smallint(5) NOT NULL auto_increment,
username varchar(30) NOT NULL default '',
email varchar(32) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY username (username)
);";
?>
it doesnt look like you should be creating a new table...
you should only be adding a new row to the table.
Posted: Sat Mar 19, 2005 2:08 pm
by Hammer136
thnax and it works as i can tell from the error but can you look at this:
Could not create tables because You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[ben] ( id smallint(5) NOT NULL auto_increment, email varchar(32) NOT NULL def' at line 1
and tell me what i need to do please. The code is above.
Thanks again
Hammer
Posted: Sat Mar 19, 2005 2:18 pm
by John Cartwright
Why do you need to create a new table for each user.. seems very very unneccesary
Posted: Sat Mar 19, 2005 2:23 pm
by Hammer136
yeah i suppose so. so instead ill add it as a new row in a table. thanx