Jump to content

Recommended Posts

Hi,

 

I have a page where a user types in their username, and gets sent to another page where their account information is extracted from the database and displayed on the page. My database is set up so that each user has their own table.

 

This all works fine, except obviously when someone who does not have their own table in the database types in and submits their name. Basically, I want the script to first check if the user's table already exists. But if it doesn't, I need it to create a table with that username as the table's name.

 

Here's what I was trying:

$user_table = mysql_real_escape_string($_POST["user_select"]);

$result = mysql_query("SELECT * FROM $user_table WHERE id='1'")
or die($create == 1);

if ($create == 1) {
mysql_query("CREATE TABLE $user_table(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
name VARCHAR(20))")
or die(mysql_error()); 
} 

Basically, I just need to define a variable as a number if the original mysql query dies - but the above piece of code doesn't seem to be creating a new table if a non-existing name is submitted. I imagine it's possible to directly place the entire create table function within the Or Die(), but I need to specifically define $create as "1" if the original mysql query fails, as the $create variable will be used in other places.

 

Help would be greatly appreciated, thanks! :)

 

Because you have die(), remove die, so that it becomes:

 

$result = mysql_query("SELECT * FROM $user_table WHERE id='1'") or ($create = 1);

 

I'm sure that's the syntax, because I did it once before.

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.