我無法使用PHP腳本插入值到MySQL數據庫PHP MySQL的POST插入
<?php
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $db) or die (mysqli_error($conn)); mysqli_select_db($conn, $db) or die (mysqli_error($conn));
$token = null; $lastWaterDate = null; $lastWaterAmount = null; $zipCode = null; $waterToDate = null; $addKey = null;
if(isset ($_POST["token"])){
$token = $_POST["token"];
}
if(isset ($_POST["lastWaterDate"])){
$lastWaterDate = $_POST["lastWaterDate"];
}
if(isset ($_POST["lastWaterAmount"])){
$lastWaterAmount = $_POST["lastWaterAmount"];
}
if(isset ($_POST["zipCode"])){
$zipCode = $_POST["zipCode"];
}
if(isset ($_POST["waterToDate"])){
$waterToDate = $_POST["waterToDate"];
}
if(isset ($_POST["addKey"])){
$addKey =$_POST["addKey"];
}
if($addKey == -1){
$sql = "INSERT INTO UserInfo (userToken) VALUES ('$token')";
}
elseif($addKey == 0){
$sql = "INSERT INTO UserInfo (userToken, lastWaterDate, lastWaterAmount, zipCode, waterToDate) VALUES ('$token','$lastWaterDate','$lastWaterAmount','$zipCode','$waterToDate') ON DUPLICATE KEY UPDATE lastWaterDate=VALUES(lastWaterDate), lastWaterAmount=VALUES(lastWaterAmount), waterToDate=VALUES(waterToDate)";
}
else{
$sql = "INSERT INTO UserInfo (userToken, lastWaterDate, lastWaterAmount, zipCode, waterToDate) VALUES ('$token','$lastWaterDate','$lastWaterAmount','$zipCode','$waterToDate') ON DUPLICATE KEY UPDATE lastWaterDate=VALUES(lastWaterDate), lastWaterAmount=VALUES(lastWaterAmount), zipCode=VALUES(zipCode), waterToDate=VALUES(waterToDate)";
}
$res = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if($res){
echo "Successfully updated db";
}
else{
echo "Failed to update db";
}
mysqli_close($conn);
?>
當addKey是= -1插入工作正常,但是當它是沒有什麼似乎發生在數據庫和我無法弄清楚爲什麼。
我已經嘗試使用新的密鑰和重複密鑰,但在數據庫中沒有任何更改。非常感謝
如果您echo $ sql'然後手動執行該查詢會發生什麼? – Barmar
你是否收到'成功更新數據庫'的消息? – Barmar
你永遠不會收到'更新數據庫失敗'的消息,因爲當它失敗時它會轉到'die(mysqli_error($ conn)')。 – Barmar