Jump to content

Is it possible to create a table in mysql through PHP dynamicly?


jdock1

Recommended Posts

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!

Link to comment
Share on other sites

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!?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.