0

First of all good day to everyone and sorry me if this question could be very simple but my knowledge in php are small.

I'm getting this error in the page:

*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 'WHERE compagni_id=NULL' at line 1*

here's my code, it quite long so I'm putting only the starting part where I think the error might be. I know it will be for sure a very silly mistake something like a missing comma or similar, but I cannot find nothing even with some PHP code inspector sites.

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",     $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $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;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form_compagni")) {
  $updateSQL = sprintf("UPDATE compagni SET av_compagni_numero=%s, av_compagni_tipo01=%s, av_livello_01=%s, av_nome01=%s, av_compagni_tipo02=%s, av_livello_02=%s, av_nome02=%s, anim_taglia01=%s, anim_tipo01=%s, anim_iniz01=%s, anim_velo01=%s, anim_pf01=%s, anim_ca01=%s, anim_ca_sprov01=%s, anim_ca_cont01=%s, anim_atta01=%s, anim_lotta01=%s, anim_spazi01=%s, anim_porta01=%s, anim_atsp01=%s, anim_quali01=%s, anim_temp01=%s, anim_rifl01=%s, anim_volo01=%s, anim_forz01=%s, anim_dest01=%s, anim_cost01=%s, anim_inte01=%s, anim_sagg01=%s, anim_cari01=%s, anim_abil01=%s, anim_tale01=%s, anim_coma01=%s, anim_ogge01=%s, cava_taglia01=%s, cava_tipo01=%s, cava_iniz01=%s, cava_velo01=%s, cava_pf01=%s, cava_ca01=%s, cava_ca_sprov01=%s, cava_ca_cont01=%s, cava_atta01=%s, cava_lotta01=%s, cava_spazi01=%s, cava_porta01=%s, cava_atsp01=%s, cava_quali01=%s, cava_temp01=%s, cava_rifl01=%s, cava_volo01=%s, cava_forz01=%s, cava_dest01=%s, cava_cost01=%s, cava_inte01=%s, cava_sagg01=%s, cava_cari01=%s, cava_abil01=%s, cava_tale01=%s, cava_ogge01=%s, comd_taglia01=%s, comd_tipo01=%s, comd_iniz01=%s, comd_velo01=%s, comd_pf01=%s, comd_ca01=%s, comd_ca_sprov01=%s, comd_ca_cont01=%s, comd_atta01=%s, comd_lotta01=%s, comd_spazi01=%s, comd_porta01=%s, comd_atsp01=%s, comd_quali01=%s, comd_temp01=%s, comd_rifl01=%s, comd_volo01=%s, comd_forz01=%s, comd_dest01=%s, comd_cost01=%s, comd_inte01=%s, comd_sagg01=%s, comd_cari01=%s, comd_abil01=%s, comd_tale01=%s, comd_coma01=%s, comd_ogge01=%s, comr_taglia01=%s, comr_tipo01=%s, comr_iniz01=%s, comr_velo01=%s, comr_pf01=%s, comr_ca01=%s, comr_ca_sprov01=%s, comr_ca_cont01=%s, comr_atta01=%s, comr_lotta01=%s, comr_spazi01=%s, comr_porta01=%s, comr_atsp01=%s, comr_quali01=%s, comr_temp01=%s, comr_rifl01=%s, comr_volo01=%s, comr_forz01=%s, comr_dest01=%s, comr_cost01=%s, comr_inte01=%s, comr_sagg01=%s, comr_cari01=%s, comr_abil01=%s, comr_tale01=%s, comr_coma01=%s, comr_ogge01=%s, fami_taglia01=%s, fami_tipo01=%s, fami_iniz01=%s, fami_velo01=%s, fami_pf01=%s, fami_ca01=%s, fami_ca_sprov01=%s, fami_ca_cont01=%s, fami_atta01=%s, fami_bab01=%s, fami_lotta01=%s, fami_spazi01=%s, fami_porta01=%s, fami_atsp01=%s, fami_quali01=%s, fami_temp01=%s, fami_rifl01=%s, fami_volo01=%s, fami_forz01=%s, fami_dest01=%s, fami_cost01=%s, fami_inte01=%s, fami_sagg01=%s, fami_cari01=%s, fami_abil01=%s, fami_tale01=%s, fami_ogge01=%s, greg_classe01=%s, greg_liv01=%s, greg_razza01=%s, greg_taglia01=%s, greg_iniz01=%s, greg_velo01=%s, greg_pf01=%s, greg_ca01=%s, greg_ca_sprov01=%s, greg_ca_cont01=%s, greg_atta01=%s, greg_bab01=%s, greg_lotta01=%s, greg_spazi01=%s, greg_porta01=%s, greg_atsp01=%s, greg_inca01=%s, greg_inca_classe01=%s, greg_inca_classe01_descr=%s, greg_quali01=%s, greg_temp01=%s, greg_rifl01=%s, greg_volo01=%s, greg_forz01=%s, greg_dest01=%s, greg_cost01=%s, greg_inte01=%s, greg_sagg01=%s, greg_cari01=%s, greg_abil01=%s, greg_tale01=%s, greg_ogge01=%s, greg_descr01=%s, anim_taglia02=%s, anim_tipo02=%s, anim_iniz02=%s, anim_velo02=%s, anim_pf02=%s, anim_ca02=%s, anim_ca_sprov02=%s, anim_ca_cont02=%s, anim_atta02=%s, anim_lotta02=%s, anim_spazi02=%s, anim_porta02=%s, anim_atsp02=%s, anim_quali02=%s, anim_temp02=%s, anim_rifl02=%s, anim_volo02=%s, anim_forz02=%s, anim_dest02=%s, anim_cost02=%s, anim_inte02=%s, anim_sagg02=%s, anim_cari02=%s, anim_abil02=%s, anim_tale02=%s, anim_coma02=%s, anim_ogge02=%s, cava_taglia02=%s, cava_tipo02=%s, cava_iniz02=%s, cava_velo02=%s, cava_pf02=%s, cava_ca02=%s, cava_ca_sprov02=%s, cava_ca_cont02=%s, cava_atta02=%s, cava_lotta02=%s, cava_spazi02=%s, cava_porta02=%s, cava_atsp02=%s, cava_quali02=%s, cava_temp02=%s, cava_rifl02=%s, cava_volo02=%s, cava_forz02=%s, cava_dest02=%s, cava_cost02=%s, cava_inte02=%s, cava_sagg02=%s, cava_cari02=%s, cava_abil02=%s, cava_tale02=%s, cava_ogge02=%s, comd_taglia02=%s, comd_tipo02=%s, comd_iniz02=%s, comd_velo02=%s, comd_pf02=%s, comd_ca02=%s, comd_ca_sprov02=%s, comd_ca_cont02=%s, comd_atta02=%s, comd_lotta02=%s, comd_spazi02=%s, comd_porta02=%s, comd_atsp02=%s, comd_quali02=%s, comd_temp02=%s, comd_rifl02=%s, comd_volo02=%s, comd_forz02=%s, comd_dest02=%s, comd_cost02=%s, comd_inte02=%s, comd_sagg02=%s, comd_cari02=%s, comd_abil02=%s, comd_tale02=%s, comd_coma02=%s, comd_ogge02=%s, comr_taglia02=%s, comr_tipo02=%s, comr_iniz02=%s, comr_velo02=%s, comr_pf02=%s, comr_ca02=%s, comr_ca_sprov02=%s, comr_ca_cont02=%s, comr_atta02=%s, comr_lotta02=%s, comr_spazi02=%s, comr_porta02=%s, comr_atsp02=%s, comr_quali02=%s, comr_temp02=%s, comr_rifl02=%s, comr_volo02=%s, comr_forz02=%s, comr_dest02=%s, comr_cost02=%s, comr_inte02=%s, comr_sagg02=%s, comr_cari02=%s, comr_abil02=%s, comr_tale02=%s, comr_coma02=%s, comr_ogge02=%s, fami_taglia02=%s, fami_tipo02=%s, fami_iniz02=%s, fami_velo02=%s, fami_pf02=%s, fami_ca02=%s, fami_ca_sprov02=%s, fami_ca_cont02=%s, fami_atta02=%s, fami_bab02=%s, fami_lotta02=%s, fami_spazi02=%s, fami_porta02=%s, fami_atsp02=%s, fami_quali02=%s, fami_temp02=%s, fami_rifl02=%s, fami_volo02=%s, fami_forz02=%s, fami_dest02=%s, fami_cost02=%s, fami_inte02=%s, fami_sagg02=%s, fami_cari02=%s, fami_abil02=%s, fami_tale02=%s, fami_ogge02=%s, greg_classe02=%s, greg_liv02=%s, greg_razza02=%s, greg_taglia02=%s, greg_iniz02=%s, greg_velo02=%s, greg_pf02=%s, greg_ca02=%s, greg_ca_sprov02=%s, greg_ca_cont02=%s, greg_atta02=%s, greg_bab02=%s, greg_lotta02=%s, greg_spazi02=%s, greg_porta02=%s, greg_atsp02=%s, greg_inca02=%s, greg_inca_classe02=%s, greg_inca_classe02_descr=%s, greg_quali02=%s, greg_temp02=%s, greg_rifl02=%s, greg_volo02=%s, greg_forz02=%s, greg_dest02=%s, greg_cost02=%s, greg_inte02=%s, greg_sagg02=%s, greg_cari02=%s, greg_abil02=%s, greg_tale02=%s, greg_ogge02=%s, greg_descr02 WHERE compagni_id=%s",
                   GetSQLValueString($_POST['av_compagni_numero'], "text"),
                   GetSQLValueString($_POST['av_compagni_tipo01'], "text"),

[...and here goes on with all the posting infos..]

GetSQLValueString($_POST['greg_ogge02'], "text"),
                   GetSQLValueString($_POST['greg_descr02'], "text"),
                   GetSQLValueString($_POST['compagni_id'], "int"));

  mysql_select_db($database_Database, $Database);
  $Result1 = mysql_query($updateSQL, $Database) or die(mysql_error());

  $updateGoTo = "compagni.php?" . $row_compagni['compagni_id'] . "=" .     $row_compagni['compagni_id'] . "";
  if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}

