Jump to content

php mysql script help please


jaylam13

Recommended Posts

Hi,

 

Can anyone help me with a script, I have tried a if statement but I cannot get it to work and its driving me mad.

 

Basically I have a string my website uses to get if the user is logged in what their username is and I want it so when they click a certain link it checks to see if a table all ready exists called their username, if it does it displays a message, if it doesnt it creates the table and if the username is "anonymous" is displays a message.

 

So in short:

 

if $username is the same as a table display "Table all ready exists"

if $username="anonymous" display "You must be logged int"

if $username not the same as a table then create table.

 

Many thanks in advance

 

Jay

Link to comment
Share on other sites

Sorry, its as follows:

 

$dcon = mysql_connect("localhost","*******","******");
if (!$dcon)
  {
  die('Could not connect: ' . mysql_error());
  }
  


// get all tables in the database  
$result = @mysql_list_tables ("*********");  

// loop through result to look for your table  
  while ($tbl=@mysql_tablename($result, $i++))  
{  
      
    if($tbl==$username) 
     {  
      echo "table all ready exist";      
     }  else
     {
       	mysql_select_db("*********", $dcon);
$dsql = "CREATE TABLE $username 
(
DiaryID int NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(DiaryID),
FirstName varchar(15),
LastName varchar(15),
Age int
)";

mysql_query($dsql,$dcon); 
echo "Table created";
        }
}

	?>

 

I haven't attempted the anonymous bit yet as im not sure how to do it if im honest.

 

Jay

Link to comment
Share on other sites

try echoing it to see if your while loop is looping through the existing tables correctly

  while ($tbl=@mysql_tablename($result, $i++))  
{  
   echo $tbl;   
    if($tbl==$username) 
     {  
      echo "table all ready exist";      
     }  else
     {
       	mysql_select_db("*********", $dcon);
$dsql = "CREATE TABLE $username 
(
DiaryID int NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(DiaryID),
FirstName varchar(15),
LastName varchar(15),
Age int
)";

Link to comment
Share on other sites

Ok I got it working using this script but it prints the echo the same amount of times their are tables, so if I have 3 tables created I get "log in log in log in"

 

What am I doing wrong?

 

$dcon = mysql_connect("localhost","****","****");
if (!$dcon)
  {
  die('Could not connect: ' . mysql_error());
  }
  


// get all tables in the database  
$result = @mysql_list_tables ("******");  

// loop through result to look for your table  
  while ($tbl=@mysql_tablename($result, $i++))  
{  
    
    if($username=="Anonymous") 
     {  
      echo "log in";      
     }  elseif($tbl==$username) 
     {
     echo "all ready exists";} else
     {
      echo "No table, <a href='creatediary.php'>create one?</a>";
        }
}

	?>

Link to comment
Share on other sites

Then you should be setting up only two tables, users and diaries. When the user registers you add them in to the users table. When they add a diary entry you insert it into the diaries table. Creating separate tables for each user is bad database design.

Link to comment
Share on other sites

Then you should be setting up only two tables, users and diaries. When the user registers you add them in to the users table. When they add a diary entry you insert it into the diaries table. Creating separate tables for each user is bad database design.

of course.! then I can link the user id in the users table to the user id in the diary entry....

 

Didnt even think of that, thanks for the brain kick....been a long day.

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.