-1

我仍在学习有关 PHP 的更多有趣细节。示例:从 MySQL 迁移到 MySQLi。我目前正在做的是尝试输入这样的内容:http ://music.daum.net/artist/main?artist_id=2289

从我通过切分网址从分页中学到的知识:

  • 主要的?
  • 艺术家 ID=
  • 2289

我怎样才能制作这样的页面?我有 2 个部分可用,并会在弄清楚这一点时制作其他部分。

  • 艺术家信息(可作为testhub-artist.php 获得
  • 专辑(可作为testhub-artistalbum.php 获得
  • 音乐视频
  • 照片部分

我希望在制作页面时更容易,而不是为每个人制作单独的文件夹。

我的网址是:“../artist/detail?artist_id=#”


这是艺术家页面的顶部。

<?php
//Connect to ...
include "testhub-artist.php";
include "testhub-artistalbum.php";
?>

testhub-artist.php

<?php
//Connect to database
include "mysqli_connect.php";

// Construct our join query
$sql = "SELECT * FROM individuals WHERE soloID = 1";

// Create results
$result = mysqli_query($link, $sql);

// Checking if query is successful
if($result){

// Print out the contents of each row into a table 
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){

// If else states on each variable
    if ($profilepic = $row['profilepic']){
        $profilepic = $row['profilepic'];
    }else{
        $profilepic = "DamjuNoImage";
    }
    if ($engname = $row['engname']){
        $engname = $row['engname'];
    }else{
        $engname = "Unknown";
    }
    if ($korname = $row['korname']){
        $korname = $row['korname'];
    }else{
        $korname = "Unknown";
    }
    if ($engbn = $row['engbn']){
        $engbn = $row['engbn'];
    }else{
        $engbn = "Unknown";
    }
    if ($korbn = $row['korbn']){
        $korbn = $row['korbn'];
    }else{
        $korbn = "Unknown";
    }
    if ($dateofbirth = $row['dateofbirth']){
        $dateofbirth = $row['dateofbirth'];
    }else{
        $dateofbirth = "Unknown";
    }
    if ($occupation = $row['occupation']){
        $occupation = $row['occupation'];
    }else{
        $occupation = "Unknown";
    }
    if ($debut = $row['debut']){
        $debut = $row['debut'];
    }else{
        $debut = "Unknown";
    }
    if ($recordlabel = $row['recordlabel']){
        $recordlabel = $row['recordlabel'];
    }else{
        $recordlabel = "Unknown";
    }
    if ($officialsite = $row['officialsite']){
        $officialsite = $row['officialsite'];
    }else{
        $officialsite = "#";
    }
    if ($sitename = $row['sitename']){
        $sitename = $row['sitename'];
    }else{
        $sitename = "Unknown";
    }
} // End of while statement
}else{
    $engname = "Unknown";
    $korname = "Unknown";
    $engbn = "Unknown";
    $korbn = "Unknown";
    $dateofbirth = "Unknown";
    $occupation = "Unknown";
    $debut = "Unknown";
    $recordlabel = "Unknown";
    $officialsite = "#";
    $sitename = "Unknown";
} // End of If statement

// Free result set
//mysqli_free_result($result);

?>

testhub-artistalbum.php

<?php
//connect to db
include "mysqli_connect.php";

//check for a page number. If not, set it to page 1
if (!(isset($_GET['albumpage']))){
    $albumpage = 1;
}else{
    $albumpage = $_GET['albumpage'];
}

//query for record count to setup pagination
$sqli = "SELECT * FROM albums WHERE soloID = 3";
$album_data = mysqli_query($link, $sqli);
$album_rows = mysqli_num_rows($album_data); 

//number of photos per page
$album_pagerows = 4; 

//get the last page number
$last_album = ceil($album_rows/$album_pagerows); 

//make sure the page number isn't below one, or more than last page num
if ($albumpage < 1){
    $albumpage = 1;
}elseif ($albumpage > $last_album){
    $albumpage = $last_album;
}

//Set the range to display in query
$max_album = 'limit ' .($albumpage - 1) * $album_pagerows .',' .$album_pagerows;

//get all of the photos
$albumList = "";
$sqli2 = "SELECT * FROM albums WHERE soloID = 3 ORDER BY releasedate DESC $max_album";
$album_sql = mysqli_query($link, $sqli2);

//check for photos
$albumCount = mysqli_num_rows($album_sql);

if ($albumCount > 0){
    while($album_rows = mysqli_fetch_array($album_sql)){
    $albumID = $album_rows["albumID"];
    $albumpic = $album_rows["albumpic"];
    $title = $album_rows["albumTitle"];
    $releasedate = $album_rows["releasedate"];
    $page = $album_rows["page"];
    $albumList .= '
      <li class="albumthumb">
         <a href="' . $page . '" title="' . $title . '"><img class="profile" src="../albums/album_th/' . $albumpic . '.jpg" alt="' . $albumpic . '" width="120" height="120" border="0" /><p class="datatitle">' . $title . '</p></a><p class="data-releasedate">' . $releasedate . '</p>
       </li>
                  ';
    }
}else{
    $albumList = "There are no available albums at this time!";
}

//mysql_close();
?>

抱歉没有解释清楚。我希望在制作像 url 这样的个人资料页面时能够使用分页。我想使用url中的数字来更改sql代码中的id(soloID)。

节省时间的好主意,对吧?MySQLi 每次我看到它都会变得更容易。

谢谢你。


已更改 2012 年 5 月 31 日下午 5:44 CT

$artist = $_GET['artist_id']

进入

    if(is_numeric($_GET['artist_id'])){
    $artist = $_GET['artist_id'];
}else{
    $artist = 1;
}
4

2 回答 2

2
artist/detail?artist_id=#

您将detail用作页面,(可能有一个带有索引的详细文件夹)并在详细页面上,在$_GET[]某处有一个变量来获取艺术家 ID。所以你的代码可能看起来像这样:

$artist = $_GET['artist_id']; // Filter this variable

$sql = "SELECT * FROM individuals WHERE soloID = '{$artist}'";

/**
 * Verify if the ID exists
 * Display query results, etc.
 */

因此,每次更改 URL 中的 artist_id 变量时,页面都应相应更改。

于 2012-05-31T17:07:31.683 回答
1

欢迎使用我第二喜欢的语言!我爱php。有人已经回答了你的问题,但我有一些建议。

您拥有的代码并不容易受到攻击,因为用户提供的数据是通过数学传递的……但是内联变量是让自己容易受到SQL 注入攻击的好方法。查看bind_param()和准备好的语句并养成使用它们的习惯。总是。好吧几乎总是..

不幸的是,SQL 不允许您绑定您使用的值之类的东西,LIMIT,ORDER BY,GROUP BY因此您必须自己处理这些内容。永远不要相信来自用户的任何东西,所以做这项工作并检查它。

排序列应始终是列名。检查它们。

if ( ! in_array($sort_column,array('column1','column2','column3') ) ) $sort_column = 'column1';

限制应始终为整数。像这样投射它们。

$safe_limit = (int) $user_limit;

无需将数组值复制到另一个变量中。只需直接使用它们。

您需要将您的值转义到 html 中。查找urlencode()htmlentities()

我的 IE 有足够的内存,所以我必须稍后再完成。

于 2012-05-31T18:07:00.133 回答