Jump to content

redirect if id is empty


frank_solo
Go to solution Solved by requinix,

Recommended Posts

Well I've done this so far but this helps only if index.php?id=(blank)

if (!isset($_GET['id']) || empty($_GET['id'])) {
    header('location: http://www.mydomain.co') ;
}

What I would like as well is to query the table and check if the id exist if not to redirect. 

Link to comment
Share on other sites

This worked

<?php
session_start();
include 'config.php';
$url_id = mysql_real_escape_string($_GET['id']);
$sql = "SELECT id FROM places WHERE id='$url_id' AND unit='Residential' AND county='New Jersey'";
$result = mysql_query($sql);
if(mysql_num_rows($result) >0){
   //found
   $query="UPDATE `places` SET `views` = `views` + 1 WHERE `id` = $url_id";

}else{
   //not found
   header("location: http://www.mydomain.co");
}
?>

But now this won't work
$query="UPDATE `units` SET `views` = `views` + 1 WHERE `id` = $url_id";

Can someone show me how to make that statement work or should I start a different thread?

Thanks

Link to comment
Share on other sites

Got it:
 

<?php
include "config.php";
$id = mysql_real_escape_string((int)$_GET['id']);
$sql = "SELECT id FROM places WHERE id='$id' AND unit='Residential' AND county='New Jersey'";
$result = mysql_query($sql);
if(mysql_num_rows($result) >0){
   //found
   
}else{
   //not found
   header("location: http://www.mydomian.co");
}
$query = "UPDATE `units` SET `views` = `views` + 1 WHERE `id` = $id";
$res = mysql_query($query, $bd);
$query = "SELECT * FROM `places` WHERE `id` = $id";
$res = mysql_query($query, $bd);
$result = mysql_fetch_assoc($res);
$lat = $result['lat'];
$contact = $result['contact'];
$phone = $result['phone']; 
$long = $result['lng'];
?>
Link to comment
Share on other sites

  • Solution

header() does not immediately end your script - it will continue running. Use an exit; or die; after calling it.

//not found
header("location: http://www.mydomian.co");
exit;
Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.