从PHP更新查询不起作用

时间:2017-06-10 16:34:43

标签: php mysql pdo

我从PHP代码运行此更新查询:

$update_begin_insurance = "UPDATE `vehicles`
                           SET `begin_insurance_date` = '$begin_insurance'
                           WHERE `plate` = '$plate'";

$conn->query($update_begin_insurance);

$connPDO个对象。

问题是$conn抛出了任何异常,但我的数据库中的vehicles表没有更新。所以,我试图直接通过phpmyadmin运行这个查询,它运行正常,所以我认为它是一个PHP问题,但我无法弄清楚问题出在哪里。 我的begin_insurance_date列的类型为DATE$begin_insurance是一个格式正确的字符串(YYYY-MM-DD,我已尝试使用2017-06-10此代码)。

我正在使用MySQL DBMS

这是echo的{​​{1}}:

$update_begin_insurance

更新

这是我页面的完整PHP代码:

UPDATE `vehicles`
SET `begin_insurance_date` = '2017-06-10'
WHERE `plate` = 'ccccc'

正如我之前所说,我没有得到任何异常(正如你所看到的,我还打印<?php require_once "connect_db.php"; $plate = $_POST["plate"]; $begin_insurance = $_POST["begin_insurance"]; $end_insurance = $_POST["end_insurance"]; $fuel_economy = $_POST["fuel_economy"]; $fuel_type = $_POST["fuel_type"]; $response = array(); try { $conn->beginTransaction(); if ($begin_insurance != "") { $update_begin_insurance = "UPDATE `vehicles` SET `begin_insurance_date` = '$begin_insurance' WHERE `plate` = '$plate'"; $conn->query($update_begin_insurance); } if ($end_insurance != "") { $update_end_insurance = "UPDATE `vehicles` SET `end_insurance_date` = '$end_insurance' WHERE `plate` = '$plate'"; $conn->query($update_end_insurance); } if ($fuel_economy != "") { $update_fuel_economy = "UPDATE `vehicles` SET `fuel_economy` = $fuel_economy WHERE `plate` = '$plate'"; $conn->query($update_fuel_economy); } if ($fuel_type != "") { $update_fuel_type = "UPDATE `vehicles` SET `id_fuel` = $fuel_type WHERE `plate` = '$plate'"; $conn->query($update_fuel_type); } $response["post"] = $_POST; $response["error_code"] = "0"; $response["error_message"] = "none"; $response["driver_error_code"] = "0"; } catch (PDOException $e) { if ($conn->inTransaction()) { $conn->rollBack(); } $response["post"] = $_POST; $response["error_code"] = $e->getCode(); $response["error_message"] = $e->getMessage(); $response["driver_error_code"] = $e->errorInfo[1]; } echo json_encode($response); ?> 数组来检查是否正确接收了参数,是的,它们是)。 这是$_POST打印的内容:

echo json_encode($response)

我确定连接正常,因为我有其他PHP文件执行了一些 { "post": { "plate":"ccccc", "begin_insurance":"2017-06-10" }, "error_code":"0", "error_message":"none", "driver_error_code":"0" }查询,并且它们正常工作。

1 个答案:

答案 0 :(得分:0)

我已经解决了问题,我错过了$conn->commit()