Trouble with sample code for a searchable database

Hi so I’ve been scouring the web for awhile for a decent tutorial on how to use Google’s Query Language and finally found one that doesn’t dump you into the deep end and hope you can swim. Unfortunately the sample php file throws an error so it isn’t even workable enough for me to figure out what is going on and learn how to tweak and build what I need from there. I posted the sample code below for you all to take a look at. On the off chance it makes a difference end game wise I’m trying to use a google spreadsheet to create a searchable database, in this case the info you get off of business cards.

[php]





<?php $search= $_REQUEST['search']; if ($search > ''){ $search = $search;} else { $search = '';} ?>

[/php]

I found Google documentation pretty helpful and in my opinion doesn’t dump you off in the deep end of the pool. I would suggest keeping most of your PHP at the top end of the page. I wrote a simple search using the world database that you can find at php.net (another good source for help). You HTML between your tags are incorrect take a look at mine to get a better idea how to do it.

[php]<?php
include ‘lib/includes/connect/connect.php’;

$db_options = [
/* important! use actual prepared statements (default: emulate prepared statements) /
PDO::ATTR_EMULATE_PREPARES => false
/
throw exceptions on errors (default: stay silent) /
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
/
fetch associative arrays (default: mixed arrays) */
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
];
if (filter_input(INPUT_POST, submit)) {

$pdo = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=world;charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);

$query = 'SELECT Name, CountryCode, District, Population FROM city WHERE CountryCode=:CountryCode ORDER BY District'; // Set the Search Query:

$stmt = $pdo->prepare($query); // Prepare the query:

$result = $stmt->execute([':CountryCode' => filter_input(INPUT_POST, countryCode)]); // Execute the query with the supplied user's parameter(s):

}
?>

Database Search
search
> <?php if ($result) { while ($record = $stmt->fetch()) { echo ""; echo '"; echo '"; echo '"; echo '"; echo ""; } } ?>
City Country Code District / State Population
' . $record->Name . "' . $record->CountryCode . "' . $record->District . "' . $record->Population . "
</body>
[/php]

It uses PHP 7 but with a few minor version it would run on lower versions. I would also look over Google’s documentation once again and maybe looking over my code to get a GENERAL idea on how to make work? Maybe someone else here will have a better idea?

$_SERVER[‘PHP_SELF’] is vulnerable to an XSS Attack.

Sponsor our Newsletter | Privacy Policy | Terms of Service