第二次更新表没有使用Ajax更新

时间:2016-07-05 14:53:22

标签: php jquery mysql ajax pdo

您好我在这里面临一个非常奇怪的问题。我有两个表连接,以获取数据。然后,在用户编辑数据后,我想单独更新表。其中一个表更新,另一个表没有。

我的代码:

$contact = $_POST['contact'];
    $address = $_POST['address'];
    $company = $_POST['company'];
    $activated = $_POST['activated'];
    $client_id = $_POST['client_id'];
    $level = $_POST['level'];
    $apikey = $_POST['apikey'];
    $phone = $_POST['phone'];


    $update_rest_clients = $handler->prepare("UPDATE rest_api_clients
        SET contact_person = ?, address = ?, company = ?,
            activated = ?, phone_number = ? WHERE client_id = ? ");

    $update_rest_clients->bindValue(1, $contact);
    $update_rest_clients->bindValue(2, $address);
    $update_rest_clients->bindValue(3, $company);

    if ($activated == 'true') {
        $update_rest_clients->bindValue(4, 'yes');
    } else {
        $update_rest_clients->bindValue(4, 'no');
    }

    $update_rest_clients->bindValue(5, $client_id);
    $update_rest_clients->bindValue(6, $phone);

    $update_rest_clients->execute();
    $update_rest_clients->closeCursor();

    $update_api_key = $handler->prepare("UPDATE rest_api_keys
             SET api_key = ?, level = ? WHERE user_id = ? ");

    $update_api_key->bindValue(1, $apikey);
    $update_api_key->bindValue(2, $level);
    $update_api_key->bindValue(3, $client_id);

    $update_api_key->execute();
    $update_api_key->closeCursor();

    if ($update_api_key) {
        echo "success api key";
    } 

    if ($update_rest_clients) {
        echo "Success rest client";
    }

浏览器中的Ajax响应:

Array
(
    [contact] => Name Surname
    [address] => Awesome Str. 8
    [company] => mycompany.com
    [apikey] => 09f9bae2fe72975f7da25d284139dc1ee
    [phone] => 00379305557229
    [level] => 10
    [activated] => true
    [client_id] => 3
)
<br />
success api keySuccess rest client

我的PDO $处理程序:

try{
        $handler = new PDO("mysql:host=".$dbhost.";dbname=".$dbname."", $dbuser, $dbpass);
        $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $handler->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES utf8mb4");
        $handler->exec("SET CHARACTER SET utf8mb4");
    }catch(Exception $e){
        echo $e->getMessage();
        die();
    }

这是第一个查询的错误信息,但我仍然不明白为什么表格没有得到更新。

Array ( [0] => 00000 [1] => [2] => ) 

我该怎么做才能成功?

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

在我看来,你的客户端和电话绑定值是错误的方式,所以你试图用电话号码更新ID,这意味着如果从0开始会失败,因为它永远不会匹配,没有任何意义获得更新,但查询仍将执行。见下文:

$update_rest_clients = $handler->prepare("UPDATE rest_api_clients SET contact_person = ?, address = ?, company = ?, activated = ?, phone_number = ? WHERE client_id = ? ");

$update_rest_clients->bindValue(1, $contact);
$update_rest_clients->bindValue(2, $address);
$update_rest_clients->bindValue(3, $company);

if ($activated == 'true') {
    $update_rest_clients->bindValue(4, 'yes');
} else {
    $update_rest_clients->bindValue(4, 'no');
}

//I have swapped these around.
$update_rest_clients->bindValue(6, $client_id);
$update_rest_clients->bindValue(5, $phone);

答案 1 :(得分:0)

级别是MySQl 5.7中的reserved word

对字段名称使用反对`

相关问题