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);
$ok=1;

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

// 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.<br>";
$ok=0;
}

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

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

//Writes file to server
if(move_uploaded_file($_FILES, $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES). " 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.";
}
?>

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')");

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.