473,217 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,217 software developers and data experts.

Redirect Error Reported As SQL Syntax Error

I just spent waaaaaaaaaaaayy too much time trying to track down an error that was incorrectly reported just now, and I would like to see if someone
can explain to me why it was reported that way.

The purpose of the code is simply to delete a record and then redirect back to the page where the delete was started. The code looks like this:

elseif ($_GET[action] == "delete")
{
$query = "delete from product_subcategory2 where product_sku=$_GET[product_sku] and subcategory2_id=$_GET[subcategory2_id]";
$result = mysql_query($query) or die (mysql_error());
$affected_rows = mysql_affected_rows();

if ($affected_rows == 1)
{
header("Location:product.php?action=edit&product_s ku=$product_sku");
}
else
{
do_header();
echo("<center><p class=\"body\">Unable to delete Subcategory2. Please try again.</p>");
echo("<a href=\"product.php?action=edit&product_sku=$produc t_sku\">Edit Product</a>");
do_footer();
}
}

The SQL was find, but the error I had was in the header line. What I had was this:

header("Location:product.php?action=edit&product_s ku=<?php echo $product_sku?>");

so since I was already in PHP, I didn't need the "<?php echo" for $product_sku.

The problem was that the error message I got was "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." Aside from being extremely descriptive (yeah, right), the error message had nothing to do with my SQL
syntax, since it executed fine. Can anyone explain why an error with header() was reported as an SQL syntax error?

Thanks.

Steve
Jul 17 '05 #1
1 3320
Steve wrote:
I just spent waaaaaaaaaaaayy too much time trying to track down an error
that was incorrectly reported just now, and I would like to see if
someone can explain to me why it was reported that way.

The purpose of the code is simply to delete a record and then redirect
back to the page where the delete was started. The code looks like this:

elseif ($_GET[action] == "delete")
{
$query = "delete from product_subcategory2 where
product_sku=$_GET[product_sku] and subcategory2_id=$_GET[subcategory2_id]";
$result = mysql_query($query) or die (mysql_error());
$affected_rows = mysql_affected_rows();

if ($affected_rows == 1)
{

header("Location:product.php?action=edit&product_s ku=$product_sku");
}
else
{
do_header();
echo("<center><p class=\"body\">Unable to delete Subcategory2.
Please try again.</p>");
echo("<a
href=\"product.php?action=edit&product_sku=$produc t_sku\">Edit
Product</a>");
do_footer();
}
}

The SQL was find, but the error I had was in the header line. What I
had was this:

header("Location:product.php?action=edit&product_s ku=<?php echo
$product_sku?>");

so since I was already in PHP, I didn't need the "<?php echo" for
$product_sku.

The problem was that the error message I got was "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." Aside from
being extremely descriptive (yeah, right), the error message had nothing
to do with my SQL syntax, since it executed fine. Can anyone explain why
an error with header() was reported as an SQL syntax error?

Thanks.

Steve


Because the problem with your header statement was responsible for a
malformed SQL query. In your redirect, product_sku was filled with junk
and you then used it to form a query. Something I learned from the
perl world is to ALWAYS AND WITHOUT EXCEPTION quote values in your SQL
queries, even when they are (supposed to be) numeric. It is also
standard practice to use uppercase for SQL keywords. This makes it
easier to spot SQL syntax errors.

$query = "DELETE FROM product_subcategory2 WHERE
product_sku='$_GET[product_sku]' AND
subcategory2_id='$_GET[subcategory2_id]'";

The above query, with '$_GET[product_sku]' quoted, should have prevented
the SQL error because the junk was quoted and therefore the SQL parser
ignored it. Of course, your application would not have worked...

Also, rather than just using die(), I find it helpful to print out the
SQL I am sending the server when things break:

if (!$result = mysql_query($query))
{
echo "<br />$query<br />\n";
die (mysql_error());
}

Better yet, while you are developing a new application, have a var
$debug that you can set/clear and use it throughout your code to print
all SQL queries prior to sending them to the server. I find LOTS of
stupid mistakes this way.

if ($debug)
{
echo "<br />$query<br />\n";
}
if (!$result = mysql_query($query))...

Hope that helps!

NM

--
convert uppercase WORDS to single keystrokes to reply
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: \A_Michigan_User\ | last post by:
Ok, I give up... why do 1-4 work fine... but 5-6 give "can't find" errors? 1. Client-side VBscript code: call navigate("\\209.11.22.33\MyDir") 2. Client-side VBscript code: location.href...
1
by: iksrazal | last post by:
Hi all, I've been struggling to make this command work from Java: /usr/bin/mysql c4 --user=root --password=mypass -e "source /home/crissilva/c4.sql" Works fine as shown when run from the...
1
by: lkrubner | last post by:
Look at this page: http://www.lauradenyes.com/test3.html It contains this function: <script type="text/javascript"> function changeBackgroundImageZ (imageToChangeTo) {
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
1
by: Mukund Patel | last post by:
Hi friends, I have implemented custom error handling at page level and application level. If there is any syntax error in my aspx class file it will redirect to error page when I run the page....
5
by: r.nikhilk | last post by:
Hi, Currently, we are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by...
1
by: Gavin Chen | last post by:
Hello, I tried to install Tk400.200 on SunOS 4.1.4. At "make" time, I got the error message as below: cd pTk; make -e syntax OK -e syntax OK -e syntax OK -e syntax OK -e syntax OK -e...
15
by: madhu.ab | last post by:
Hi All, I am getting the following errors when i am including header file winuser.h I dont know whats happening. How will an error occur in winuser.h?? Please help. \microsoft visual...
2
by: xtremebass | last post by:
Hi Bytes, in Linux , is it possible to redirect Mysql table output to a file in Linux. i tried it, shows error , but output has displayed in linux prompt when no redirection of file has given(say...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.