-2

if a query what exist http://localhost/page.php?pid=first-page is open me, it is work, but if i write http://localhost/page.php?pid=second-page and second-page doesnt exist it still open a page and give this error because doesnt find...

I understand why give me this error, but i dont know how to relocation the url if a some write a wrong query

session_start();
require "conx.php";
// Determine which page ID to use in our query below ---------------------------------------------------------------------------------------

if(isset($_GET['pid'])){
    $pageid = preg_replace('[^a-z0-9_]', '', $_GET['pid']);
    }   
    // $tag is now santized and ready for database queries here
// Query the body section for the proper page
$stmt = $con->prepare('SELECT pagebody FROM pages WHERE linklabel = ?');
$stmt->bind_param('s', $pageid);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_array()) {
    // do something with $row
    $body = $row["pagebody"];   
}

so i how i can relocation the url address if a the query doesnt exist??

Stefan J.
  • 93
  • 9
  • 1
    *Notice: Undefined variable: titlu* - ok, so you obviously are trying to use a variable somewhere that has yet been defined. – Funk Forty Niner Jul 28 '16 at 15:28
  • If You detect that the page doesn't exist, You may relocate to another page using HTTP's `Location` header. – Roman Hocke Jul 28 '16 at 15:30
  • use a conditional `isset()` and/or `empty()`. Your question's short on relevant code though and looks to be pagination-related. – Funk Forty Niner Jul 28 '16 at 15:32
  • @Fred-ii- i said if you paid attention.... i understand why give me a error and i didnot ask for how to fix that, i asked for other this... how to Relocation if a query doesnt exist, and i figured up how to do that. anyway thanks for you tried to help me – Stefan J. Jul 28 '16 at 15:52

1 Answers1

1

If your goal is to redirect (relocation the URL address) the user when the result is empty (the query doesn't exist), please try this:

//If the query return empty result redirect the user to 404 page !
if(!$result->num_rows) {
    header('Location: /404.php'); //Change it to your page
    exit;
}

I hope this will help.

Ismail RBOUH
  • 10,292
  • 2
  • 24
  • 36
  • I doubt that's what the question/issue is about. However, let's have the OP decide. – Funk Forty Niner Jul 28 '16 at 15:33
  • 1
    @Fred-ii- It's the main question or the only question :D **so i how i can relocation the url address if a the query doesnt exist??** – Ismail RBOUH Jul 28 '16 at 15:35
  • TBH, I have no idea at this point as the question is too unclear for my taste. An undefined variable that hasn't been posted, will more than likely get closed based on it. – Funk Forty Niner Jul 28 '16 at 15:36
  • Why should the OP "try this"? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Jul 28 '16 at 15:38
  • Yes, the error is on line **82** . However, the code above is way less than that @Fred-ii- ! – Ismail RBOUH Jul 28 '16 at 15:38
  • 1
    @JayBlanchard the main question is **i how i can relocation the url address if a the query doesnt exist??** and the code is commented. Here, a **good answer** would be the PHP Manual !!! – Ismail RBOUH Jul 28 '16 at 15:40
  • @Fred-ii- i said i know why was gave me a error, other was the problem and Ismail help me, the code works fine, thank you man – Stefan J. Jul 28 '16 at 15:44
  • 1
    @JayBlanchard Please remove the duplicated mark and read the question carefully ! – Ismail RBOUH Jul 28 '16 at 15:53
  • I removed the duplicate mark! Please stop shouting as it is unnecessary! P.S. I read the question carefully the first time and until the OP made clarification the duplicate was correct. – Jay Blanchard Jul 28 '16 at 16:05