473,769 Members | 5,834 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 2856
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
3772
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 file with XMLhttprequest.
31
3161
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 facts is the most likely to be believed by conspiracy theorists. Fitting the
1
16512
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
2991
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" src="http://www.my_web_page.com//js/prototype-1.4.0.js"></script> <script> function showMoreOptions( url, pars, div_id ){ if...
21
29820
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| where in the select box values are fetched from table here also i have used ajax for getting the m_name...
2
3173
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 whats the prob.this is the code i m using: <? include("config.inc.php"); //error_reporting(0); ...
17
2944
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 using ajax can update database on onblur event ...... how to do this ? this is my code for...
2
2256
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, and press the button i have created to refresh the data(without reloading), the data doesn't change....
1
4610
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 table which Admin can EDIT with CHECKBOX for each record to select. He can select one or more than one...
0
9413
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10192
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10027
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9848
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7391
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3941
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
3
2810
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.