php - Using $_get on URL that is linked to id in database row -
i'm trying $_get part of url in href. want happen when click link redirected specific link. if makes sense.
i have 2 functions:
first function, list of links:
function showposts() { $sql = ("select * 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 link:
function viewpost() { if(empty($_get['id']) ) { $listid = ''; } else { $listid = $_get['id']; } $sql = ("select title, content, timestamp blog 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 can see i'm using $_get['id'] here , read $_get can used retrieve passed url parameters.
the way have set url defined set of variables in 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 click link, redirects of content related id not there.
edit
i figured out wrong in code. in sql statement used dots(.) inside apostrophes(') separate themselfes variable. when removed dots worked fine.
blog_id='.$listid.'
not sure why used dots in first place? :/
also removed die's in switch per instruction 1 answer.
you misusing die() command in switch. die command used handle errors, stops script execution , returns error message set argument. try removing it.
Comments
Post a Comment