phpserver Posted January 15, 2010 Share Posted January 15, 2010 Hello,i have this html form. <html> <body> <form action="create.php" method="post"> Class: <input type="text" name="class" /> Year: <input type="text" name="year" /> <input type="submit" /> </form> </body> </html> I want to create using the post values,possibly concatenate class.year and that will be the table name in mysql. <?php $con = mysql_connect("localhost","wikinger","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // Create database if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } // Create table mysql_select_db("my_db", $con); $sql = "CREATE TABLE Persons //How do i replace Persons with '$_POST[class]'.'$_POST[year]' ( FirstName varchar(15), LastName varchar(15), Age int )"; // Execute query mysql_query($sql,$con); mysql_close($con); ?> I want to use '$_POST[class]'.'$_POST[year]' as my table name.How do i do it in my case?. Quote Link to comment https://forums.phpfreaks.com/topic/188549-naming-a-mysql-in-php-using-a-forms-post-value/ Share on other sites More sharing options...
trq Posted January 15, 2010 Share Posted January 15, 2010 Why would you want to create multiple databases is the big question? Smells of poor design, anyway.... $class = mysql_real_escape_string($_POST['class']); $year = mysql_real_escape_string($_POST['year']); $sql = "CREATE TABLE {$class}{$year} ( FirstName varchar(15), LastName varchar(15), Age int )"; The user your php scripts connect with shouldn't even have permissions to create tables in my opinion, but on shared hosting and the like you r probably won't have much choice in the matter. Quote Link to comment https://forums.phpfreaks.com/topic/188549-naming-a-mysql-in-php-using-a-forms-post-value/#findComment-995427 Share on other sites More sharing options...
phpserver Posted January 15, 2010 Author Share Posted January 15, 2010 First of all thank you very much for the help.I am making a software too run on the server2go,so its a software running on a cd.The client has a massive database that he needs to key in and so i thought it will be better to create a unique table for classes.I figured it could get fairly complicated since i am using extjs editable grid and i want a situation where the amount of joins are a minimum. Quote Link to comment https://forums.phpfreaks.com/topic/188549-naming-a-mysql-in-php-using-a-forms-post-value/#findComment-995468 Share on other sites More sharing options...
phpserver Posted January 15, 2010 Author Share Posted January 15, 2010 Why would you want to create multiple databases is the big question? Smells of poor design, anyway.... $class = mysql_real_escape_string($_POST['class']); $year = mysql_real_escape_string($_POST['year']); $sql = "CREATE TABLE {$class}{$year} ( FirstName varchar(15), LastName varchar(15), Age int )"; The user your php scripts connect with shouldn't even have permissions to create tables in my opinion, but on shared hosting and the like you r probably won't have much choice in the matter. Your code do not work! Quote Link to comment https://forums.phpfreaks.com/topic/188549-naming-a-mysql-in-php-using-a-forms-post-value/#findComment-995569 Share on other sites More sharing options...
Buddski Posted January 15, 2010 Share Posted January 15, 2010 Firstly, saying "Your code dont work" is not going to get you a nice response.. Have you thought of trying to find out WHY it doesnt work? A little debugging perhaps. mysql_query(...) or trigger_error(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/188549-naming-a-mysql-in-php-using-a-forms-post-value/#findComment-995575 Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2010 Share Posted January 15, 2010 And actually as soon as you put any code into one of your files and attempt to execute it on your server, it becomes your code. You are responsible at that point for at least stating what symptom you see in front of you when you try it, because as has already been pointed out, telling someone that code does not work is useless information. Quote Link to comment https://forums.phpfreaks.com/topic/188549-naming-a-mysql-in-php-using-a-forms-post-value/#findComment-995578 Share on other sites More sharing options...
Buddski Posted January 15, 2010 Share Posted January 15, 2010 10 bucks says if it worked it would be 'his' code.. Quote Link to comment https://forums.phpfreaks.com/topic/188549-naming-a-mysql-in-php-using-a-forms-post-value/#findComment-995583 Share on other sites More sharing options...
phpserver Posted January 17, 2010 Author Share Posted January 17, 2010 $class = mysql_real_escape_string($_POST['class']); $year = mysql_real_escape_string($_POST['year']); $sql = "CREATE TABLE $class.$year ( FirstName varchar(15), LastName varchar(15), Age int)"; The code above worked,sorry if you took it the wrong way on the "Your code do not work",thingy,i tend to be straight,if you are thinking i was rude or trying to,maybe you are over analysing things. Quote Link to comment https://forums.phpfreaks.com/topic/188549-naming-a-mysql-in-php-using-a-forms-post-value/#findComment-996707 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.