944,080 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 11063
  • MS SQL RSS
May 19th, 2006
0

SQL problem - table names as variables

Expand Post »
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:

MS SQL Syntax (Toggle Plain Text)
  1. $to = $_SESSION["to"];
  2. $subject = $_SESSION["subject"];
  3. $message = $_SESSION["message"];
  4. $from = $_SESSION["username"];
  5. echo "To: $to<br>";
  6. echo "Subject: $subject<br>";
  7. echo "Message: $message<br>";
  8. echo "From: $from<br>";
  9. include 'connection.php';
  10. IF ($to <> "") {
  11. IF ($subject <> "") {
  12. IF ($message <> "") {
  13. $rsusers = $conn->Execute("INSERT INTO $to (subject, message, from) VALUES('$subject', '$message', '$from')");
  14. }
  15. }
  16. }

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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
clarkeyboy is offline Offline
8 posts
since May 2006
May 19th, 2006
0

Re: SQL problem - table names as variables

if you are using sql server, try running profiler to make sure that your php page is actually sending the script correctly
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
May 19th, 2006
0

Re: SQL problem - table names as variables

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
clarkeyboy is offline Offline
8 posts
since May 2006
May 19th, 2006
0

Re: SQL problem - table names as variables

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
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
May 19th, 2006
0

Re: SQL problem - table names as variables

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
clarkeyboy is offline Offline
8 posts
since May 2006
May 19th, 2006
0

Re: SQL problem - table names as variables

that should be fine. Did you get profiler to work?
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
May 19th, 2006
0

Re: SQL problem - table names as variables

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
clarkeyboy is offline Offline
8 posts
since May 2006
May 19th, 2006
0

Re: SQL problem - table names as variables

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
clarkeyboy is offline Offline
8 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: finding a matches recursively
Next Thread in MS SQL Forum Timeline: chicken and egg problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC