954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Create Table with name being the last primary key generated

Hello, I have a script that among other things, creates a table with a variable being the table name. This worked fine when i was using the variable "$name".

Since then I've realized that i need the table name to be "$merchid" (primary key from previous table. I've used "$newid = mysql_insert_id();" to recall the last auto incremented primary key and this echoes just fine, but when i use $newid in place of $name, in the CREATE TABLE query the darn thing breaks, meaning no table is created. The rest of the form functions properly with this error.

Again, this was working fine when using $name from the form data. When i switch to using the primary key grabbed with mysql_insert_id(); it stops working.

Any help would be greatly appreciated!

---------------------------------------------------

<?php

//where images are saved
$target = "../logo/";
$target = $target . basename( $_FILES['logo']['name']);
$ok=1;

//info from form
$name=$_POST['name'];
$type=$_POST['type'];
$tagline=$_POST['tagline'];
$keywords=$_POST['keywords'];
$address=$_POST['address'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$phone=$_POST['phone'];
$fax=$_POST['fax'];
$web=$_POST['web'];
$email=$_POST['email'];
$created=$_POST['created'];
$status=$_POST['status'];
$uploaded=($_FILES['logo']['name']);

// connect to db
mysql_connect("xxx", "xxx", "xxxx") or die(mysql_error()) ;
mysql_select_db("xxx") or die(mysql_error()) ;

//writes the form data to db
mysql_query("INSERT INTO `merchants` VALUES ( '$merchid','$name', '$type', '$tagline', '$keywords', '$address', '$city', '$state', '$zip', '$phone', '$fax', '$web', '$email', '$created', '$modified', '$status', '$uploaded', '$mini')") ;

// calls the last unique id that mysql generated
$newid = mysql_insert_id();
echo $newid;

// creates new table
mysql_query ("CREATE TABLE $newid (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
merchid VARCHAR(100),
custid VARCHAR(100),
modified VARCHAR(100),
created TIMESTAMP DEFAULT NOW())");

// inserts last entered unique id to the new table
mysql_query("INSERT INTO $newid VALUES ('id', '$newid', 'custid', 'modified', 'created')");

//limits upload file size
if ($uploaded_size > 350000)
{
echo "Your file is too large.
";
$ok=0;
}

//file type limits
if ($uploaded_type =="text/php")
{
echo "No PHP files
";
$ok=0;
}

if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}

//Writes file to server
if(move_uploaded_file($_FILES['logo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

strongpot
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This is the area of code in which i am having trouble.

mysql_query("INSERT INTO `merchants` VALUES ( '$merchid','$name', '$type', '$tagline', '$keywords', '$address', '$city', '$state', '$zip', '$phone', '$fax', '$web', '$email', '$created', '$modified', '$status', '$uploaded', '$mini')") ;

// calls the last unique id that mysql generated
$newid = mysql_insert_id();
echo $newid;

// creates new table
mysql_query ("CREATE TABLE $newid (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
merchid VARCHAR(100),
custid VARCHAR(100),
modified VARCHAR(100),
created TIMESTAMP DEFAULT NOW())");

// inserts last entered unique id to the new table
mysql_query("INSERT INTO $newid VALUES ('id', '$newid', 'custid', 'modified', 'created')");

strongpot
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

mysql_insert_id is an integer. You cannot have database, table or field names beginning with numerals. Prefix the table name with some literal constant.

smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: