473,574 Members | 18,327 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AJAX call to a PHP/MySQL script failes if a dynamic table name has a space in it.

21 New Member
Hello,

Below I have some Ajax and the page it points to. This code works great if $_SESSION['find'] has no spaces in it (for example, if it is "elpaso"). However, if $_SESSION['find'] has a space in it (for example, "el paso"), then the Ajax fails.

What can I do to make this Ajax work when $_SESSION['find'] has a space in it?

Thanks.

Ajax:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. ob_start();
  5. session_start();
  6. $find = strip_tags($_POST['find']);
  7. $find = trim ($find);
  8. $find = strtolower($find);
  9. $_SESSION['find'] = $find;
  10. ?>
  11.  
  12. $.ajax({
  13.             type: "POST",
  14.             data: "action=vote_up&id="+$(this).attr("id"),
  15.             url: "votes12.php",
  16.             success: function(msg)
  17.             {
  18.                 $("span#votes_count"+the_id).html(msg);
  19.                 //fadein the vote count
  20.                 $("span#votes_count"+the_id).fadeIn();
  21.                 //remove the spinner
  22.                 $("span#button"+the_id).remove();
  23.             }
  24.         });

Then, on votes12.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. ?>
  4.  
  5. <?php
  6. mysql_connect("mysqlv10", "username", "password") or die(mysql_error());
  7. mysql_select_db("database") or die(mysql_error());
  8.  
  9. function getAllVotes($id)
  10.     {
  11.     /**
  12.     Returns an array whose first element is votes_up and the second one is votes_down
  13.     **/
  14.     $votes = array();
  15.     $q = "SELECT * FROM {$_SESSION['find']} WHERE id = $id";
  16.     $r = mysql_query($q);
  17.     if(mysql_num_rows($r)==1)//id found in the table
  18.         {
  19.         $row = mysql_fetch_assoc($r);
  20.         $votes[0] = $row['votes_up'];
  21.         $votes[1] = $row['votes_down'];
  22.         }
  23.     return $votes;
  24.     }
  25. ?>
Jun 8 '09 #1
2 2843
ArizonaJohn
21 New Member
Someone else told me to put backticks around the session variable and it solved the problem, so

$q = "SELECT * FROM `{$_SESSION['find']}` WHERE id = $id";
Jun 8 '09 #2
Atli
5,058 Recognized Expert Expert
Yep, that would do it.

MySQL doesn't allow table/database/procedure names with spaces, or other special characters, unless you enclose them in back-ticks.

Anyways, glad you found a solution, and thanks for sharing it!

P.S.
I change the title of the thread to reflect the actual problem... might make it easier for somebody suffering the same problem to find an answer ;)
Jun 8 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3757
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I understand it I need a PHP script that pulls the query and dumps the data into XML format, that is called by triggering a javascript event that reads that...
31
3102
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked around here? If so, do you know if Ajax.NET can be used without prototype.js? -- "The most convoluted explanation that fits all of the made-up...
1
16488
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX - microlink pattern tutorial : A microlink is a link that opens up
3
2972
by: noballack | last post by:
I've got a problem, I'm working with Ajax in a web with a form with a list of checkbox added to the form via an Ajax.Updater method. These added checkboxs are not been sended by the form if I use Firefox or Safari. My source code is something like that: <script type="text/javascript"...
21
29758
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays prodcut anme have used ajax to avoid refreshing of page this works fine 2.now i have created one row with checkbox|select box|text|text|text|text|...
2
3143
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if i change something in php file using in ajax function.it not refreshed,means its shows the previous result it not get updated.i can't understand...
17
2928
by: Shalini Bhalla | last post by:
i have 2 tables bank master and branch details having bankcode as a common feild . i have designed a form in which i am filtering branches according to a particular bank code using ajax , i an fine till this ..... now i want that fields of these records should come in a separate field so that i can change my data there itselfe and...
2
2236
by: malcster2 | last post by:
hello, i am a beginner to ajax. i have created a mysql database, which i would like to access from a web page. i have created 3 files, a html to display the data, a php file to extract the data, and a javascript file to to the clever stuff. when i access the html page, all the data is displayed correcty, but if i add or delete any records,...
1
4591
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in Database but I'm stuck in the EDIT. I'm getting 2 problems over here. Below is the description: 1)The FIRST page will list all the records from the...
0
7738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8081
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6482
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3771
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2254
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1082
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.