I’m trying to insert a record into a db table. All fields go in except for the fields I get from functions. I have several
functions in one snippet.
I know this is probably not the best practice, but here it goes.
1.) I have a form that posts uning jquery/ajax.
2.) it posts to /index.php?id=124 (cached or uncached = same error)
a.)id 124 contains snippet [!CnbPhp!]
3.) There are two parts to [!CnbPhp!] depending on the post values $_POST[’search’] or $_POST[’insert’]
a.) searches for a record from the database & returns json (works fine)
b.) retrieves $_POST variables and inserts into DB returns json confirmation (partially working)
In step 3.b I process some of the $_POST variables with functions with global variables.
This works when developing on my local machine using regular php scripts, but as soon as I copy them into a snippet
modx (the only change is the DBAPI for db query, still using same insert fields and values) eveythign except the data
that should have been processed by a function gets inserted. No errors. Still returns json data with confirmation of inserting
record into db.
Any help or explanation woudl be appreciated.
//assume the post sends this
$_POST['product'] = 'DM-CFB1G139';
//define variables
//(tried it with or without definine these outside the function. Same result
$myPrice = '';
$myQty = '';
//define my function with global variables
function myOffers(){
global $myPrice, $myQty;
switch ($_POST['product']):
case 'DM-CFB1G139':
$myPrice = '39.99';
$myQty = '1';
break;
case 'DM-CFB1G139AS':
$myPrice = '39.99';
$myQty = '1';
break;
case 'DM-CFTRY29':
$myPrice = '0.00';
$myQty = '1';
break;
case 'DM-CFTRY29AS':
$myPrice = '0.00';
$myQty = '1';
break;
default:
$myPrice = '0.00';
$myQty = '1'; //Should be 0, but set to 1 just to test, no differnce
endswitch;
}
//call the function
myOffers();
//fields going into DB
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$product = $_POST['product'];
//fields for sql statment
$insertFields = 'FNAME,
LNAME,
PRODUCT,
PRICE,
QTY';
//insert values for sql statement
$insertValues ="'".$fname."',"; //First Name,
$insertValues .="'".$lname."',"; //LAst name,
$insertValues .="'".$product."',"; //Product,
$insertValues .="'".$myPrice."',"; //Price
$insertValues .="'".$myQty."'"; //Qty
//insert into db
$sql = "INSERT INTO modx_table1 (".$insertFields.") VALUES(".$insertValues.") ";
$rs = $modx->db->query($sql);
//Confirmation ID
if(!$rs){
$key = 'ERROR INSERTING RECORD.';
} else {
//now get the id
$key = $modx->db->getInsertId();
}
//just for reference my normal db call outside of modx.
//mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
//mysql_select_db("modx") or die(mysql_error());
// Insert a row of information into the table "example"
//$insert = mysql_query("INSERT INTO modx_table1
//(".$insertFields.") VALUES(".$insertValues.") ")
//or die(mysql_error());
//$key = mysql_insert_id();
Here is some code. Maybe there is soemthign blatently wrong with it (any sytax errors are jsut from me typing it in this post.)
Like I said it works perfectly fine outside of modx. I simply copy and paste the code into a snippet with different DB call