简体   繁体   中英

Page for each row with PHP and MySQL

I'm making a movie database that's accessible on my local network. I've set up a MySQL database and I have PHP to access the table and to output the results onto a webpage.

I want to have a unique page for each movie/row in the database. I want the webpage to have each value for the movie (title, year, genre, plot, rating)

I've established a connection:

<?php

$username="root"; $password="12345"; $database="videos";

$con1 = mysqli_init();
mysqli_options($con1, MYSQLI_OPT_LOCAL_INFILE, true);
$con = mysqli_real_connect($con1, "localhost", $username, $password, $database);

?>

I have tried the answer from this question: page for each row in mysql

But it does not work. I already have a page to show all the rows in the table, but I'd like a unique page for each row in the table.

Here is the PHP for the table on a single page:

<?php

$username="root"; $password="12345"; $database="videos";

$con = mysqli_init();
mysqli_options($con, MYSQLI_OPT_LOCAL_INFILE, true);
mysqli_real_connect($con, "localhost", $username, $password, $database);

mysqli_select_db($database);

mysqli_query($con, "TRUNCATE TABLE films");
if (!mysqli_query($con, "LOAD DATA LOCAL INFILE '/home/ryan/movies.txt' INTO TABLE films")) {
    printf("Errormessage: %s\n", mysqli_error($con));
}

$result = mysqli_query($con, "SELECT title, yr, genre, plot, rating FROM films") or die("Unable to query.");

echo "<table border='1'>";
echo "<tr> <th>Title</th> <th>Year</th> <th>Genre</th> <th>Plot</th> <th>Rating</th> </tr>";

// keep getting the next row until no rows left
while($row = mysqli_fetch_array( $result )) {
  // print out contents of each row into a table
  echo "<tr> <td>";
  echo $row['title'];
  echo "</td><td>";
  echo $row['yr'];
  echo "</td><td>";
  echo $row['genre'];
  echo "</td><td>";
  echo $row['plot'];
  echo "</td><td>";
  echo $row['rating'];
  echo "</td></tr>";
}

echo "</table>";
?>

Are you familiar with get method? You can use the same layout of a page for each of the movies you have for example create a page where you want to see every movie you have in the database and when you click in one movie it will redirect to the layout page like so:

<a href='movieLayout.html?movieID=<?php echo movieId; ?>' id='LOTR1' />

with a get method "?movieID=123"specifying what movie you clicked and on top of the layout movie page:

<?php
  if(isset($_GET['ID movie you want to see or name of the movie']){
        send a query to database .."where id_movie =1 " OR "where name_movie LIKE 'LOTR';
  then you receive the path to the file where your movie is and the title of the movie, year, plot, etc.. 
  }
?>
<html>....</html>

this is all theoretically i can't be more specific

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM