phpmysqldynamicinsert

mySQL Error - query is empty


Starnge problem i cant seem to find the root of,

I am using jqgrid which submits data for update edit and delete to my page server.php

as sometimes values are empty i have written code for the update/insert functions under a switch statement.

before the switch statement is run i have code that generatess the update and insert statements for the mySQL.

Just before the insert statement is run i echo out the sql call which returns:

my insert = INSERT INTO mapdata_mdt (id_etp,geoaddr_mdt,active_mdt,flag_mdt) VALUES(1 ,'n11hl' ,1 ,1 )

as far as i can acertain thats a valid sql query??? so why does it error with the following:

Error: Query was empty

am puzzled to this so any pointers would be graetly appreciated.

the full code is below:

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

 //get formdata
$fdOper = $_POST['oper'];
$myKeys = array();
$myValues = array();
$myID = GetSQLValueString($_POST['id'], 'int');

if(isset($_POST['id_etp'])){
    if ($_POST['id_etp'] !=''){
        array_push($myKeys, 'id_etp');
        array_push($myValues, GetSQLValueString($_POST['id_etp'], 'int'));
    };
};
if(isset($_POST['displayaddr_mdt'])){
    if ($_POST['displayaddr_mdt'] !=''){
        array_push($myKeys, 'displayaddr_mdt');
        array_push($myValues, GetSQLValueString($_POST['displayaddr_mdt'], 'text'));
    };
};
if(isset($_POST['geoaddr_mdt'])){
    if ($_POST['geoaddr_mdt'] !=''){
        array_push($myKeys, 'geoaddr_mdt');
        array_push($myValues, GetSQLValueString($_POST['geoaddr_mdt'], 'text'));
    };
};
if(isset($_POST['lat_mdt'])){
    if ($_POST['lat_mdt'] !=''){
        array_push($myKeys, 'lat_mdt');
        array_push($myValues, GetSQLValueString($_POST['lat_mdt'], 'text'));
    };
};
if(isset($_POST['lng_mdt'])){
    if ($_POST['lng_mdt'] !=''){
        array_push($myKeys, 'lng_mdt');
        array_push($myValues, GetSQLValueString($_POST['lng_mdt'], 'text'));
    };
};
if(isset($_POST['description_mdt'])){
    if ($_POST['description_mdt'] !=''){
        array_push($myKeys, 'description_mdt');
        array_push($myValues, GetSQLValueString($_POST['description_mdt'], 'text'));
    };
};
if(isset($_POST['website_mdt'])){
    if ($_POST['website_mdt'] !=''){
        array_push($myKeys, 'website_mdt');
        array_push($myValues, GetSQLValueString($_POST['website_mdt'], 'text'));
    };
};
if(isset($_POST['email_mdt'])){
    if ($_POST['email_mdt'] !=''){
        array_push($myKeys, 'email_mdt');
        array_push($myValues, GetSQLValueString($_POST['email_mdt'], 'text'));
    };
};
if(isset($_POST['telephone_mdt'])){
    if ($_POST['telephone_mdt'] !=''){
        array_push($myKeys, 'telephone_mdt');
        array_push($myValues, GetSQLValueString($_POST['telephone_mdt'], 'text'));
    };
};
if(isset($_POST['active_mdt'])){
    if ($_POST['active_mdt'] !=''){
        array_push($myKeys, 'active_mdt');
        array_push($myValues, GetSQLValueString($_POST['active_mdt'], 'int'));
    };
};
if(isset($_POST['flag_mdt'])){
    if ($_POST['flag_mdt'] !=''){
        array_push($myKeys, 'flag_mdt');
        array_push($myValues, GetSQLValueString($_POST['flag_mdt'], 'int'));
    };
};
$keyCount = count($myKeys);
$valCount = count($myValues);
$insertKeyStr ='';
$insertValStr = '';
$insertKeys = '';
$insertVals = '';
$updateStr = '';
$myUpdate = '';
$myInsert = '';
if($keyCount == $valCount){
    echo('Number of keys: '.$keyCount.' matches number of values: '.$valCount.'');
    //generate our sql
    for ( $i = 0; $i <= $keyCount-1; $i++) {
        $insertKeys .= $myKeys[$i].","; // generates list of keys for insert
        $insertVals .= "".$myValues[$i]." ,"; //generates list of values for insert
        $updateStr .= $myKeys[$i] . " = " . $myValues[$i] . ",";

    }
    //strip last comma from strings
    $insertKeys = substr($insertKeys, 0, -1);
    $insertVals = substr($insertVals, 0, -1);
    $updateStr = substr($updateStr, 0, -1);
    $myInsert = "INSERT INTO mapdata_mdt ($insertKeys) VALUES($insertVals)";
    $myUpdate = "UPDATE mapdata_mdt SET $updateStr WHERE id_mdt = $myID";

}else{
    echo('Number of keys: '.$keyCount.' does not match number of values: '.$valCount.'');
}

switch ($fdOper) {
    case 'edit':
        echo('my update = '.$myUpdate);
        if (!mysql_query(mysql_query($myUpdate, $growthConn))){
            die('Error: ' . mysql_error());
        }else{
            echo('Update ok');
        };
    break;
    case 'add':     
        echo('my insert = '.$myInsert);
        if (!mysql_query(mysql_query($myInsert, $growth_conn))){
            die('Error: ' . mysql_error());
        }else{
            echo('Insert ok');
        };
    break;
    case 'delete':
        mysql_query("delete from mapdata_mdt where id_mdt= $myID");
    break;
}

Solution

  • You have mysql_query calling mysql_query:

    if (!mysql_query(mysql_query($myInsert, $growth_conn))){