Hi guys,

I'm pretty much self taught, and I'm working on a site that stores mysql data using php.

In the site I previously did, I used separate pages with a query relating to what I wanted to show, eg. a page called Manchester.php that displayed all records with an area of Manchester.

This meant I had 300 separate pages though, and yes, I know it's a messy way of doing it!!

Can someone point me towards a tutorial where I can learn how to code my query so it only uses a single page, eg.

index.php?query=manchester or index.php?id=213 etc

I'm not asking for anyone to do this for me, as I'll never learn otherwise, but if someone can tell me the correct term for what I want to do and where I can read more then that would be very ace of you all

Thanks :)

Dani AI

Generated

As describes, the solution is to stop creating static pages and use a single dynamic page that reads parameters and runs the appropriate query. That pattern is usually called a front controller or routing using query strings (e.g., index.php?id=213) or path-based routing (clean URLs). As hinted, a form or links can drive the parameter, but the important parts are input validation, safe queries, and friendly routing.

A minimal, safe approach:

  • Use GET parameters for bookmarkable URLs (or route the path to index.php with mod_rewrite for pretty URLs).
  • Validate and sanitize inputs with filter_input (or strict whitelist checks for known area names).
  • Use PDO or MySQLi prepared statements to avoid SQL injection.
  • Escape all output with htmlspecialchars to avoid XSS.

Example pattern (conceptual):

<?php
$pdo = new PDO(..., [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

$id   = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
$area = filter_input(INPUT_GET, 'area', FILTER_SANITIZE_STRING);

if ($id) {
  $stmt = $pdo->prepare('SELECT * FROM items WHERE id = :id');
  $stmt->execute([':id' => $id]);
} elseif ($area) {
  $stmt = $pdo->prepare('SELECT * FROM items WHERE area = :area');
  $stmt->execute([':area' => $area]);
} else {
  $stmt = $pdo->query('SELECT * FROM items LIMIT 50');
}

foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
  echo htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8');
}

If you want cleaner URLs, route all requests to index.php with a simple .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Troubleshooting tips: validate inputs and use whitelists for non-numeric fields; add DB indexes on columns used in WHERE; paginate large result sets; show a friendly 404 or message when no records are found; log DB errors but never display raw errors to users. Avoid the old mysql_* API (it is removed in later PHP versions) and prefer PDO or MySQLi prepared statements.

Recommended Answers

All 5 Replies

Are you using ajax?

no, just basic php and mysql

How are you directing your users to each page?
i.e. a select box, or table of links ?

yeah I was using a drop down which was populated from another table in the mysql db

http://www.tizag.com/phpT/forms.php

These should see you right. Once you're comfortable with this method of using forms and redirecting learn Ajax.

I left it too long before I read about Ajax and it would have made my life so much easier.

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.