Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php插入数据库值变为0_Php_Mysql_Mysqli - Fatal编程技术网

php插入数据库值变为0

php插入数据库值变为0,php,mysql,mysqli,Php,Mysql,Mysqli,我刚开始为一个学校项目编写PHP,我偶然发现了一个问题。我有一个API,我将一些数据发布到它。数据是传感器的名称、ID和类别。当我尝试将其插入mySQL数据库时,它没有输入正确的数据。它不输入实际数据,而是将0输入到除建筑外的每一列。甚至是状态列,它是一个硬编码字符串。 我已经尝试了几乎所有的东西(以我有限的PHP知识所知),但我被难倒了。 require_once "../meta.php"; $tmpdevice = clean($_POST['label']); $device = st

我刚开始为一个学校项目编写PHP,我偶然发现了一个问题。我有一个API,我将一些数据发布到它。数据是传感器的名称、ID和类别。当我尝试将其插入mySQL数据库时,它没有输入正确的数据。它不输入实际数据,而是将0输入到除建筑外的每一列。甚至是状态列,它是一个硬编码字符串。 我已经尝试了几乎所有的东西(以我有限的PHP知识所知),但我被难倒了。
require_once "../meta.php";

$tmpdevice = clean($_POST['label']);
$device = str_replace('¥', ':', $tmpdevice);

$tmpgenericClass = clean($_POST['zwave_class_generic']);
$genericClass = str_replace('¥', ':', $tmpgenericClass);

$tmpuniqueID = clean($_POST['thingUID']);
$uniqueID = str_replace('¥', ':', $tmpuniqueID);

$status = 'Not Accepted';
$building = 303;

if ($uniqueID == "") {
    echo '{"status":"error"}';
    exit;
}
if ($device == "") {
    echo '{"status":"error"}';
    exit;
}
if ($genericClass == "") {
    echo '{"status":"error"}';
    exit;
}

$servername = DB_HOST;
$username = DB_USER;
$password = DB_PASSWORD;
$databasename = DB_NAME;

$conn = new mysqli($servername, $username, $password, $databasename);
if ($conn->connect_error) {
    die("Connection error: " . $conn->connect_error);
    exit;
}

$stmt = $conn->prepare("INSERT IGNORE INTO Actuator (UniqueID, Device, GenericClass, Status, Building) values (?,?,?,?,?)");
$stmt->bind_param("idddd", $uniqueID, $device, $genericClass, $status, $building);

if ($stmt->execute()) { 
    echo '{"status":"ok"}';
} else {
echo '{"status":"error"}';
}

$conn->close();   

?>

问题在于大多数字段都是字符串(varchar),但是当您绑定参数时,会将它们绑定为数字(
i
-integer,
d
-double)

使用适当的数据类型进行绑定(
s
-string):


基督非常感谢你!Copy从另一个stackoverflow帖子中粘贴了该方法,对此没有多加考虑。非常感谢。
$stmt->bind_param("ssssi", $uniqueID, $device, $genericClass, $status, $building);