Skip to Content Skip to Menu

[SOLVED] Single quote is causing issues in CB Auto Action PHP code

  • llau
  • llau
  • OFFLINE
  • Posts: 38
  • Thanks: 1
  • Karma: 0
6 years 8 months ago - 6 years 7 months ago #303169 by llau
Hi there,

We're using an auto action on the "after user confirm" event, where the action uses the PHP eval code to build a SQL query using a cb field substitution
on a field from the registration form.

If this field contains a single quote it will cause the below code to fail.
Code:
global $_CB_framework; global $_CB_database; $query = "SELECT `id` FROM `#__database_table` WHERE `fieldname` = '[cb_myfieldname]' ORDER BY `id` DESC LIMIT 1";

I've also tried using addslashes as well as putting that field into a separate variable and then replacing the single quote with a backslash with no success.

Is there a way around this issue?

Kindly
V
Last edit: 6 years 7 months ago by krileon. Reason: Added [SOLVED] tag to subject

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48541
  • Thanks: 8290
  • Karma: 1445
6 years 8 months ago #303184 by krileon
Don't understand why you're using a Code action to do that. Use a Query action and just directly add your SQL statement to it. Substitutions in a query action are sent through escaping for SQL usage. If you're going to use a Code action you'll need to handle the SQL escaping your self using Quote function of $_CB_database (same as if you would be using Joomla API).


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • nant
  • nant
  • OFFLINE
  • Posts: 12339
  • Thanks: 1467
  • Karma: 877
6 years 8 months ago #303216 by nant
[Mod note] Move post from "potential bug" area to "usage" area - please note that support for paid add-ons should use the paid forums area.

Please Log in or Create an account to join the conversation.

  • llau
  • llau
  • OFFLINE
  • Posts: 38
  • Thanks: 1
  • Karma: 0
6 years 8 months ago - 6 years 8 months ago #303237 by llau
Thanks for you reply Krileon.

The reason I used the code action is because I needed to call a select query to first get an id value, then I needed to call a "custom" function from an "included" file using that value.

After reading your post, I tried escaping my CB value using the quote and getEscaped functions below, but it didn't work for some reason, maybe I don't have the right syntax?
Code:
global $_CB_framework; global $_CB_database; require_once( $_CB_framework->getCfg( 'absolute_path' ). '/includes/my_functions.php'); //$fieldname = $_CB_database->quote([cb_myfieldname]); // doesn't work for some reason... $fieldname = $_CB_database->getEscaped([cb_myfieldname]); $query = "SELECT `id` FROM `#__database_table` WHERE `fieldname` = $fieldname ORDER BY `id` DESC LIMIT 1"; // get CB user info $_CB_database->setQuery($query); $neededID = $_CB_database->loadResult(); $test = custom_function($neededID); // then function performs necessary tasks

I can't seem to find the right function to use...let me know if there's an easier way :)

Thanks again,
V
Last edit: 6 years 8 months ago by llau.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48541
  • Thanks: 8290
  • Karma: 1445
6 years 8 months ago #303259 by krileon
All substitutions should be treated as strings. This means you need to enclose them in quotes when being used in PHP.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You: llau

Please Log in or Create an account to join the conversation.

  • llau
  • llau
  • OFFLINE
  • Posts: 38
  • Thanks: 1
  • Karma: 0
6 years 7 months ago #303271 by llau
Thank you, Krileon, it's working now!

Kind regards,
V

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum