kyleldi Posted August 2, 2007 Share Posted August 2, 2007 Hi Everyone, i'm getting this odd error when I try to launch my code in a browser. "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1" The code is supposed to populate a list of employees in that given department called by the URL. (e.g. department.php?dept=Engineering would pull all of the engineering employees). As far as I can see the code is written properly? Can anyone else see a problem? <?php require_once('connect.php'); ?> <?php if (isset($_GET['dept'])) { //Page first accessed $dept = $_GET['Department_Title']; } else { if (isset($_POST['dept'])) { //form has been submit $dept = $_POST['Department_Title']; } } if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_staff, $staff); $query_rs_name = "SELECT* FROM staffdirectory WHERE Department_Title = $dept"; $rs_name = mysql_query($query_rs_name, $staff) or die(mysql_error()); $row_rs_name = mysql_fetch_assoc($rs_name); $totalRows_rs_name = mysql_num_rows($rs_name); Quote Link to comment Share on other sites More sharing options...
DeadEvil Posted August 2, 2007 Share Posted August 2, 2007 $query_rs_name = "SELECT * FROM staffdirectory WHERE Department_Title = $dept"; Quote Link to comment Share on other sites More sharing options...
kyleldi Posted August 2, 2007 Author Share Posted August 2, 2007 For some reason it still returns the error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1" :-\ Quote Link to comment Share on other sites More sharing options...
DeadEvil Posted August 2, 2007 Share Posted August 2, 2007 $query_rs_name = "SELECT * FROM staffdirectory WHERE Department_Title = '$dept' "; or $query_rs_name = "SELECT * FROM staffdirectory WHERE Department_Title = '".$dept."' "; Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 2, 2007 Share Posted August 2, 2007 Change this line: $rs_name = mysql_query($query_rs_name, $staff) or die(mysql_error()); to this: $rs_name = mysql_query($query_rs_name, $staff) or die('Error with query:<br>' . $query_rs_name . '<br>' . mysql_error()); The error should now show you the generated query from your code. Is the query correct? Quote Link to comment Share on other sites More sharing options...
kyleldi Posted August 2, 2007 Author Share Posted August 2, 2007 It seems to have taken care of the syntax errors, but I guess something else must be wrong, for its not displaying any results. Any further ideas? Here's the whole page code. <?php require_once('connect.php'); ?> <?php if (isset($_GET['dept'])) { //Page first accessed $dept = $_GET['Department_Title']; } else { if (isset($_POST['dept'])) { //form has been submit $dept = $_POST['Department_Title']; } } if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_staff, $staff); $query_rs_name = "SELECT * FROM staffdirectory WHERE Department_Title = '".$dept."' "; $rs_name = mysql_query($query_rs_name, $staff) or die('Error with query:<br>' . $query_rs_name . '<br>' . mysql_error()); $row_rs_name = mysql_fetch_assoc($rs_name); $totalRows_rs_name = mysql_num_rows($rs_name); ?>?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Test</title> <link href="style.css" rel="stylesheet" type="text/css"> <script language=javascript src=activateSF123.js></script> <script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script> <style type="text/css"> <!-- .staffleft { height: 233px; width: 350px; border: 2px groove #FFFFFF; float: left; margin-top: 20px; margin-right: 2px; margin-bottom: 15px; margin-left: 25px; text-align: center; white-space: normal; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: x-small; font-style: normal; line-height: normal; font-weight: normal; font-variant: normal; text-transform: capitalize; text-decoration: none; color: #FFFFFF; padding: 3px; } .newblock { margin-top: 45px; margin-right: 15px; margin-bottom: 15px; margin-left: 15px; padding-top: 0px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; text-align: center; vertical-align: bottom; } .stafftable { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: x-small; color: #FFFFFF; margin: 0px; padding: 2px; } .staffright { height: 233px; width: 550px; margin-top: 20px; margin-right: 105px; margin-bottom: 15px; margin-left: 105px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: x-small; color: #FFFFFF; padding-top: 3px; padding-right: 3px; padding-bottom: 8px; padding-left: 8px; } .staffcenter { padding: 3px; width: 600px; margin-top: 15px; margin-right: auto; margin-bottom: 15px; margin-left: auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: large; font-style: normal; line-height: normal; font-weight: bold; font-variant: normal; color: #FFFFFF; text-align: center; white-space: normal; text-transform: capitalize; } .stafftableheading { color: #FFFFFF; font-weight: bold; } --> </style> </head> <body> <table height="77%" border="0" align="left" cellpadding="0" cellspacing="0" style="height:100% "> <tr> <td width="766" height="495" valign="top" style="height:100% "> <table width="776" border="0" cellpadding="0" cellspacing="0" style="height:100% "> <tr> <td valign="top" width="1180" height="274" style=""> <div align="center" id="nav"> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="766" height="227"> <param name="movie" value="flash/main.swf"> <param name="quality" value="high"> <param name="menu" value="false"> <param name="wmode" value="transparent"> <!--[if !IE]> <--> <object data="flash/main.swf" width="766" height="227" type="application/x-shockwave-flash"> <param name="quality" value="high"> <param name="menu" value="false"> <param name="wmode" value="transparent"> <param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer"> FAIL (the browser should render some flash content, not this). </object> <!--> <![endif]--> </object> <script type="text/javascript" src="fixit.js"></script> <ul> <li><a href="laser-custom-tube-cutting.html">Tube Laser Cutting</a></li> <li><a href="flat-metal-laser-cutting.html">Flat Laser Cutting</a></li> <li><a href="cnc-turning.html">CNC Turning</a></li> <li><a href="metalfabrication.html">Metal Fabrication</a></li> <li><a href="multimedia.html">Video Library</a></li> <li><a href="about-us.html">About Us</a></li> <li><a href="our-clients.html">Clients</a></li> <li><a href="careers.html">Careers</a></li> <li><a href="http://www.laser-dynamics.com/forum/" target="_blank">Forum </a></li> <li><a href="contact-us.html">Contact Us</a></li> <li><a href="quote.html">Request A Quote</a></li> </ul> </div> </td> </tr> <tr> <td valign="top" width="1180" style="" class="content"> <div class="staffcenter"> Staff Directory</div> <div class="staffright"> <div align="center"> <h3>Please Choose An Employee<br> </h3> </div> <div class="results" id="results"> <div align="center"> <table border="0"> <?php do { ?> <tr> <td><?php echo "<a href=\"viewstaff.php?id=".$row_rs_name['id']."\">".$row_rs_name['Name']; ?></td> </tr> <?php } while ($row_rs_name = mysql_fetch_assoc($rs_name)); ?> </table> </div> </div> </div> <div class="newblock"> <div align="center"></div> </div> </td> </tr> <tr> <td valign="top" width="1180" height="34" style="background-image:url(images/bottom.gif) " class="footer"><div style="margin:9 0 0 51px "><b><a href="terms.html">Terms of Use</a> |<a href="privacy.html"> Privacy Statement</a><a href="privacy.html"></a></b><img alt="" src="images/spacer.gif" width="314px" height="1px"> © 2007 Test. All rights reserved</div></td> </tr> </table> </td> </tr> </table> </body> </html> <?php mysql_free_result($rs_name); ?> Quote Link to comment Share on other sites More sharing options...
DeadEvil Posted August 2, 2007 Share Posted August 2, 2007 if (isset($_GET['dept'])) { //Page first accessed $dept = $_GET['Department_Title']; } if (isset($_POST['dept'])) { //form has been submit $dept = $_POST['Department_Title']; } Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted August 2, 2007 Share Posted August 2, 2007 You have two php closing tags, (?>) I don't know if that does anything, but you should fix that. You can also put the require_once() inside the same php tags as the rest of the code, instead of opening tags, including, closing tags, opening tags, starting code, closing tags...etc. finally, you didn't seem to end your link toward the end: <?php do { ?> <tr> <td><?php echo "<a href=\"viewstaff.php?id=".$row_rs_name['id']."\">".$row_rs_name['Name']; ?></td> </tr> <?php } while ($row_rs_name = mysql_fetch_assoc($rs_name)); ?> Should be: <?php do { ?> <tr> <td><?php echo "<a href=\"viewstaff.php?id=".$row_rs_name['id']."\">".$row_rs_name['Name']."</a>"; ?></td> </tr> <?php } while ($row_rs_name = mysql_fetch_assoc($rs_name)); ?> That's all I can see. Quote Link to comment Share on other sites More sharing options...
kyleldi Posted August 2, 2007 Author Share Posted August 2, 2007 Hm... I've made both code changes and neither of which changed anything in the results.. It's odd that it doesn't give any error at all. Any other ideas? Quote Link to comment Share on other sites More sharing options...
kyleldi Posted August 2, 2007 Author Share Posted August 2, 2007 With this code data is displayed, but it will not filter accordingly, and the GET array seems to be written correctly. Any ideas? Here's the code: <?php require_once('Connections/staff.php'); ?> <?php if (isset($_GET['dept'])) { //Page first accessed $dept = $_GET['Department_Title']; } if (isset($_POST['dept'])) { //form has been submit $dept = $_POST['Department_Title']; } if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_staff, $staff); $query_rs_name = "SELECT id, Name FROM staffdirectory ORDER BY Name ASC"; $rs_name = mysql_query($query_rs_name, $staff) or die(mysql_error()); $row_rs_name = mysql_fetch_assoc($rs_name); $totalRows_rs_name = mysql_num_rows($rs_name); ?> And the code calling for the data in the table: <?php do { ?> <tr> <td><?php echo "<a href=\"viewstaff.php?id=".$row_rs_name['id']."\">".$row_rs_name['Name']."</a>"; ?></td> </tr> <?php } while ($row_rs_name = mysql_fetch_assoc($rs_name)); ?> Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted August 3, 2007 Share Posted August 3, 2007 DUR! Jeez, Here: if (isset($_GET['dept'])) { //Page first accessed $dept = $_GET['Department_Title']; } if (isset($_POST['dept'])) { //form has been submit $dept = $_POST['Department_Title']; } Why would you check that $_GET['dept'] exists then try to call $_GET['Department_Title']? Use this: if (isset($_GET['dept'])) { //Page first accessed $dept = $_GET['dept']; } if (isset($_POST['dept'])) { //form has been submit $dept = $_POST['dept']; } Do you understand why that is different? If not then I didn't really help you. Replace your code with this: <?php require_once('Connections/staff.php'); if (isset($_GET['dept'])) { $dept = $_GET['Department_Title']; } else if (isset($_POST['dept'])) { $dept = $_POST['Department_Title']; } function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if(!get_magic_quotes_gpc()) { $theValue = stripslashes($theValue); } $theValue = (function_exists("mysql_real_escape_string")) ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } mysql_select_db($database_staff, $staff); $query_rs_name = "SELECT id, Name FROM staffdirectory WHERE `dept` = '".$dept."'ORDER BY Name ASC"; if($rs_name = mysql_query($query_rs_name, $staff)) { $row_rs_name = mysql_fetch_assoc($rs_name); $totalRows_rs_name = mysql_num_rows($rs_name); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.