PHP SQL create table with variable name

This is a discussion on "PHP SQL create table with variable name" within the PHP Forum section. This forum, and the thread "PHP SQL create table with variable name are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jun 4th, 2007, 09:19
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 154
Thanks: 0
Thanked 0 Times in 0 Posts
PHP SQL create table with variable name

Hi,

I have two questions, firstly can I create a table with the name of a variable using php?? This is what I'm trying:
PHP: Select all

$query="CREATE TABLE $quote_num 
(
ID int(7) NOT NULL auto_increment,
itemid varchar(10) NOT NULL,
itemdesc varchar(50) NOT NULL,
)"

EDIT----------------
PHP: Select all

mysql_query("CREATE TABLE '".$quote_num."' 
   (
   ID int(7) NOT NULL auto_increment,
   itemid varchar(10) NOT NULL,
   itemdesc varchar(50) NOT NULL,
   )"
); 
This doesn't work either... and neither of them give an error..

/EDIT------------------

Also, are there any security issues in the following scenario:

A company produces quote and saves to database. Sends an email to client with a link and query string containing quote number to client. Client clicks link and webpage retrieves quote from database. Client checks "accept quote" and submits.

Thanks,

Nathan.

Last edited by karinne; Jun 12th, 2007 at 12:26. Reason: Please use [php]...[/php] tags when displaying PHP code!
Reply With Quote

  #2 (permalink)  
Old Jun 4th, 2007, 10:12
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Blake121
Re: PHP SQL create table with variable name

This should work... You have to use real escape string to make the data safe before using it with a database.

The mysql_error() part will give you a detailed error if something goes wrong. (Remove the mysql_error() part from a live website.);

Code: Select all
$quote_num = mysql_real_escape_string($quote_num);
 
$sql="CREATE TABLE $quote_num
(
ID int(7) NOT NULL auto_increment,
itemid varchar(10) NOT NULL,
itemdesc varchar(50) NOT NULL,
)";
 
$query = mysql_query($sql) or die (mysql_error());
Reply With Quote
  #3 (permalink)  
Old Jun 4th, 2007, 12:51
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP SQL create table with variable name

I can't see a syntax mistake in your original version but AFAIK you have to set a primary key on your auto-increment column. This is a mysql problem.
Reply With Quote
  #4 (permalink)  
Old Jun 12th, 2007, 12:25
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 154
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP SQL create table with variable name

Hmm, thanks for your help. I couldn't get this to work so I had to do it another way, thanks anyway.
Reply With Quote
Reply

Tags
table, sql, php, create

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
variable classes CloudedVision PHP Forum 4 Feb 14th, 2008 03:07
is this variable right? geyids PHP Forum 4 Aug 6th, 2007 21:45
Am I using table-cell and table-row too much? idl Web Page Design 15 Sep 7th, 2006 12:55
Need to align width of Float table with the table below Vertabase Web Page Design 4 Mar 8th, 2006 21:21
..copy data from column A in Table A to column B in Table B? gecastill Databases 10 Jun 23rd, 2005 18:27


All times are GMT. The time now is 21:05.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43