Have an account? Sign in
Login  Register  Facebook
If ID does not exist or...display this error
Hello there,
I'm playing around (again :P ) with my article script that i'm creating but i'm blocked somewhere. I have the display page that checks if and ID is queried, if yes, displays the article, if not, displays all the article page with the last articles added. But, what's happens if the ID queried does not exist? It mess up the page and does not display any error. What i need to do is an if statement to check first of all if the id exists and then display it, and if the id does not exist, to appear a "Article does not exist" message on the page.
Also, in the article db i have a table that stores if the article should be active or not for the visitors. In the mysql statement i have "Where Active=Yes" and the visitors can see only the active articles, but, when a visitor changes the id on the URL manually and puts there an article id that is inactive, the article is shown :( . How to prevent this? I need to do something than, when a visitor tried to access an article that is inactive (Where Active=NO), to appear a message on the page that tells the user that the id is looking for is inactive, or has no access to show it.
Can you PLEASE help me on this complicated think for me? Please!

Thank you in advance
Started: September 19, 2011 Latest Activity: September 19, 2011 mysql php errorhandling custom error
3 Answers
DesignShop, this is the code:
<?php
session_start();
include("functions/functions.php");
include('functions/dbconn.php');
include('koka.php');
 
                if(isset ($_GET['lajmeid'])) {

  
    $lajmeid = intval($_GET['lajmeid']); //make sure its an integer
   // $sql="SELECT B.katemri, A.id AS prodid, A.prodimg AS prodimg, A.prodinfo AS prodinfo, A.prodemri AS prodemri FROM p_produkte A LEFT JOIN p_kategori B ON A.katid = B.id WHERE B.id = ".$catId;
	$sql = "SELECT B.LKatEmri, A.LajmeID AS LajmeID, A.LajmeEmri AS LajmeEmri, A.LajmeLink AS LajmeLink, A.LajmeIntro AS LajmeIntro, A.LajmeTxt AS LajmeTxt, A.LajmeTag AS LajmeTag, A.LajmeImg AS LajmeImg, A.LajmeVideo AS LajmeVideo, A.LajmeAktiv AS LajmeAktiv, A.KatLajmeID AS KatLajmeID FROM lajme A LEFT JOIN lajme_kategori B ON A.KatLajmeID = B.LKatID WHERE B.LKatID = KatLajmeID AND A.LajmeID='$lajmeid' AND A.LajmeAktiv='PO'";
	

    $result=mysql_query($sql);
	    if(mysql_num_rows($result) > 0) //products found
    {
        while($row = mysql_fetch_assoc($result)) //Loop through the procusts and display them
        {
     ?>
<body>
<div class="trupi">

<?php include('koka1.php');
include('koka2.php');
 ?>
	
	<div class="indexqender">
		<div class="trupimajtas">
			<div class="lajmehot">Lajmi fundit - HOT</div>
            <div class="lajmetqender">
                <div id="kokakuqe"> <p>Informohu! Eshte e drejta jote...</p></div>
                <div class="menupkokakuqe"><h1><?php echo $row['LKatEmri']; ?> - <?php echo $row['LajmeEmri']; ?></h1></div>
                
                
              
     <div class="artikullqender">
     	<div class="artikulllajmi">
			           
             <h3> <?php echo $row['LajmeIntro']; ?></h3>
           
             <div class="teksti"> <img src="imazhet/artikuj/<?php echo $row['LajmeImg']; ?>" width="250" height="200" border="0" vspace="3" hspace="5" align="right" alt="<?php echo $row['LajmeEmri']; ?>" title="<?php echo $row['LajmeEmri']; ?>" />  <?php echo $row['LajmeTxt']; ?> </div>
             <div class="video"> <?php echo $row['LajmeVideo']; ?> </div>
             
            
              
             <div class="tag">Fjalët Kyçe:  <?php echo $row['LajmeTag']; ?></div>
             
             
            <?php
					 }
				  }
				  
			  }
                else{  // Nese nuk eshte marre asnje id lajmi shfaqet si me poshte    ?>
                    
                    <body>
<div class="trupi">

<?php include('koka1.php'); ?>
<?php include('koka2.php'); ?>
    
    <div class="indexqender">
        <div class="trupimajtas">
            <div class="lajmehot">Lajmi fundit - HOT</div>
            <div class="lajmetqender">
                <div id="kokakuqe"> <p>Informohu! Eshte e drejta jote...</p></div>
                <div class="menupkokakuqe">Artikujt e fundit</div>
                
                
              
     <div class="artikullqender">
         <div class="artikulllajmi">
             
             <?php 
             echo lajmetfundit ();
                } //Mbyllet else
             ?>
         </div>  
         
         <div class="artikulllajmidjathtas">
            <div class="menu">
                <ul>
                    <li><a href="#">List Item 1</a></li>
                    <li><a href="#">List Item 1</a></li>
                    <li><a href="#">List Item 1</a></li>
                    <li><a href="#">List Item 1</a></li>
                    <li><a href="#">List Item 1</a></li>  
                    
                </ul>

            </div>
         </div>
	</div>      
                <div class="pastro"></div>
                <div class="bllokposhtelajme"> Bllok poshte lajmeve</div>
                
            </div>
		</div>
		
		<div class="trupidjathtas">
			<div class="bllokd">
            	<div class="bllokdtitulli">Parashikimi Motit </div>
                <div class="bllokdcontent">
                	<div class="bllokdcontenttxt">
                        <img src="http://l.yimg.com/a/i/us/we/52/<?=$currentImage;?>.gif" align="right" />
                        Temperatura: <?=$currentTemperature;?>&deg;C <br />
                        Me e larta: <?=$currentHigh;?>&deg;C<br />
                        Me e ulta: <?=$currentLow;?>&deg;C<br />
                        Lindja Djellit: <?=$currentSunrise;?><br />
                        Perendimi Djellit: <?=$currentSunset;?><br />
                        
                    </div><div class="pastro"></div>
                </div>
            </div>
            
            
            <div class="bllokd" style="margin-top:5px">
            	<div class="bllokdtitulli">Kembimi Valutor </div>
                <div class="bllokdcontent">
                	<div class="bllokdcontenttxt"><center><p>
                       <img src="imazhet/ikona/ikona_europe.jpg" width="30" align="left" hspace="3" /><b>Euro - </b> <?php echo kv_euro (); ?> Lek</p>
                       <p><img src="imazhet/ikona/ikona_usa.gif" width="30" align="left" hspace="3" /><b>USD - </b> <?php echo kv_dollare (); ?> Lek</p>
                       <p><img src="imazhet/ikona/ikona_angli.gif" width="30" align="left" hspace="3" /><b>GBP - </b> <?php echo kv_gbp (); ?> Lek </p>
                        </center>
                    </div><div class="pastro"></div>
                </div>
            </div>
            <?php
			echo reklama_2 (); // Shfaq reklama te rastesishm ne te djathte.
			include('includet/rss.php');
			?>
            
		</div>
	</div>
	</div>
<?php
include('fundi.php');
?>

Thank you!

Posted: Kleidi
In: September 19, 2011

Any answer that can help me on this please? I have tried all method you suggest and i had no success. Help me please?

Posted: Kleidi
In: September 19, 2011

@jooria way is the best; just make it adapt with your script
September 19, 2011

I'm sure about it but, since i'm new on php and still learning, i don't know how to implement it :( Can you help me on this please? I have posted my code in a new reply. Thank you in advance ;)
September 19, 2011

Hello @Kleidi,

first

makeing the Active filed int will be more good with 0 and 1 | 0 mean inactive and 1 mean active and you will use in like that:
if ($row[\'active\']) {
    //when 1 do some thing	
}else{
    //when 0 do some thing else	
}

second

showing only active and exist articles very very easy just follow:

first method

merge the two case in one error:
   //get id of the article
   $id = intval($_GET[\'id\']);
   
   $query = mysql_query(\"SELECT * FROM `table` where `id` = $id and `Active` = \'Yes\'\");
        $row = mysql_fetch_array($query, MYSQL_ASSOC );    
    
    //if the article not exist or the article inactive show this error
    if (!$row) {
        exit(\'display any error\');
    }
// here the page

second method

   $id = intval($_GET[\'id\']);
   
   $query = mysql_query(\"SELECT * FROM `table` where `id` = $id\");
        $row = mysql_fetch_array($query, MYSQL_ASSOC );    
    
    //if the article not exist show this error
    if (!$row) {
        exit(\'display any error\');
    }else{
        if ($row[\'Active\'] != \'Yes\') {
                //if the article not active show this error
                exit(\'display inactive error\');
        	} else {
            //here your page
        }        
    }

Posted: MacOS
In: September 19, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer