Jump to content

Selecting table name from user input


jones53084

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!!!!!!!!!!!
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.