jaylam13 Posted May 20, 2011 Share Posted May 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/ Share on other sites More sharing options...
fugix Posted May 20, 2011 Share Posted May 20, 2011 can you show your code please Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/#findComment-1218048 Share on other sites More sharing options...
jaylam13 Posted May 20, 2011 Author Share Posted May 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/#findComment-1218049 Share on other sites More sharing options...
fugix Posted May 20, 2011 Share Posted May 20, 2011 have you tried debugging...like echoing $tbl to make sure your while loop is working properly? Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/#findComment-1218054 Share on other sites More sharing options...
jaylam13 Posted May 20, 2011 Author Share Posted May 20, 2011 I havent to be honest as im a newbie to php Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/#findComment-1218055 Share on other sites More sharing options...
fugix Posted May 20, 2011 Share Posted May 20, 2011 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 )"; Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/#findComment-1218058 Share on other sites More sharing options...
jaylam13 Posted May 20, 2011 Author Share Posted May 20, 2011 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/#findComment-1218101 Share on other sites More sharing options...
wildteen88 Posted May 20, 2011 Share Posted May 20, 2011 Any reason why you're creating a new table for each user? Why not just have one tabled called users which stores all users personal details. Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/#findComment-1218126 Share on other sites More sharing options...
jaylam13 Posted May 20, 2011 Author Share Posted May 20, 2011 Because I want the users to be able to keep an online aquarium diary which they will be able to run charts from showing their progress. Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/#findComment-1218137 Share on other sites More sharing options...
wildteen88 Posted May 20, 2011 Share Posted May 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/#findComment-1218142 Share on other sites More sharing options...
jaylam13 Posted May 20, 2011 Author Share Posted May 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236968-php-mysql-script-help-please/#findComment-1218145 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.