Jump to content


This topic is now archived and is closed to further replies.


Selecting table name from user input

Recommended Posts

I am creating a PHP page using a MySQL database.  On my page, I have the user register for the first time, and upon registration it automatically creates a table with their username.  I am running into a problem when the user logs into their account.  The user logs in, and it brings them to a page where they can add new information to their table or can search their table.  Since every user is using a separate table, I am running into trouble when I set the table that specific user is using.  I have no problem if it is a static table name, but when the table name comes from a variable set as:
$table = $_REQUEST["user"];
then there is a problem. 

Here is more of my code:
$table = $_REQUEST["user"];

$result = mysql_query("SELECT * FROM $table Where Name='$name'");while($row = mysql_fetch_array($result))

Then later in the code:
$sql = "INSERT INTO $table (name, address, city, state, zip) VALUES ('$name', '$address', '$city', '$state', '$zip')";

This results in an error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Test\add.php on line 54
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 '(name, address, city, state, zip) VALUES ('l', 'j', 'j', 'j', 'j')' at line 1

The $_REQUEST[“user”] comes from a different page.  This is done on the initial logon screen.  After the username and password is submitted correctly it brings you to a page that asks if you wish to search or add a new record.  Then if you go to the add a new record page(haven’t worked with the search page yet!) this is where the code above is located. 

Will using, $table = $_REQUEST["user"];, allow me to access the data from this variable a couple pages deep into my project?  Is this global, so should any page have access to it?  Is there something in MySQL that will not allow a variable such as the one I am calling?

Thanks for any help you can provide!!!!!!!!!!!

Share this post

Link to post
Share on other sites
you literally made a new table for every single user? or did you make a whole new row?

Share this post

Link to post
Share on other sites


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.