0

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();
        die();          
        break;
    case "save":
        savePost();
        die();
        break;
    case "show":
        showPosts();
        die();
        break;
    case "view":
        viewPost();
        die();
        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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Joakim Hedman
  • 15
  • 1
  • 5

1 Answers1

0

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.

Dimitris Kalaitzis
  • 1,426
  • 1
  • 11
  • 16