User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 374,021 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,736 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 5059 | Replies: 7
Reply
Join Date: May 2006
Posts: 8
Reputation: clarkeyboy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
clarkeyboy clarkeyboy is offline Offline
Newbie Poster

Help SQL problem - table names as variables

  #1  
May 19th, 2006
Hi,

I have a slight problem. I am trying to create a mail service for a new game which I have started creating recently. Each user that registers also creates a table for their mail. As the usernames are variable and the table name is the username, I need to insert messages, subjects and "froms" into this variable table.

I am using the following code:

$to = $_SESSION["to"];
$subject = $_SESSION["subject"];
$message = $_SESSION["message"];
$from = $_SESSION["username"];
echo "To: $to<br>";
echo "Subject: $subject<br>";
echo "Message: $message<br>";
echo "From: $from<br>";
include 'connection.php';
if ($to <> "") {
	if ($subject <> "") {
		if ($message <> "") {
			$rsusers = $conn->Execute("INSERT INTO $to (subject, message, from) VALUES('$subject', '$message', '$from')");
		}
	}
}

The problem is that it will not accept $to as the table name - it just comes up with some nasty error about the syntax of my insert statement. Can anyone help me on this, please?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: SQL problem - table names as variables

  #2  
May 19th, 2006
if you are using sql server, try running profiler to make sure that your php page is actually sending the script correctly
Reply With Quote  
Join Date: May 2006
Posts: 8
Reputation: clarkeyboy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
clarkeyboy clarkeyboy is offline Offline
Newbie Poster

Re: SQL problem - table names as variables

  #3  
May 19th, 2006
Originally Posted by campkev
if you are using sql server, try running profiler to make sure that your php page is actually sending the script correctly

I don't know what a profiler is - could you explain how to run it please? Also, I MAY have just figured out what it is that is wrong as it has happened on a couple of pages now. I am accessing several tables at the same time (or at least trying to). Is it possible to extract data or insert data into several tables at the same time or do you have to close one before opening another one?

RC
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: SQL problem - table names as variables

  #4  
May 19th, 2006
look under start>programs>microsoft sql server>profiler

yes you can extract data from more than one table at a time but I don't know of anyway to insert into more than one table at a time
Reply With Quote  
Join Date: May 2006
Posts: 8
Reputation: clarkeyboy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
clarkeyboy clarkeyboy is offline Offline
Newbie Poster

Re: SQL problem - table names as variables

  #5  
May 19th, 2006
I am not exactly trying to insert into more that one table at ONE time - I didn't exactly make that clear. I am using several SQL statements in a row to insert into several tables.
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: SQL problem - table names as variables

  #6  
May 19th, 2006
that should be fine. Did you get profiler to work?
Reply With Quote  
Join Date: May 2006
Posts: 8
Reputation: clarkeyboy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
clarkeyboy clarkeyboy is offline Offline
Newbie Poster

Re: SQL problem - table names as variables

  #7  
May 19th, 2006
No, I couldn't find it - I am using Dreamweaver for all my webpages and just use SQL code by reading out of a book or searching on the internet.

The error it comes up with is as follows:

Fatal error: Uncaught exception 'com_exception' with message 'Source: Microsoft OLE DB Provider for ODBC Drivers
Description: [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.' in D:\Websites\ClarkeyBoy\wwwroot\gametrialversion\sendmail.php:76 Stack trace: #0 {main} thrown in D:\Websites\ClarkeyBoy\wwwroot\gametrialversion\sendmail.php on line 76

I have been trying to find a solution all evening but have got nowhere.

I have sort of managed to fix it - but now, whenever I go to the view mail page I get the following error:

CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers.
Reply With Quote  
Join Date: May 2006
Posts: 8
Reputation: clarkeyboy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
clarkeyboy clarkeyboy is offline Offline
Newbie Poster

Re: SQL problem - table names as variables

  #8  
May 19th, 2006
I have now solved the CGI error but I am back to the original error now - thus not allowing me to insert records into any user mail tables.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 11:16 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC