Using variables for table names
Moderator: General Moderators
Using variables for table names
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
Hammer
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Yes it is very possible. Well, firstly, your going to have to learn how to append the $_POST variable. So lets say you have
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.
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
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>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
}
?>To learn how to create a table visit
http://dev.mysql.com/doc/mysql/en/create-table.html
Youve lost me. Currently my code looks like this:
How do i modify this to allow me to create a table using the username as the table name?
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>";
}
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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)
);";
?>you should only be adding a new row to the table.
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
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: