I am using prepared queries and binding variables to placeholders. This is done during inserting of data. This works well and I normally do not have problems.
However, on our registration form, someone put a value of St. John's into a form field. The data was successfully written and it seems alright from my phpMyAdmin console. However, any attempt to get data from that record do not work. The execution doesn't seem to fail as I have a try/catch around the execute and the catch would email me. If I remove the apostrophe then I can retrieve the record as normal.
I've searched around and all I see are people saying to use prepared statements. I am using them. They also say that addslashes()
is a bad thing (and I don't use that). I've also tried using ':hospital' => $db_pdo->quote($_POST['hospital'])
but the results are that the inserted fields have extra apostrophes which I do not want. Perhaps there is some sort of unquote method?
Also, it may be useful to know that I am fetching the entire row as an assoc_array and then json_encoding it so that I can send it back to be interpreted as JavaScript where each part of the array would be written to the appropriate field on a form.
So, I've decided to ask a question myself.
Example code: Insertion: ':fname' => $_POST['fname'], ENT_QUOTES ,
Retrieval: $db_pdo->prepare($statement_SQL);
$db_pdo->bind(':preregkey', $_POST['id']);
$retval_execute = $db_pdo->execute();
if($retval_execute){$retval_execute='true';}
else{$retval_execute = 'false';}
<br>";
$result = $db_pdo->statement->fetch(PDO::FETCH_ASSOC);
$output = json_encode($result);
echo "<script type='text/javascript'>";
echo " var formdata = '" . $output . "';";
echo "console.log('value: ', " . $_POST['id'] . ")" ;
echo "</script>";
I use $.post to get the data and I think I'm having a problem with characters actually returned. I get illegal character or unexpected identifier. It seems that everything in 'data' as a result is dumped and lost.
// Gather the information for one pre-reg record, put the data into the form, switch to the basic tab.
function displayrecord_prereg(id) {
// We were passed the id for the record, go get it!
$.post('admin-board_p.php', { o: 'displayrecord_prereg', id: id}, function(data) {
// Results have to go somewhere...
console.log('data: ', data);
$('#utilitydiv').html(data);
// Make the JSON_ENCODE array actually usable in Javascript.
var formdata2 = JSON.parse(formdata);
// Distribute the array of fields into their correct form field.
// Must do it within this part of the function.
$("#studentphoto").prop("value", formdata2['studentphoto']);
$("#grade").val(formdata2['grade']);