Jump to content

Syntax Error


kyleldi

Recommended Posts

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);

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)); ?>

 

Link to comment
Share on other sites

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);
  }
?>

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.