I have a very basic web site that I use on a local WAMP server, it’s a magazine index I’m trying to compile for a club I’m a member of. For the specific problem I’m having, I have a html form where I enter some car details, when I move away from the text box where I enter the registration number, I call a very short php function that searches the database for that registration number, and returns the details or the string “Not Found”. If the details are found, the rest of the forum is populated.
The problem I am having is that sometimes I enter a registration number that I know is in the database, but it returns “Not Found” and therefore the form is left empty. If I call the lookup php code directly with the registration as the argument in the url, it returns the correct information. For the sake of completeness here is the lookup code:
<?php
require_once("../magissues_db.php");
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<response>';
$reg = strtoupper(StripReg($_GET['reg'])); // added toupper() although stripreg should already do it.
$q = "select * from vehicles where vehicle_reg = :r";
$b = $dbc->prepare($q);
$b->bindParam(':r', $reg);
$b->execute();
if ($row = $b->fetch()) {
echo $row['vehicle_type'];
}
else
{
echo 'Not Found';
}
echo '</response>';
?>
// This is Stripreg, it's in the include file usually
function StripReg($inreg) {
$newreg = "";
$a = 0;
while ($a < strlen($inreg)) {
$x = strtoupper(substr($inreg, $a, 1));
if (($x >='A' && $x <= 'Z') || ($x >= '0' && $x<= '9')) {
$newreg .= $x;
}
$a += 1;
}
return $newreg;
}
The function just removes anything that isn’t valid in the registration, and converts to upper case. The same function is used to store the registration as well.
But sometimes it returns “Not Found” when it should return the vehicle details. Could it be some kind of buffering issue? I noticed today that I created a new vehicle entry, realised I’d made a typo, opened phpmyadmin on another IE tab, made the change, stored the data, but when my code above ran, it still returned the old information even though phpmyadmin had stored the correction.
I added some alert() functions into the javascript to show the registration as it arrives, and the response from the above code, and it is receiving “Not Found”, not just getting no response.
Any ideas would be appreciated.