duanpenpan5796 2014-06-07 11:51
浏览 27
已采纳

在链接到数据库行中的id的URL上使用$ _get

I'm trying to $_get part of URL in a href. What I want to happen is when you click the link you a redirected to that specific link. If that makes sense.

I have 2 functions:

First function, The list of links:

function showPosts() {
    $sql = ("SELECT * FROM blog");
    $query = mysql_query($sql);

    while ($row = mysql_fetch_array($query)) {
        $listId = $row["blog_id"];
        $showTitle = $row["title"];
        $showContent = $row["content"];
        $showTimestamp = $row["timestamp"];

        echo'
            <div>
            <a href="index.php?option=blog&task=view&id='$listId'"><h3>'.$showTitle.'</h3></a>
                <div>'.$showContent.'</div>
                <p>'.$showTimestamp.'</p>
            </div>
        ';
    }
}

Second function, redirect to link:

function viewPost() {

    if(empty($_GET['id']) ) {
        $listId = '';   
    } else {
        $listId = $_GET['id'];
    }

    $sql = ("SELECT title, content, timestamp FROM blog WHERE blog_id='$listId'");
    $query = mysql_query($sql);

    while ($row = mysql_fetch_array($query)) {
        $showTitle = $row["title"];
        $showContent = $row["content"];
        $showTimestamp = $row["timestamp"];

        echo'
            <div>
                <h2>'.$showTitle.'</h2>
                <p>'.$showTimestamp.'</p>
                <div>'.$showContent.'</div>
            </div>
        ';
    }
}

As you can see i'm using $_get['id'] here and I read that $_get can be used to retrieve passed url parameters.

The way i have set up the URL is defined by a set of variables in a switch.

if(empty($_GET['task']) ) {
    $task = 'show'; 
} else {
    $task = $_GET['task'];
}


switch ($task){
    case "create":
        createPost();       
        break;
    case "save":
        savePost();
        break;
    case "show":
        showPosts();
        break;
    case "view":
        viewPost();
        break;
    default:    echo'Something went wrong!';
}

Currently when I click a link, it redirects but all of the content related to that id is not there.

EDIT

I figured out what was wrong in the code. In the SQL statement I used dots(.) inside apostrophes(') to separate themselfes from the variable. When I removed the dots it worked fine.

blog_id='.$listId.'

Not sure why I even used the dots in the first place? :/

Also removed all the die's in my switch as per instruction from one answer.

  • 写回答

1条回答 默认 最新

  • douzhou7037 2014-06-07 12:04
    关注

    You are misusing the die() command in the switch. Die command is used to handle errors, it stops immediately the script execution and returns the error message set as argument. Try removing it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错