1

I'm building a simple bug tracking tool. After you logged in you can create a new project, when you've created a new project you get redirected to the project page.

My question is, how can I give every project a unique page? I know I only have to create 1 page (projectpage.php) and then create a unique page for each project. I have to get the id from each project.

After you fill in a form to create a new project, that form will post to this page:

project.class.php

$name        = $_POST['name'];
$descr       = $_POST['description'];   
$leader      = $_POST['leader'];    
$email       = $_POST['email'];

$sql="INSERT INTO projects (name, description, leader, email, registration_date)
VALUES ('$name', '$descr', '$leader', '$email', NOW())";
$result = mysql_query($sql); 

if($result){
header('Location: ../projectpage.php');
}
else {
echo "Oops, there is something wrong. Try again later.";
}

mysql_close();

This will store the data in the MySQL database.

So, how can I make a unique page for each project? What's the best way to do this?

Thanks in advance!

  • 1
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 02 '13 at 14:55
  • Don't think of it in terms of a Unique page for each project. That would scale horribly. In fact, you're populating the same page with different data, and that's the beauty of server side programming. – christopher Aug 02 '13 at 14:56

2 Answers2

2

You have one PHP file, take a project id through the query string (or some other part of the URL) and query the database for the data associated with that id. Then you generate the HTML for the project page using that data.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

First, mysql_* functions are obsolete, you should use PDO...

You should have a PHP script that gets a project from the database by its ID. You probably should have an ID column in your table. Here is a sample code :

if($result){
    header('Location: ../project.php?id='.mysql_insert_id());
}

mysql_insert_id returns the last inserted id. See the documentation. Then, project.php :

<?php
if (!isset($_GET['id']) || empty($_GET['id']) {
   echo "error : bad url";
   exit();
}

$cn = new PDO(...); // Check PDO documentation for this

$sql = "SELECT * FROM projects WHERE project_id = :id";
$stmt = $cn->prepare($sql);

$stmt->bindParam(":id", $_GET['id']);
$stmt->execute();

$project = $stmt->fetch(PDO::FETCH_OBJ);

// $project will behave like an Object, you can display for example the project name
echo $project->name;

// or the project date...
echo $project->registration_date;
Mohamed Amine
  • 2,264
  • 1
  • 23
  • 36