2017-06-03 63 views
0

我無法使用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插入工作正常,但是當它是沒有什麼似乎發生在數據庫和我無法弄清楚爲什麼。

我已經嘗試使用新的密鑰和重複密鑰,但在數據庫中沒有任何更改。非常感謝

+0

如果您echo $ sql'然後手動執行該查詢會發生什麼? – Barmar

+0

你是否收到'成功更新數據庫'的消息? – Barmar

+1

你永遠不會收到'更新數據庫失敗'的消息,因爲當它失敗時它會轉到'die(mysqli_error($ conn)')。 – Barmar

回答

1

當addkey不是-1,你也許想UPDATE你的表,而不是INSERT INTO中心

看看這個約UPDATEINSERT的差異的詳細信息轉換爲What are differences between INSERT and UPDATE in MySQL?

此外,您還可以提供一些更多的信息有關數據庫和關於你的錯誤(如果你得到什麼?任何)

您的問題,很可能與您實施$ sql查詢的方式有關。專注於腳本的那部分

+0

我試圖'$ sql =「更新UserInfo設置lastWaterDate = $ lastWaterDate,lastWaterAmount = $ lastWaterAmount,zipCode = $ zipCode,waterToDate = $ waterToDate WHERE userToken = $ token「;' 但這也沒有幫助 – mooooo098

+0

另外,我似乎沒有得到任何錯誤 – mooooo098

0

問題可能是您從表單接收到字符串零「0」而不是整數。提交表單時,您的條件期望爲整數0。你需要強制一個int,然後在你的條件中使用$ _POST。請務必逃避帖子變量。

<?php 
$conn = mysqli_connect('localhost', 'xxx', 'xxx', 'xxx') or die (mysqli_error($conn)); 

mysqli_select_db($conn, 'test') or die (mysqli_error($conn)); 

$_POST['token'] = 'abca'; 
$_POST['lastWaterDate'] = '2017-12-12'; 
$_POST['lastWaterAmount'] = '$100'; 
$_POST['zipCode'] = '01010'; 
$_POST['waterToDate'] = ''; 

// Let's ensure that the value is an INT 
$_POST['addKey'] = (int)'0'; 

if(isset($_POST["token"])){ 
    $token = mysqli_real_escape_string($conn, $_POST["token"]); 
} 

if(isset($_POST["lastWaterDate"])){ 
    $lastWaterDate = mysqli_real_escape_string($conn, $_POST["lastWaterDate"]); 
} 

if(isset($_POST["lastWaterAmount"])){ 
    $lastWaterAmount = mysqli_real_escape_string($conn, $_POST["lastWaterAmount"]); 
} 

if(isset($_POST["zipCode"])){ 
    $zipCode = mysqli_real_escape_string($conn, $_POST["zipCode"]); 
} 

if(isset($_POST["waterToDate"])){ 
    $waterToDate = mysqli_real_escape_string($conn, $_POST["waterToDate"]); 
} 

if(isset($_POST["addKey"])){ 
    $addKey = mysqli_real_escape_string($conn, $_POST["addKey"]); 
} 

if($_POST['addKey'] == -1){ 
    $sql = "INSERT INTO UserInfo (userToken) VALUES ('$token')"; 
} elseif($_POST['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); 

if($res){ 
    echo "Successfully updated db"; 
} 
else{ 
    echo "Failed to update db"; 
} 
+0

@ mooooo098這是否回答你的問題? –

+0

我的回答對你有幫助嗎?如果有,請不要猶豫,將其標記爲最佳答案。 –