MySQL UPDATE查询无法正常工作

时间:2011-05-13 02:31:22

标签: php mysql sql sql-update

这个脚本有什么问题?我无法弄清楚这个问题!

function editAccount() {
  $id = $_POST['id'];   
  $rep = $_POST['rep'];
  $lastchange = date('mdY');
  $account = $_POST['account'];
  $areacode = $_POST['areacode'];
  $number = $_POST['number'];
  $address1 = $_POST['address1'];
  $address2 = $_POST['address2'];
  $city = $_POST['city'];
  $state = $_POST['state'];
  $zip = $_POST['zip'];
  $country = $_POST['country'];
  $fax = $_POST['fax'];
  $descmaker1 = $_POST['descmaker1'];
  $descmaker2 = $_POST['descmaker2'];
  $title = $_POST['title'];
  $email = $_POST['email'];
  $cvendor = $_POST['cvendor'];
  $cequipment = $_POST['cequipment'];
  $leaseexp1 = $_POST['leaseexp1'];
  $leaseexp2 = $_POST['leaseexp2'];
  $leaseexp3 = $_POST['leaseexp3'];
  $leaseexp4 = $_POST['leaseexp4'];
  $leaseexp5 = $_POST['leaseexp5'];
  $leaseexp6 = $_POST['leaseexp6'];
  $volume = $_POST['volume'];
  $notes = $_POST['notes'];     

  mysql_db_query("UPDATE accounts 
                     SET (lastchange = '$lastchange',
                             account = '$account',
                            areacode = '$areacode',
                              number = '$number',
                            address1 = '$address1',
                            address2 = '$address2',
                                city = '$city',
                               state = '$state',
                                 zip = '$zip',
                                 fax = '$fax',
                          descmaker1 = '$descmaker1',
                          descmaker2 = '$descmaker2',
                               title = '$title',
                               email = '$email',
                             cvendor = '$cvendor',
                          cequipment = '$cequipment',
                           leaseexp1 = '$leaseexp1',
                           leaseexp2 = '$leaseexp2',
                           leaseexp3 = '$leaseexp3',
                           leaseexp4 = '$leaseexp4',
                           leaseexp5 = '$leaseexp5',
                           leaseexp6 = '$leaseexp6',
                              volume = '$volume',
                               notes = '$notes')            
         WHERE id = $id");
}

错误:

警告:第83行/home/content/53/7311353/html/cca/accounts/include/processAct.php中mysql_db_query()的参数计数错误

行是:WHERE id=$id");

4 个答案:

答案 0 :(得分:2)

UPDATE命令中,丢失括号。而已。

另外,请注意SQL Injection

<强>更新

以下是最终代码的样子:

function editAccount() {
  $id = $_POST['id'];   
  $rep = $_POST['rep'];
  $lastchange = date('mdY');
  $account = $_POST['account'];
  $areacode = $_POST['areacode'];
  $number = $_POST['number'];
  $address1 = $_POST['address1'];
  $address2 = $_POST['address2'];
  $city = $_POST['city'];
  $state = $_POST['state'];
  $zip = $_POST['zip'];
  $country = $_POST['country'];
  $fax = $_POST['fax'];
  $descmaker1 = $_POST['descmaker1'];
  $descmaker2 = $_POST['descmaker2'];
  $title = $_POST['title'];
  $email = $_POST['email'];
  $cvendor = $_POST['cvendor'];
  $cequipment = $_POST['cequipment'];
  $leaseexp1 = $_POST['leaseexp1'];
  $leaseexp2 = $_POST['leaseexp2'];
  $leaseexp3 = $_POST['leaseexp3'];
  $leaseexp4 = $_POST['leaseexp4'];
  $leaseexp5 = $_POST['leaseexp5'];
  $leaseexp6 = $_POST['leaseexp6'];
  $volume = $_POST['volume'];
  $notes = $_POST['notes'];     

  mysql_query("UPDATE accounts 
                     SET  lastchange = '$lastchange',
                             account = '$account',
                            areacode = '$areacode',
                              number = '$number',
                            address1 = '$address1',
                            address2 = '$address2',
                                city = '$city',
                               state = '$state',
                                 zip = '$zip',
                                 fax = '$fax',
                          descmaker1 = '$descmaker1',
                          descmaker2 = '$descmaker2',
                               title = '$title',
                               email = '$email',
                             cvendor = '$cvendor',
                          cequipment = '$cequipment',
                           leaseexp1 = '$leaseexp1',
                           leaseexp2 = '$leaseexp2',
                           leaseexp3 = '$leaseexp3',
                           leaseexp4 = '$leaseexp4',
                           leaseexp5 = '$leaseexp5',
                           leaseexp6 = '$leaseexp6',
                              volume = '$volume',
                               notes = '$notes'         
         WHERE id = $id");
}

答案 1 :(得分:1)

这里的问题是你正在调用错误的函数。 mysql_db_query应为mysql_query

SET之后您也不需要括号。

关于sql注入,我建议你使用mysql_real_escape_string转义所有$_POST变量。

答案 2 :(得分:1)

According to PHP docs,已弃用的函数mysql_db_query()至少需要两个参数。首先是数据库名称,第二个是SQL查询。您只指定了SQL查询。

resource mysql_db_query ( string $database , string $query [, resource $link_identifier ] )

所以在你的情况下:

mysql_db_query('yourdatabasename', "UPDATE accounts set....");

现在更常见的是使用mysql_query(),它将使用先前使用mysql_select_db()选择的数据库。

答案 3 :(得分:0)

检查是否已建立mysql连接。 有时可能也是一个问题

mysql_query("UPDATE accounts 
                     SET (lastchange = '$lastchange',
                             account = '$account',
                            areacode = '$areacode',
                              number = '$number',
                            address1 = '$address1',
                            address2 = '$address2',
                                city = '$city',
                               state = '$state',
                                 zip = '$zip',
                                 fax = '$fax',
                          descmaker1 = '$descmaker1',
                          descmaker2 = '$descmaker2',
                               title = '$title',
                               email = '$email',
                             cvendor = '$cvendor',
                          cequipment = '$cequipment',
                           leaseexp1 = '$leaseexp1',
                           leaseexp2 = '$leaseexp2',
                           leaseexp3 = '$leaseexp3',
                           leaseexp4 = '$leaseexp4',
                           leaseexp5 = '$leaseexp5',
                           leaseexp6 = '$leaseexp6',
                              volume = '$volume',
                               notes = '$notes')            
         WHERE id = $id") 

这是更新查询的语法,使用mysql_query而不是mysql_db_query