So begins part two of my eCommerce site build. Here’s part one.

2:08pm
At this point, my AJAX calls are passing data correctly, but my PHP script is failing when I’m trying to insert a new row into the Cart table. So I’m having the script log the MySQL statement for testing using this log function I extended from PHP.net’s example.

[php]
/*
* function writes writes a given string to an error file
* $text – string, text to be written to file
* $filename – string, without extension
* $rewrite – boolean, whether to clear the file
*/
function writeFile( $text, $filename, $rewrite ){
$file = $filename . ‘.txt’;
// Open the file to get existing content
$current = file_get_contents($file);
// Append the string to the file
if($rewrite == true){ //empty the file
$current = $text;
}
else{ //append text
$current .= $text;
}
$current .= "\n ————— \n";
// Write the contents back to the file
file_put_contents($file, $current);
}
</code>

When called, I have it log the statement, and lo and behold, I have the answer to my question.
<code>
INSERT INTO cart(‘name’, ‘description’, ‘quantity’, ‘cost’, ‘id’) VALUES(‘Floating Platform Bed’,’This handmade platform bed is both sturdy and beautiful. It’s guaranteed to last for years to come. ‘,’1′,’500′,’1’)
—————
[/php]
The description string is being terminated early by my “It’s” having an apostrophe. I’ll have to work some htmlspecialchars() magic on it.

Leave a Reply

Your email address will not be published. Required fields are marked *