drwu24647 2014-05-07 12:30
浏览 50
已采纳

没有从数据库中选择正确的ID

I am trying to build a CMS system where an admin inserts data in a template. There can be as many pages created with that template as the admin wants. The pages should then be accessed in a menu with links and be displayed with the respective data according the link you just clicked.

Everything is working as expected except the system is not picking up the data from the id that the link had. Instead the system is picking up the data from the last id on the database.

Example: If I click on the link Page#2, I will have the URL http://www.website/page.php?pid=2 but the content that is loaded is the content from the last id on the database and not the 2.

Code to make and display the links:

if (!$_GET['pid']) {
    $pageid = '1';
    } else {
        $pageid = preg_replace('/[^0-9]/', "", $_GET['pid']); // filter everything but numbers for security
    }

    $sqlCommand = "SELECT id, linklabel FROM pages WHERE showing='1' ORDER BY id ASC"; 
    $query = mysqli_query($myConnection, $sqlCommand) or die('Error: ' . mysqli_error($myConnection)); 

    $listadeavatars = '';
    while ($row = mysqli_fetch_array($query)) { 
        $pid = $row["id"];
        $avatar = html_entity_decode($row["linklabel"]);
    } 

    $sqlCommand = "SELECT id, linklabel FROM pages WHERE showing='1' ORDER BY id ASC"; 
    $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 

    $menuDisplay = '';
    while ($row = mysqli_fetch_array($query)) { 
        $pid = $row["id"];
        $linklabel = $row["linklabel"];
        $avatar = html_entity_decode($row["linklabel"]);

        $menuDisplay .= '<li><a href="page.php?pid=' . $pid . '">' . $avatar . '</a></li>';

    } 
    mysqli_free_result($query);

Code to display the data on page.php according to the ID

if (!$_GET['pid']) {
    $pageid = '1';
} else {
    $pageid = ereg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}

$sqlCommand = "SELECT pagetitle, linklabel, evenemang, presentation, producent, pagebody, mapa FROM pages WHERE id='$pid' LIMIT 1";  
    $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
    while ($row = mysqli_fetch_array($query)) { 
        $pagetitle = $row["pagetitle"];
        $linklabel = $row["linklabel"];
        $evenemang = $row["evenemang"];
        $presentation = $row["presentation"];
        $producent = $row["producent"];
        $pagebody = $row["pagebody"];
        $mapa = $row["mapa"];
        $avatar = html_entity_decode($row["linklabel"]);
    } 
    mysqli_free_result($query);

Please do let me know if I need to explain better! Any suggestions to solve this issue? Cheers!

  • 写回答

1条回答 默认 最新

  • doutou7286 2014-05-07 12:43
    关注

    On page.php, change WHERE id='$pid'

    $sqlCommand = "SELECT pagetitle, linklabel, evenemang, presentation, producent, pagebody, mapa FROM pages WHERE id='$pid' LIMIT 1"; 
    

    to WHERE id='$pageid'

    $sqlCommand = "SELECT pagetitle, linklabel, evenemang, presentation, producent, pagebody, mapa FROM pages WHERE id='$pageid' LIMIT 1"; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面