PHP - 列数与值计数不匹配。

时间:2014-04-22 06:56:28

标签: php mysql

你好我的代码。

print_r($_POST);
foreach ($_POST as $key => $value){
  echo $$key  =  $value;
}

echo $dpopName1= json_encode($_POST['dpopName']);
echo $dpopPosition1= json_encode($_POST['dpopPosition']);
echo $dpopLocation1= json_encode($_POST['dpopLocation']);

    if($sub_cat=='all') {
    $sub_cat='';    
    }

//$sql = mysql_query("INSERT INTO venders(vender_name, contact_personName,category, subcategory,email,contact_no,address,city,state,pin_code) VALUES ('$vendor_name', '$cont_personName','$cat_type','$sub_cat','$vendor_email', '$contact_no','$vendor_address','$state','$city','$vendor_pin')") or die(mysql_error());
  $insertQuery=mysql_query("INSERT IGNORE INTO `venders`(`vender_name`, `vendor_ddNo`, `vendor_ddDate`,     `vendor_regid`, `dateCorp`, `contact_personName`, `category`, `subcategory`, `email`, `contact_no`, `fax_no`,      `website`, `address`, `city`, `taluka`, `state`, `pin_code`, `vendor_Baddress`, `Bstate`, `Bcity`, `Bvendor_pin`,      `Btaluka`, `Bcontact_no`, `Bfax_no`, `Bvendor_email`, `Bwebsite`, `govtUndertake`, `stateUndertake`, `publiclCompany`,      `privatelCompany`, `copSociaty`, `partnerFirm`, `propritorship`, `anyOtherspecify`, `dpopName`, `dpopPosition`,       `dpopLocation`, `panDetails`, `panRequireFor`, `serviceTaxregDetails`, `serviceTaxrequiredFor`, `pfRegisterDetails`,       `pfRequireFor`, `excDutyreg`, `excRequireFor`, `labourLicensedetails`, `licencePanrequireFor`, `stvRegNo`,       `stvRequireFor`, `stvregDateDetails`, `stvregDateRequiredFor`, `orgStrngth`, `NameofCompany`, `NameofBank`,       
     `nameOfbankBranch`, `cityofMB`, `accountNo`, `accountType`, `bankBranchIfsccode`, `micodeofBankbranch`,       `otherBankerDetails`) 
     VALUES ('$vendor_name','$vendor_ddNo','$vendor_ddDate','$vendor_id','$dateCorp','$cont_personName','$vendor_address',      '$cat_type','$sub_cat','$vendor_email','$contact_no','$fax_no','$website','$vendor_address','$city','$taluka','$state','$vendor_pin',      '$vendor_Baddress','$Bstate','$Bcity','$Bvendor_pin','$Btaluka','$Bcontact_no','$Bfax_no','$Bvendor_email','$Bwebsite','$govtUndertake',      '$stateUndertake', '$publiclCompany',      '$privatelCompany', '$copSociaty', '$partnerFirm', '$propritorship', '$anyOtherspecify', '$dpopName1', '$dpopPosition1',       '$dpopLocation1','$panDetails', '$panRequireFor', '$serviceTaxregDetails', '$serviceTaxrequiredFor', '$pfRegisterDetails',       '$pfRequireFor', '$excDutyreg', '$excRequireFor', '$labourLicensedetails', '$licencePanrequireFor', '$stvRegNo',       
     '$stvRequireFor', '$stvregDateDetails', '$stvregDateRequiredFor', '$orgStrngth', '$NameofCompany', '$NameofBank',       '$nameOfbankBranch', '$cityofMB', '$accountNo', '$accountType', '$bankBranchIfsccode', '$micrCodeofBankbranch',       '$otherBankerDetails'
    )")  or die(mysql_error());  
  1. 数组到字符串转换错误
  2. 列数与第1行的值计数不匹配。
  3. 我试着解决这个问题:

    1. 用于解决第一个错误我使用json编码将数组转换为json字符串,然后将json字符串变量放入insert表中。
    2. 列计数错误我检查了我的数据库ID是否有主键,它有一个主键,然后我也计算所有的值和列,我也交叉检查所有的语法,但我什么都没发现。

