Starnge 问题我似乎找不到根源,
我正在使用jqgrid,它将更新编辑和删除的数据提交到我的页面server.php
有时值是空的,我在 switch 语句下编写了更新/插入函数的代码。
在运行 switch 语句之前,我有代码为 mySQL 生成更新和插入语句。
在运行插入语句之前,我回显返回的 sql 调用:
my insert = INSERT INTO mapdata_mdt (id_etp,geoaddr_mdt,active_mdt,flag_mdt) VALUES(1 ,'n11hl' ,1 ,1 )
据我所知这是一个有效的sql查询???那么为什么会出现以下错误:
Error: Query was empty
对此感到困惑,因此我们将不胜感激。
完整代码如下:
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;
}
您有
mysql_query
致电mysql_query
:
if (!mysql_query(mysql_query($myInsert, $growth_conn))){