Jump to content

create table not working


Vivid Lust

Recommended Posts

hey,

 

this mysql query comes back with no errors but keeps on creating the table "$seller" instead of the data assigned to the identifier. I need some help so the table created is the value of $seller!

 

Query:

<?php
mysql_query("CREATE TABLE '$seller'( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), rating VARCHAR(30), feedback VARCHAR(250)) "); 
?>

 

rest of script:

 

<?php
//get database info
require("db.php"); 

//set variables
$dbname   = 'ebay';


//get form data
$seller   = $_POST['seller'];
$select   = $_POST['select'];
$feedback = $_POST['feedback'];

//connect to database
mysql_select_db($dbname);

//checks if table exists (checks if seller exists also)
$check = mysql_query("SELECT * FROM $seller LIMIT 0,1");
if ($check){

//if the seller does exist do this...
echo "table exists";
}else{

//if the seller doesn't exist do this...
mysql_query("CREATE TABLE '$seller'( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), rating VARCHAR(30), feedback VARCHAR(250)) ");  

echo "Table Created!";

}
?>

 

Thanks for any help in advanced!

Link to comment
https://forums.phpfreaks.com/topic/121283-create-table-not-working/
Share on other sites

It sounds as though your $seller variable doesn't really contain the data you are expecting. You are assigning the data from $_POST['seller'], but are you positive that the post variable is being set as intended? Try echoing out your query strings before actually executing them. Once you are sure they reflect what you are after, you can execute them successfully.

I commented out some lines so the script looks like this:

 

<?php
//get database info
require("db.php"); 

//set variables
$dbname   = 'ebay';


//get form data
$seller   = $_POST['seller'];
$select   = $_POST['select'];
$feedback = $_POST['feedback'];

//connect to database
mysql_select_db($dbname);

//checks if table exists (checks if seller exists also)
//$check = mysql_query('SELECT * FROM tbl WHERE tbl=="$seller" LIMIT 0,1');
//if ($check){

//if the seller does exist do this...
//echo "table exists";
//}else{

//if the seller doesn't exist do this...
mysql_query('CREATE TABLE "$seller"( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), rating VARCHAR(30), feedback VARCHAR(250)) ');  

//echo "didnt exist, now created";
echo $seller; 
//}
?>
<b>works?</b>

 

Still the table doesnt get created... this is weird?

Now you have a single quote around your string which is wrong.

 

mysql_query("CREATE TABLE $seller ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), rating VARCHAR(30), feedback VARCHAR(250)) ");

 

 

Use mysql_error() after the query so you can see what error is being generated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.