jdock1 Posted September 10, 2010 Share Posted September 10, 2010 What I mean by dynamic is say I created a function to generate a random code. I put it in a variable named $key. I make a form with a hidden input type with the name and value of $key. When the form posts, it generates the table name to whatever $key is. Because I tried something like this & its not querying. Heres my code: <?php $dbc = mysqli_connect('localhost', 'cpacrop_todo', 'pass', 'cpacrop_todo') or die('Failed to connect to database!'); function make_key($num_chars) { if ((is_numeric($num_chars)) && ($num_chars > 0) && (! is_null($num_chars))) { $key = ''; $accepted_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; //seed srand(((int)((double)microtime()*1000003)) ); for ($i=0; $i<=$num_chars; $i++) { $random_number = rand(0, (strlen($accepted_chars) -1)); $key .= $accepted_chars[$random_number] ; } return $key; } } $key = make_key(6); ?> <form action="" method="post"> <input type="hidden" name="<?php echo "$key"; ?>" /> <input type="submit" name="submit" value="Submit" /> </form> <?php if ($_POST['submit'] == "Submit") { $query = "CREATE TABLE `$key` ( `id` INT AUTO_INCREMENT, `title` VARCHAR(35), `description` VARCHAR(365) );"; mysqli_query($dbc,$query) or die('Unable to query database.'); echo "Success"; } ?> If this is possible, what am I doing wrong? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/213075-is-it-possible-to-create-a-table-in-mysql-through-php-dynamicly/ Share on other sites More sharing options...
NLCJ Posted September 10, 2010 Share Posted September 10, 2010 Try doing it like this: CREATE TABLE `".$key."` Quote Link to comment https://forums.phpfreaks.com/topic/213075-is-it-possible-to-create-a-table-in-mysql-through-php-dynamicly/#findComment-1109676 Share on other sites More sharing options...
jdock1 Posted September 10, 2010 Author Share Posted September 10, 2010 Try doing it like this: CREATE TABLE `".$key."` Thanks. I tried that and it didnt work, still giving me the query error. Anybody else no how to do this? I would love to know how to do this so I can complete this script Ive been working on for a while. There has got to be a way to do this, right!? Quote Link to comment https://forums.phpfreaks.com/topic/213075-is-it-possible-to-create-a-table-in-mysql-through-php-dynamicly/#findComment-1109683 Share on other sites More sharing options...
PFMaBiSmAd Posted September 10, 2010 Share Posted September 10, 2010 Where are you setting the variable $key at in your form processing code? Also, it would not be secure to pass such a piece of information through a hidden form field AND dynamically creating a bunch of randomly named tables creates a data management nightmare because you will spend more time keeping track of the tables and their names than you will spend actually querying the information in those tables. You should use 1 (one) table to hold all the same type information and have a column that distinguishes who/what each piece of information in that single table belongs to. Quote Link to comment https://forums.phpfreaks.com/topic/213075-is-it-possible-to-create-a-table-in-mysql-through-php-dynamicly/#findComment-1109684 Share on other sites More sharing options...
PFMaBiSmAd Posted September 10, 2010 Share Posted September 10, 2010 You are also setting the name="..." of the hidden field to the randomly generated key. I suspect that you wanted to set the value="..." to the randomly generated key and wanted to set the name="..." to something that you would then use in your form processing code. Edit: Once you correct the mechanics of what you are doing, you will discover that your query must define the auto-increment column as a key. Use mysqli_error($dbc) to find out what errors your query generates. Quote Link to comment https://forums.phpfreaks.com/topic/213075-is-it-possible-to-create-a-table-in-mysql-through-php-dynamicly/#findComment-1109691 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.