aero4x Posted May 8, 2006 Share Posted May 8, 2006 Hi, I just recently learned some basic PHP and MYSQL in hopes of creating a database driven website. Basically, I'm trying to let users search for something. If a MYSQL table allready exists with a name equal to their search term then great, I know what to do from there, but if no table exists allready, then i would like to create one using their search term as the name of the table. My code looks something like this...[code]<?phpmysql_connect ("localhost", "username", "password") or die(mysql_error());mysql_select_db ("DB_name") or die(mysql_error());$searchTerm = $_POST['searchTerm'];$createTable = "CREATE TABLE " . $searchTerm . "(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), age INT)";$mysqlString = "INSERT INTO " . $searchTerm . " (name, age) VALUES('" . $someVar . "', '" . $anotherVar . "' )";mysql_query($mysqlString) or mysql_query($createTable) or die(mysql_error());[/code]But whenever I use a search term that is not allready in the database i get the following error.[code]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 'test( id INT NOT NULL AUTO_I' at line 1[/code]Even more wierd to me is that whatever the search term I use is displayed as [b]bold[/b] in my error message. Even when I add some random string onto the search term variable, only the part that was actually part of the HTML form is bold. My current theory is that for some reason the HTML forms send some wierd type of a string that doesnt make MYSQL happy but I have no idea. I'm so confused. Any help would be greatly appreciated. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/9336-create-mysql-table-from-html-form-using-php/ Share on other sites More sharing options...
trq Posted May 8, 2006 Share Posted May 8, 2006 My biggest question is, why do you want to do this? Quote Link to comment https://forums.phpfreaks.com/topic/9336-create-mysql-table-from-html-form-using-php/#findComment-34454 Share on other sites More sharing options...
aero4x Posted May 8, 2006 Author Share Posted May 8, 2006 [!--quoteo(post=372445:date=May 8 2006, 06:34 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ May 8 2006, 06:34 PM) [snapback]372445[/snapback][/div][div class=\'quotemain\'][!--quotec--]My biggest question is, why do you want to do this?[/quote]I'm trying to create a site that is largely based on user generated content, so if they search for something that doesn't allready exist, then I would like to make it exist :) Quote Link to comment https://forums.phpfreaks.com/topic/9336-create-mysql-table-from-html-form-using-php/#findComment-34459 Share on other sites More sharing options...
sanfly Posted May 8, 2006 Share Posted May 8, 2006 I also dont quite understand the need for this, but try this anyway.[code]$createTable = "CREATE TABLE '$searchTerm' (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), age INT)";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9336-create-mysql-table-from-html-form-using-php/#findComment-34460 Share on other sites More sharing options...
aero4x Posted May 9, 2006 Author Share Posted May 9, 2006 I get the same error with that code. Quote Link to comment https://forums.phpfreaks.com/topic/9336-create-mysql-table-from-html-form-using-php/#findComment-34667 Share on other sites More sharing options...
trq Posted May 9, 2006 Share Posted May 9, 2006 Creating seperate tables for users/searches is rediculous. I think you need to look into database normalization concepts. Quote Link to comment https://forums.phpfreaks.com/topic/9336-create-mysql-table-from-html-form-using-php/#findComment-34673 Share on other sites More sharing options...
aero4x Posted May 9, 2006 Author Share Posted May 9, 2006 From what I understand, database normalization refers to eliminating redundant data. Searches will be very specific so its not like 2 different users will search for "the boy" and "boy the" and mean the same thing, so I'm not so concerned about redundant data at this point. If I'm misunderstanding what your saying then please correct me, like I said, I'm new to PHP and MYSQL. If anyone else out there knows what im doing wrong with my code, please tell me. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/9336-create-mysql-table-from-html-form-using-php/#findComment-34694 Share on other sites More sharing options...
sanfly Posted May 9, 2006 Share Posted May 9, 2006 Oops, Try this[code]$createTable = "CREATE TABLE $searchTerm (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), age INT)";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9336-create-mysql-table-from-html-form-using-php/#findComment-34700 Share on other sites More sharing options...
aero4x Posted May 9, 2006 Author Share Posted May 9, 2006 [!--quoteo(post=372697:date=May 9 2006, 02:51 PM:name=sanfly)--][div class=\'quotetop\']QUOTE(sanfly @ May 9 2006, 02:51 PM) [snapback]372697[/snapback][/div][div class=\'quotemain\'][!--quotec--]Oops, Try this[code]$createTable = "CREATE TABLE $searchTerm (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), age INT)";[/code][/quote]No luck, same problem, but thanks for the suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/9336-create-mysql-table-from-html-form-using-php/#findComment-34734 Share on other sites More sharing options...
aero4x Posted May 10, 2006 Author Share Posted May 10, 2006 figured it out, thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/9336-create-mysql-table-from-html-form-using-php/#findComment-34814 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.