uramagget Posted June 4, 2007 Share Posted June 4, 2007 I'm making a Content Management System, so I want the system to check if a page (?id=pagename here) exists or not. If it doesn't, return a 404 error. If not, serve the page. Also, I would like some help in stopping people from entering blank URLs (?id=_blank_stuff_here). Here is what I'm using for the querystring: <? /* Script Name: query.inc.php Date: June 03, 2007 Description: ?id= URLs. Eww..... Version: 0.1 Change Log: Version 0.1 (Original Version): */ //Use GET to find query $id=mysql_real_escape_string($_GET['id']); //Use mySQL to select the query $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> Link to comment https://forums.phpfreaks.com/topic/54153-checking-if-a-mysql-row-exists-or-not/ Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 <? /* Script Name: query.inc.php Date: June 03, 2007 Description: ?id= URLs. Eww..... Version: 0.1 Change Log: Version 0.1 (Original Version): */ //Use GET to find query if (isset($_GET['id']) && !empty(trim($_GET['id'])) { $id=mysql_real_escape_string($_GET['id']); //Use mySQL to select the query $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); if (mysql_num_rows($result) > 0) $rows=mysql_fetch_array($result); else show_404(); }else { show_404(); } function show_404() { header("404 PAGE NOT FOUND"); include('404.html'); die(); } ?> Something like that? Link to comment https://forums.phpfreaks.com/topic/54153-checking-if-a-mysql-row-exists-or-not/#findComment-267716 Share on other sites More sharing options...
uramagget Posted June 4, 2007 Author Share Posted June 4, 2007 That doesn't work.... It gives me a blank page.... Link to comment https://forums.phpfreaks.com/topic/54153-checking-if-a-mysql-row-exists-or-not/#findComment-267754 Share on other sites More sharing options...
uramagget Posted June 4, 2007 Author Share Posted June 4, 2007 Fatal error: Can't use function return value in write context in /home/rindiny/public_html/beta/query.inc.php on line 13 Link to comment https://forums.phpfreaks.com/topic/54153-checking-if-a-mysql-row-exists-or-not/#findComment-267762 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 [code] <?php /* Script Name: query.inc.php Date: June 03, 2007 Description: ?id= URLs. Eww..... Version: 0.1 Change Log: Version 0.1 (Original Version): */ //Use GET to find query $_GET['id'] = (isset($_GET['id'']))?trim($_GET['id']):''; if (!empty($_GET['id'])) { $id=mysql_real_escape_string($_GET['id']); //Use mySQL to select the query $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); if (mysql_num_rows($result) > 0) $rows=mysql_fetch_array($result); else show_404(); }else { show_404(); } function show_404() { header("404 PAGE NOT FOUND"); include('404.html'); die(); } ?> [/code] I know it is the first if statement and is probably due to the empty www.php.net/empty try the above if the same error report it back. Link to comment https://forums.phpfreaks.com/topic/54153-checking-if-a-mysql-row-exists-or-not/#findComment-267789 Share on other sites More sharing options...
uramagget Posted June 4, 2007 Author Share Posted June 4, 2007 Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ']' in /home/rindiny/public_html/beta/query.inc.php on line Link to comment https://forums.phpfreaks.com/topic/54153-checking-if-a-mysql-row-exists-or-not/#findComment-267792 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 <?php /* Script Name: query.inc.php Date: June 03, 2007 Description: ?id= URLs. Eww..... Version: 0.1 Change Log: Version 0.1 (Original Version): */ //Use GET to find query $_GET['id'] = (isset($_GET['id']))?trim($_GET['id']):''; if (!empty($_GET['id'])) { $id=mysql_real_escape_string($_GET['id']); //Use mySQL to select the query $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); if (mysql_num_rows($result) > 0) $rows=mysql_fetch_array($result); else show_404(); }else { show_404(); } function show_404() { header("404 PAGE NOT FOUND"); include('404.html'); die(); } ?> Link to comment https://forums.phpfreaks.com/topic/54153-checking-if-a-mysql-row-exists-or-not/#findComment-267875 Share on other sites More sharing options...
uramagget Posted June 4, 2007 Author Share Posted June 4, 2007 Alright, this works, but seems that everything results in a 404 now. :/; Link to comment https://forums.phpfreaks.com/topic/54153-checking-if-a-mysql-row-exists-or-not/#findComment-267884 Share on other sites More sharing options...
uramagget Posted June 4, 2007 Author Share Posted June 4, 2007 MY BAD, IT WORKS! THANKS! Link to comment https://forums.phpfreaks.com/topic/54153-checking-if-a-mysql-row-exists-or-not/#findComment-267891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.