PHP MySQL插入查询未按预期工作

时间:2018-02-18 22:42:09

标签: mysql

我遇到的问题是我正在编写的PHP程序,我无法确定问题所在。

以下是该计划:

   <?php
require 'dbSensorData.php';

$sql = "INSERT INTO `data` (`timeStamp`, `MessageType`, `UniqueID`, `SensorID`, `SoilMoistureOne`, `SoilMoistureTwo`, `SoilMoistureThree`, `SoilMoistureFour`, `SoilMoistureFive`, `SoilMoistureSix`, `SoilTempOne`, `SoilTempTwo`, `SoilTempThree`, `SoilTempFour`, `SoilTempFive`, `SoilTempSix`, `SoilMoistureWM1`, `SoilMoistureWM2`, `SoilMoistureWM3`, `Rainfall`, `WindSpeed`, `WindDirection`, `temperature`, `humidity`) VALUES (CURRENT_TIMESTAMP, \'0010\',\'00000\',\'000\',\'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'xxx\')";

    $add = $mysqli->query($sql);

    if ( $add )
    {
        echo "Adding data to DB was Successful";
    }

    else 
    {
        echo "Adding data to DB failed";
    }

?>

我遇到了这部分代码的问题:

    $sql = "INSERT INTO `data` (`timeStamp`, `MessageType`, `UniqueID`, `SensorID`, `SoilMoistureOne`, `SoilMoistureTwo`, `SoilMoistureThree`, `SoilMoistureFour`, `SoilMoistureFive`, `SoilMoistureSix`, `SoilTempOne`, `SoilTempTwo`, `SoilTempThree`, `SoilTempFour`, `SoilTempFive`, `SoilTempSix`, `SoilMoistureWM1`, `SoilMoistureWM2`, `SoilMoistureWM3`, `Rainfall`, `WindSpeed`, `WindDirection`, `temperature`, `humidity`) VALUES (CURRENT_TIMESTAMP, \'0010\',\'00000\',\'000\',\'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'xxx\')";

    $add = $mysqli->query($sql);

代码执行但是,表中没有添加新条目,代码提供以下输出:

Adding data to DB failed

这必须意味着$ mysqli-&gt;查询($ sql)返回一个假值。

但是,当我使用以下SQL使用PHPMyAdmin插入到表中时,该条目将添加到表中而不会出现任何问题:

INSERT INTO `data` (`timeStamp`, `MessageType`, `UniqueID`, `SensorID`, `SoilMoistureOne`, `SoilMoistureTwo`, `SoilMoistureThree`, `SoilMoistureFour`, `SoilMoistureFive`, `SoilMoistureSix`, `SoilTempOne`, `SoilTempTwo`, `SoilTempThree`, `SoilTempFour`, `SoilTempFive`, `SoilTempSix`, `SoilMoistureWM1`, `SoilMoistureWM2`, `SoilMoistureWM3`, `Rainfall`, `WindSpeed`, `WindDirection`, `temperature`, `humidity`) VALUES (CURRENT_TIMESTAMP, \'0010\',\'00000\',\'000\',\'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'00.00\', \'xxx\')

此外,这是我的dbSensorData.php信息,省略了实际密码:

    <?php
$host = 'localhost';
$user = 'root';
$pass = 'password';
$db = 'SensorData';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
?>

此外,这是我用来创建MySQL表的命令:

CREATE TABLE `sensorData`.`data` 
(
    `timeStamp` TIMESTAMP NOT NULL PRIMARY KEY,
    `MessageType` INT NOT NULL,
    `UniqueID` VARCHAR(64) NOT NULL,
    `SensorID` VARCHAR(64),
    `SoilMoistureOne` FLOAT,
    `SoilMoistureTwo` FLOAT,
    `SoilMoistureThree` FLOAT,
    `SoilMoistureFour` FLOAT,
    `SoilMoistureFive` FLOAT,
    `SoilMoistureSix` FLOAT,
    `SoilTempOne` FLOAT,
    `SoilTempTwo` FLOAT,
    `SoilTempThree` FLOAT,
    `SoilTempFour` FLOAT,
    `SoilTempFive` FLOAT,
    `SoilTempSix` FLOAT,
    `SoilMoistureWM1` FLOAT,
    `SoilMoistureWM2` FLOAT,
    `SoilMoistureWM3` FLOAT,
    `Rainfall` FLOAT,
    `WindSpeed` FLOAT,
    `WindDirection` VARCHAR(32),
    `temperature` FLOAT,
    `humidity` FLOAT
);

非常感谢任何帮助,因为我对此深感不安。

1 个答案:

答案 0 :(得分:1)

这些逃脱报价是否有原因?它对我的phpMyAdmin中的反斜杠不起作用。我不得不通过代码并取消那些引号。

INSERT INTO `data` (`timeStamp`, `MessageType`, `UniqueID`, `SensorID`, `SoilMoistureOne`, `SoilMoistureTwo`, `SoilMoistureThree`, `SoilMoistureFour`, `SoilMoistureFive`, `SoilMoistureSix`, `SoilTempOne`, `SoilTempTwo`, `SoilTempThree`, `SoilTempFour`, `SoilTempFive`, `SoilTempSix`, `SoilMoistureWM1`, `SoilMoistureWM2`, `SoilMoistureWM3`, `Rainfall`, `WindSpeed`, `WindDirection`, `temperature`, `humidity`) VALUES (CURRENT_TIMESTAMP, '0010','00000','000','00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', '00.00', 'xxx')

这个查询对我有用。

否则,您可以使用mysqli_error()$mysqli->error)查看what kind of error MySQLi投掷。可能会给你一个暗示它失败的原因。