3 个答案:

答案 0 :(得分:3)

一种更好的方法......当你插入很多列时插入是......使用INSERT INTO SET,因为那样你就可以更好地了解哪些列丢失了......而不是仔细检查映射的INSERT,并检查每个值是否包含每个列。

INSERT INTO sometable
   SET field1 = 'value1',
       field2 = 'value2',
       field3 = 'value3'

Milov,2004)。

它还有助于escape您插入的每个值(但请记住......建议使用PDO而不是MySQL Real Escape。

答案 1 :(得分:0)

如果您的任何变量包含会导致查询的'

当然最好使用DBO,但是因为我猜你试图在现有代码中实现一些东西,你应该尝试更多地调试它:

print_r($_POST);
foreach ($_POST as $key => $value){
  echo $$key  =  mysql_real_escape_string($value);
}

echo $dpopName1= mysql_real_escape_string(json_encode($_POST['dpopName']));
echo $dpopPosition1= mysql_real_escape_string(json_encode($_POST['dpopPosition']));
echo $dpopLocation1= mysql_real_escape_string(json_encode($_POST['dpopLocation']));

debug with:

$query="INSERT IGNORE INTO `venders`(`vender_name`, `vendor_ddNo`, `vendor_ddDate`, `vendor_regid`, `dateCorp`, `contact_personName`, `category`, `subcategory`, `email`, `contact_no`, `fax_no`, `website`, `address`, `city`, `taluka`, `state`, `pin_code`, `vendor_Baddress`, `Bstate`, `Bcity`, `Bvendor_pin`, `Btaluka`, `Bcontact_no`, `Bfax_no`, `Bvendor_email`, `Bwebsite`, `govtUndertake`, `stateUndertake`, `publiclCompany`, `privatelCompany`, `copSociaty`, `partnerFirm`, `propritorship`, `anyOtherspecify`, `dpopName`, `dpopPosition`, `dpopLocation`, `panDetails`, `panRequireFor`, `serviceTaxregDetails`, `serviceTaxrequiredFor`, `pfRegisterDetails`, `pfRequireFor`, `excDutyreg`, `excRequireFor`, `labourLicensedetails`, `licencePanrequireFor`, `stvRegNo`, `stvRequireFor`, `stvregDateDetails`, `stvregDateRequiredFor`, `orgStrngth`, `NameofCompany`, `NameofBank`, `nameOfbankBranch`, `cityofMB`, `accountNo`, `accountType`, `bankBranchIfsccode`, `micrCodeofBankbranch`, `otherBankerDetails`) VALUES ('$vendor_name','$vendor_ddNo','$vendor_ddDate','$vendor_id','$dateCorp','$cont_personName','$vendor_address', '$cat_type','$sub_cat','$vendor_email','$contact_no','$fax_no','$website','$vendor_address','$city','$taluka','$state','$vendor_pin', '$vendor_Baddress','$Bstate','$Bcity','$Bvendor_pin','$Btaluka','$Bcontact_no','$Bfax_no','$Bvendor_email','$Bwebsite','$govtUndertake', '$stateUndertake', '$publiclCompany', '$privatelCompany', '$copSociaty', '$partnerFirm', '$propritorship', '$anyOtherspecify', '$dpopName1', '$dpopPosition1', '$dpopLocation1','$panDetails', '$panRequireFor', '$serviceTaxregDetails', '$serviceTaxrequiredFor', '$pfRegisterDetails', '$pfRequireFor', '$excDutyreg', '$excRequireFor', '$labourLicensedetails', '$licencePanrequireFor', '$stvRegNo', '$stvRequireFor', '$stvregDateDetails', '$stvregDateRequiredFor', '$orgStrngth', '$NameofCompany', '$NameofBank', '$nameOfbankBranch', '$cityofMB', '$accountNo', '$accountType', '$bankBranchIfsccode', '$micrCodeofBankbranch', '$otherBankerDetails')";

if (!$insertQuery=mysql_query($query)){
  echo $query;
  die(mysql_error());
}

答案 2 :(得分:-3)

我注意到这个小错误: 你用

  

`

而不是

  

'

这是完全错误的。