Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old January 22nd, 2003, 10:21 PM
MuuTuwon MuuTuwon is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Bloomington, IN, USA
Posts: 25 MuuTuwon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to MuuTuwon Send a message via AIM to MuuTuwon
Quoting a query for use in mysql_query function.

This is a queston I'm sure has turned up before, I'm just not sure where else to look for help.

Basicly, I'm building a a form on the fly from database entries, generating parts of the query to insert from this form the same way.

When the while loop to make the table has finished and is submitted I have two variables. $columns containing the column names and $data contianing the names of the variables holding the data. They are already formated right to be in a query and I put the query together like so: "INSERT INTO table ($columns) VALUES ($data)"

I know I need a stripslashes somewhere, but I'm not sure where, this is my current code for the part in queston..

Code:
$prequery = "INSERT INTO db_main ($columns) VALUES ($data)";
$query = stripslashes($prequery);

mysql_query($query)
or die("Query Failed");


The problem is what I get in my database are the variable names as data rather then the data in those variables.

When I echo my $columns and $data variables before the query and before the stripslashes() they look like so:

$columns: Q1, Q2
$data: '$QA1', '$QA2'

I'm guessing I have my stripslashes in the wrong place, or I'm missing a step. But what exactly is wrong here?

Sorry if this is a little long-winded, I wanted to make sure I didn't leave anything out.

Thanks,

Reply With Quote
  #2  
Old January 23rd, 2003, 01:46 AM
filefrog filefrog is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: West Peoria, IL
Posts: 62 filefrog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to filefrog
RE: Quoting a query for use in mysql_query function.

have you tried modifying the string format of $data so that you don't need to strip slashes? Maybe I'm off the mark here, but this worked when I tested it (not with SQL, but it did combine with no parse errors. someone with a working SQL setup may want to check / verify)

php Code:
Original - php Code
  1.  
  2. $columns = "column1, column2";
  3. $data = '"$QA1","$QA2"';
  4. $query = "INSERT INTO db_main($columns) VALUES ($data)";
  5.  
  6. echo "$columnsn$datan$query";


gives me:
column1, column2
"$QA1","$QA2"
INSERT INTO db_main(column1, column2) VALUES ("$QA1","$QA2")

Unless there is some reason you can't use double quotes in inserting string data into your database, I don't see why this wouldn't work.

Reply With Quote
  #3  
Old January 23rd, 2003, 06:03 PM
MuuTuwon MuuTuwon is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Bloomington, IN, USA
Posts: 25 MuuTuwon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to MuuTuwon Send a message via AIM to MuuTuwon
Results.

I tried some of that and didn't have alot of luck but I'll point out the results I got.

I found that you can use double quotes to set off data in a mysql query but it escapes the $ and you insert just the variable name. I tried that with the code below:

php Code:
Original - php Code
  1.  
  2. $columns = 'Q1, Q2';
  3. $data = '"$QA1", "$QA2"';
  4. $query = "INSERT INTO table ($columns) VALUES ($data)";
  5. mysql_query($query);


Where $QA1 and $QA2 both had the value test, the database entries were the variable names. I tried this both as the code above and a version of it on the mySQL command line and got the same result both times.

I also found that in order to make that work I had to run a second loop after my page refreshed from the form to construct the $data and $columns variables. Before they were constructed in the same loop that displayed the questons and then sent with a input type=hidden, I found that $data with its double quotes wasn't passing on when the page refreshed.

I'm really not sure what else to try, I'm including below all of my code after the page refreshes with the form variables. When building the $data and $column variables in the code below, the part that is there is what I originally had, while the commented out entries are ones I tried using filefrog's suggestion. Any insight as to why this isn't working and what I should do to fix it is welcome, as I'm pretty stumped.

php Code:
Original - php Code
  1.  
  2. IF ($submit) {
  3.  
  4. $questonsallr = mysql_query("SELECT * FROM db_questons WHERE owner = 'all' OR owner = '$owner'");
  5.  
  6. $numrows = mysql_num_rows($questonsallr);
  7.  
  8. $columns = "";
  9. $data = "";
  10.  
  11. WHILE ($questonsall = mysql_fetch_array($questonsallr)) {
  12.  
  13. $numrows = $numrows - 1;
  14.  
  15. IF ($numrows == '0') {
  16.  
  17.   $columnname = "Q";
  18.   $columnname .= "$questonsall[id]";
  19.  
  20.   $columns .= "$columnname";
  21.  
  22.   $data .= "'$";
  23.   $data .= "QA$questonsall[id]'";
  24.  
  25.   // $data .= '"$QA';
  26.   // $data .= "$questonsall[id]";
  27.   // $data .= '"';
  28.  
  29. } ELSE {
  30.  
  31.   $columnname = "Q";
  32.   $columnname .= "$questonsall[id]";
  33.  
  34.   $columns .= "$columnname, ";
  35.  
  36.   $data .= "'$";
  37.   $data .= "QA$questosnall[id]', "
  38.  
  39.   // $data .= '"$QA';
  40.   // $data .= "$questonsall[id]";
  41.   // $data .= '", ';
  42.  
  43. }
  44. }
  45. ECHO $columns;
  46. ECHO "<BR><BR>";
  47. ECHO $data;
  48. ECHO "<BR><BR>";
  49.  
  50. $prequery = "INSERT INTO db_main ($columns) VALUES ($data)";
  51. $query = stripslashes($prequery);
  52.  
  53. ECHO $query;
  54.  
  55. mysql_query($query)
  56. or die("Query Failed");
  57.  
  58. }



Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > Quoting a query for use in mysql_query function.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway