mysqli execute生成错误的查询

时间:2017-01-29 13:06:08

标签: php mysql mysqli

这是一个简单且有效的UPDATE查询(好吧,INNER JOIN),并且php代码中没有错误。如果我在php外执行它,查询就会起作用:

UPDATE address_book ab
INNER JOIN address_book_client abc ON abc.contact_id = ab.id
SET ab.name = 'name1', ab.surname = 'surname', ab.cc = '34', ab.phone = '123456789', ab.email = 'a@a.aa', ab.nif = '12345678A', ab.note = 'Blah blah blah...'
WHERE abc.contact_id = 1 AND abc.company_id = 1

执行UPDATE的代码:

private $name;

// Populate AddressBook Object From User Input
public function PopulateFromUserInput($address_book) {

    $this->name         = $address_book['name'];

}

// New / Modify / Delete Contact
public function contact($option = '') {
    $mysqli = $this->aet->getAetSql();
    $exit   = FALSE;

    } else if ($option == 'modify') {

        if ($stmt = $mysqli->prepare('UPDATE address_book ab
                                      INNER JOIN address_book_client abc ON abc.contact_id = ab.id
                                      SET ab.name = ?, ab.surname = ?, ab.cc = ?, ab.phone = ?, ab.email = ?, ab.nif = ?, ab.note = ?
                                      WHERE abc.contact_id = ? AND abc.company_id = ?')) {

            $stmt->bind_param('ssiisssii', $this->name, $this->surname, $this->cc, $this->phone, $this->email, $this->nif, $this->note,
                                           $this->id, $this->uploader);

            $exit = 'User modified an existing contact.';

        } else return array(FALSE, $mysqli->error . '. ID: ' . $this->id);

    }

    if (!$stmt->execute()) {
        $exit = [FALSE, $stmt->error . '. ID: ' . $this->id];
    }

    return $exit;
}

我确保通过在var_dump($this->name);之前执行prepare()来确保新数据存在,并且没有任何问题。此外,$mysqli->error;$stmt->error;都没有错误。我在日志中收到$exit字符串,表示$stmt->execute()返回TRUE

现在,我启用了mysql日志并查看了一下:

1353 Connect   user@localhost as anonymous on table
1353 Query     SET NAMES utf8mb4
1353 Prepare   UPDATE address_book ab
               INNER JOIN address_book_client abc ON abc.contact_id = ab.id
               SET ab.name = ?, ab.surname = ?, ab.cc = ?, ab.phone = ?, ab.email = ?, ab.nif = ?, ab.note = ?
               WHERE abc.contact_id = ? AND abc.company_id = ?
1353 Execute   UPDATE address_book ab
               INNER JOIN address_book_client abc ON abc.contact_id = ab.id
               SET ab.name = 'name1', ab.surname = 'surname', ab.cc = 34, ab.phone = 123456789, ab.email = 'a@a.aa', ab.nif = '12345678A', ab.not$
               WHERE abc.contact_id = NULL AND abc.company_id = 1
1353 Quit

由于某种原因,生成的查询确实存在最后一个字段的问题。

这与UPDATE查询一起发生,而不是INSERT

1433 Connect   user@localhost as anonymous on table
1433 Query     SET NAMES utf8mb4
1433 Prepare   INSERT INTO address_book (name, surname, cc, phone, email, nif, note)
               VALUES (?, ?, ?, ?, ?, ?, ?)
1433 Execute   INSERT INTO address_book (name, surname, cc, phone, email, nif, note)
               VALUES ('name', 'surname', 34, 123456789, 'a@a.aa', '12345678A', 'Blah blah blah...')
1433 Close stmt
1433 Prepare   INSERT INTO address_book_client (contact_id, company_id)
               VALUES (?, ?)
1433 Execute   INSERT INTO address_book_client (contact_id, company_id)
               VALUES (3, 1)
1433 Quit

由于某些未知原因,execute()$之后以ab.not结束该行。

要确保我所做的变量没有问题:

// inside the contact() function
var_dump($this->note);

我得到的是:

string(17) "Blah blah blah..."

知道问题出在哪里?

1 个答案:

答案 0 :(得分:2)

我认为这可能是WHERE abc.contact_id = NULL

的问题

这应该是WHERE abc.contact_id IS NULL

有关详细信息,请参阅discussion