我有一個項目,我將數據從 ESP32 發送到 MySQL 數據庫。我正在使用一個 PHP 腳本來獲取 POST 并更新數據庫中的數據。腳本如下:<?php$servername = "localhost";// REPLACE with your Database name$dbname = "licenta";// REPLACE with Database user$username = "admin";// REPLACE with Database user password$password = "mihnea";// Keep this API Key value to be compatible with the ESP32 code provided in the project page$// If you change this value, the ESP32 sketch needs to match$api_key_value = "tPmAT5Ab3j7F9";$api_key= $spot = $distance = $vacancy = "";if ($_SERVER["REQUEST_METHOD"] == "POST") { $api_key = test_input($_POST["api_key"]); if($api_key == $api_key_value) { $spot = test_input($_POST["spot"]); $distance = test_input($_POST["distance"]); $vacancy = test_input($_POST["vacancy"]); }我的問題是,我的腳本不是搜索指定的位置并僅更新該特定行,而是更新所有行。我的數據庫是這樣構建的:MariaDB [licenta]> describe SensorData;+--------------+-----------------+------+-----+---------------------+-------------------------------+| Field | Type | Null | Key | Default | Extra |+--------------+-----------------+------+-----+---------------------+-------------------------------+| id | int(6) unsigned | NO | PRI | NULL | auto_increment || spot | varchar(30) | NO | | NULL | || mcu | varchar(30) | YES | | NULL | || distance | int(6) | YES | | NULL | || vacancy | varchar(10) | YES | | NULL | || reading_time | timestamp | NO | | current_timestamp() | on update current_timestamp() |+--------------+-----------------+------+-----+---------------------+-------------------------------+如何編寫查詢,例如,如果我發送 spot = "B1",它將只更新 B1 所在的行。謝謝!
1 回答

慕姐8265434
TA貢獻1813條經驗 獲得超2個贊
您在查詢中缺少WHERE
條件。$sql
您的代碼中也有語法錯誤 - 我想
$sql = "UPDATE SensorData SET distance = '" . $distance . "', vacancy = '" . $vacanc$
無效。
嘗試將$sql
變量值更改為
$sql = "UPDATE SensorData SET distance = '".$distance."', vacancy = '".$vacancy."' WHERE spot = '".$spot."'";
- 1 回答
- 0 關注
- 140 瀏覽
添加回答
舉報
0/150
提交
取消