SunQuest
           PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old May 8th, 2008, 04:18 PM
adammc's Avatar
adammc adammc is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Cairns, Australia
Posts: 528 adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 19 h 42 m 22 sec
Reputation Power: 5
Question Help with query

Hi guys,

I have a table in my database that store my stores products. I want to perfrm a check that the product id actually exists before continuing on with the script. If not I want to redirect the user.

I tried the following code but all I get is a blank page:

PHP Code:
// Get details from config file 
 
include("../CMS/config.php");  

 
// include database file  
 
include("../CMS/script-files/db-connect.php");  
 

// connect to the database
        
$cid mysql_connect($host,$username,$dbpass);
       if (!
$cid) { echo("ERROR: " mysql_error() . "\n");    }
        
mysql_select_db("$db");

      
      
$query mysql_query("SELECT * FROM Products WHERE product_id=$_GET[toy]");
    while(
$r mysql_fetch_assoc($query)) 
    {      
    

// make sure the product id exists - if not redirect to main page    
    
if (!isset($r['product_id']))
  {
      
header("Location: ../index.php"); 
      exit(); 
  } 
__________________
Aussie Webmaster Forums | | Webmaster Tools

Reply With Quote
  #2  
Old May 8th, 2008, 04:20 PM
GameYin GameYin is offline
Banned
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2008
Location: Whiteford, MD
Posts: 534 GameYin User rank is First Lieutenant (10000 - 20000 Reputation Level)GameYin User rank is First Lieutenant (10000 - 20000 Reputation Level)GameYin User rank is First Lieutenant (10000 - 20000 Reputation Level)GameYin User rank is First Lieutenant (10000 - 20000 Reputation Level)GameYin User rank is First Lieutenant (10000 - 20000 Reputation Level)GameYin User rank is First Lieutenant (10000 - 20000 Reputation Level)GameYin User rank is First Lieutenant (10000 - 20000 Reputation Level)GameYin User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 4 Days 23 h 22 m 54 sec
Warnings Level: 5
Reputation Power: 0
Send a message via AIM to GameYin
MySpace
Remove echo(" and just put ". I think you can't have ( there. Also, turn error reporting on.

Reply With Quote
  #3  
Old May 8th, 2008, 04:22 PM
JavaNinja's Avatar
JavaNinja JavaNinja is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: in the ether
Posts: 491 JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 6 Days 8 h 18 m 6 sec
Reputation Power: 31
You should get a blank page. Because you need to echo something out saying product exists or not. There is no print/echo in the code in the while loop!

Reply With Quote
  #4  
Old May 8th, 2008, 04:24 PM
JavaNinja's Avatar
JavaNinja JavaNinja is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: in the ether
Posts: 491 JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 6 Days 8 h 18 m 6 sec
Reputation Power: 31
Quote:
Originally Posted by GameYin
Remove echo(" and just put ". I think you can't have ( there.

You can have parenthesis in echo.

Reply With Quote
  #5  
Old May 8th, 2008, 04:31 PM
adammc's Avatar
adammc adammc is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Cairns, Australia
Posts: 528 adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 19 h 42 m 22 sec
Reputation Power: 5
"Remove echo(" and just put ". I think you can't have ( there. Also, turn error reporting on."

?? But I dont have 'echo' I have:
header("Location: ../index.php");
exit();

Reply With Quote
  #6  
Old May 8th, 2008, 04:32 PM
adammc's Avatar
adammc adammc is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Cairns, Australia
Posts: 528 adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 19 h 42 m 22 sec
Reputation Power: 5
I added this but still get blank page?

ini_set ('display_errors', 1); // Show errors, if any
error_reporting (E_ALL & ~E_NOTICE);

Reply With Quote
  #7  
Old May 8th, 2008, 04:34 PM
JavaNinja's Avatar
JavaNinja JavaNinja is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: in the ether
Posts: 491 JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 6 Days 8 h 18 m 6 sec
Reputation Power: 31
Listen, is the page being redirected to index.php? If not and you get a blank page then thats what should happen since there is no echo in your code except the error reporting one.

Reply With Quote
  #8  
Old May 8th, 2008, 04:42 PM
adammc's Avatar
adammc adammc is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Cairns, Australia
Posts: 528 adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 19 h 42 m 22 sec
Reputation Power: 5
No its not being redirected... It should be as I ma entering a product id that doesnt exist.

I even tried this and still got a blank page:

// make sure the product id exists - if not redirect to main page
if (!isset($r['product_id']))
{
echo"assasasas";
}

Reply With Quote
  #9  
Old May 8th, 2008, 04:46 PM
JavaNinja's Avatar
JavaNinja JavaNinja is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: in the ether
Posts: 491 JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 6 Days 8 h 18 m 6 sec
Reputation Power: 31
So if you are being redirected correctly, then there must be something wrong with the page (index.php) you are redirecting to.

Reply With Quote
  #10  
Old May 8th, 2008, 04:47 PM
JavaNinja's Avatar
JavaNinja JavaNinja is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: in the ether
Posts: 491 JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 6 Days 8 h 18 m 6 sec
Reputation Power: 31
Quote:
Originally Posted by adammc
if (!isset($r['product_id']))
{
echo"assasasas";
}

EDIT: I realised you said its not redirecting.

It means the if statement doesn't evaluate to true. So maybe your query doesn't execute correctly. When test product ids echo out the $_GET and see if it contains your id.

Last edited by JavaNinja : May 8th, 2008 at 04:55 PM.

Reply With Quote
  #11  
Old May 8th, 2008, 05:02 PM
adammc's Avatar
adammc adammc is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Cairns, Australia
Posts: 528 adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 19 h 42 m 22 sec
Reputation Power: 5
the get variable does work as when i remove the check to see if it exists and there is a valid id in the url my product info page displays correctly.

Reply With Quote
  #12  
Old May 8th, 2008, 05:25 PM
Veamon's Avatar
Veamon Veamon is offline
Blowing up trees with my mind.
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2005
Location: Neverland Ranch, minus the touching.
Posts: 2,712 Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 127259 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127259 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127259 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127259 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127259 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127259 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Weeks 3 Days 23 h 43 m 24 sec
Reputation Power: 878
You can't use the Location, because data has already been sent by the php script. Use this instead:

echo '<meta http-equiv="refresh" content="1;URL=yourpage.php" />';
__________________

Raid1 in XP Pro

My open source projects:
------------------------
Blobber - Add images as blobs to SQL Server
------------------------

Reply With Quote
  #13  
Old May 8th, 2008, 05:29 PM
JavaNinja's Avatar
JavaNinja JavaNinja is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: in the ether
Posts: 491 JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level)JavaNinja User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 6 Days 8 h 18 m 6 sec
Reputation Power: 31
Quote:
Originally Posted by adammc
the get variable does work as when i remove the check to see if it exists and there is a valid id in the url my product info page displays correctly.


Try this:

PHP Code:
if (mysql_num_rows($query) == 0

Reply With Quote
  #14  
Old May 8th, 2008, 05:31 PM
adammc's Avatar
adammc adammc is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Cairns, Australia
Posts: 528 adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level)adammc User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 19 h 42 m 22 sec
Reputation Power: 5
Still getting a blank page when an invalid id is used:

// make sure the product id exists - if not redirect to main page
if (!isset($r['product_id']))
{
echo '<meta http-equiv="refresh" content="1;URL=index.php" />';
}

Reply With Quote