Using variables for table names

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Hammer136
Forum Commoner
Posts: 29
Joined: Sat Mar 19, 2005 12:18 pm

Using variables for table names

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

table, as in mysql table?
Hammer136
Forum Commoner
Posts: 29
Joined: Sat Mar 19, 2005 12:18 pm

Post by Hammer136 »

yes as in mysql. soz should of mentioned that.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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=&quote;create_db&quote; action=&quote;create_db.php&quote; method=&quote;POST&quote;>

<input type=&quote;text&quote; name=&quote;&quote; value=&quote;&quote;>
<input type=&quote;submit&quote; name=&quote;submit&quote; value=&quote;submitted&quote;>

</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
Hammer136
Forum Commoner
Posts: 29
Joined: Sat Mar 19, 2005 12:18 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Hammer136
Forum Commoner
Posts: 29
Joined: Sat Mar 19, 2005 12:18 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Why do you need to create a new table for each user.. seems very very unneccesary
Hammer136
Forum Commoner
Posts: 29
Joined: Sat Mar 19, 2005 12:18 pm

Post by Hammer136 »

yeah i suppose so. so instead ill add it as a new row in a table. thanx
Post Reply