$colname_compagni = "1";
if (isset($_GET['compagni_id'])) {
 $colname_compagni = $_GET['compagni_id'];
}
mysql_select_db($database_Database, $Database);
$query_compagni = sprintf("SELECT * FROM compagni WHERE compagni_id = %s",     GetSQLValueString($colname_compagni, "int"));
$compagni = mysql_query($query_compagni, $Database) or die(mysql_error());
$row_compagni = mysql_fetch_assoc($compagni);
$totalRows_compagni = mysql_num_rows($compagni);

$colname_compagni = "1";
if (isset($_GET['compagni_id'])) {
  $colname_compagni = $_GET['compagni_id'];
}
mysql_select_db($database_Database, $Database);
$query_compagni = sprintf("SELECT * FROM compagni WHERE compagni_id = %s",     GetSQLValueString($colname_compagni, "int"));
$compagni = mysql_query($query_compagni, $Database) or die(mysql_error());
$row_compagni = mysql_fetch_assoc($compagni);
?>

I know the code is little messy but I'm working with Dreamweaver (unfortunatly) and he's coding the php code.

Where is the error?

Thank you in advice for your patience reading all of the code!

3
  • 2
    Print the raw SQL query and execute it manually using phpMyAdmin or your MySQL command prompt. Does it work? Commented Mar 7, 2014 at 13:28
  • 1
    Try to echo $query_compagni; after the sprintf to see the query produced. Commented Mar 7, 2014 at 13:29
  • mysql_query is deprecated use mysqli Commented Mar 7, 2014 at 13:36

2 Answers 2

1

The best way to track down a mysql error is by looking at the actual error. In this case:

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 'WHERE compagni_id=NULL' at line 1

So we see that we have a syntax error near WHERE ... so we will look and see what comes directly before the near string. If this is ambiguous for long queries with multiple unions or joins, try adding line breaks and running the query again to get the line number.

So, the part of the query right before the error was greg_descr02.

The next thing to do is look for what the syntax should be and what you actually have.

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

Aw, we see that the only thing that should come right before the WHERE is a column name followed by an equal sign and then either a expression or DEFAULT. you missed the equal sign and the value.

I hope this helps you in the future.

Give a man a fish and he eats for a day, teach a man to fish and he will eat for the rest of his life.

Sign up to request clarification or add additional context in comments.

Comments

0

You are not setting the value for greg_descr02. Change

greg_descr02 WHERE compagni_id=%s

To

greg_descr02=%s  WHERE compagni_id=%s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.