2017-07-14 63 views
-1

有人可以在這段代碼中指出錯誤嗎?我無法將數據更新到數據庫。我們正在向服務器發送文本消息,並且此文件在此解碼並將其設置在數據庫中。但這種情況在這裏不起作用的原因。我檢查並嘗試排除故障,但找不到問題。使用.php將數據寫入數據庫

case 23: 
    // Gather Variables 
    $Message = preg_replace("/\s+/","%20", $Message); 

    $UnixTime = time(); 
    $cycle = explode(":", $Message); 
    $machine_press = $cycle[0]; 
    $machine_pct_full = $machine_press/20; 
    $machine_cycles_return = $cycle[1]; 
    $machine_cycles_total = $cycle[2]; 

// Build SQL Statement to update static values in the machine table 
$sql = "UPDATE `machines` SET `machine_last_run`=".$UnixTime.",`machine_press`=".$machine_press.",`machine_pct_full`=".$machine_pct_full.",`machine_cycles_return`=".$machine_cycles_return.",`machine_cycles_total`=".$machine_cycles_total." WHERE `machine_serial`='$MachSerial'"; 

// Performs the $sql query on the server to update the values 
if ($conn->query($sql) === TRUE) { 
    // echo 'Entry saved successfully<br>'; 
} else { 
    echo 'Error: '. $conn->error; 
} 


    $sql = "INSERT INTO `cycles` (`cycle_sequence`,`cycle_timestamp`,`cycle_did`,`cycle_serial`,`cycle_03_INT`,`cycle_14_INT`,`cycle_15_INT`,`cycle_18_INT`)"; 
    $sql = $sql . "VALUES ($SeqNum,$UnixTime,'$siteDID','$MachSerial',$machine_press,$machine_cycles_total,$machine_cycles_return,$machine_pct_full)"; 

    // Performs the $sql query on the server to insert the values 
    if ($conn->query($sql) === TRUE) { 
    // echo 'Entry saved successfully<br>'; 
    } else { 
    echo 'Error: '. $conn->error; 
    } 

break; 
+5

phpmyadmin不是數據庫 –

+1

你得到什麼錯誤,客戶端和/或服務器端? – j08691

+0

@ j08691 我沒有得到任何錯誤..我們正在使用codeigniter框架,所以我沒有辦法獲取錯誤..或者可能是我不知道如何查找任何錯誤。 –

回答

0

需要更多信息來幫助解決您的問題。

首先,顯示錯誤,編輯的index.php文件在你笨 項目,更新它說:

define('ENVIRONMENT', 'production'); 

define('ENVIRONMENT', 'development'); 

然後你」我會準確地看到問題所在。這樣你就可以提供幫助你所需的信息。

+0

我到底會在哪裏看到這個錯誤?這會以任何方式影響我們的在線網站,這取決於這個index.php文件? –

+0

這可以讓你看到網站上的錯誤。如果您有獨立的DEV環境,則可以在此處進行更新,修復錯誤並將更新推送到您的LIVE站點 – Oluwaseye

+0

嘗試與社區共享控制器和模型,您可以通過http://codepad.org粘貼並共享它們/或任何其他的PHP sandox。你想讓我分享你的代碼在Codeigniter中的樣子嗎? – Oluwaseye

0

我剛纔看到你在插入字符串時沒有用撇號包裹它們。所以,你的查詢應該是:

$sql = "UPDATE `machines` SET `machine_last_run`='".$UnixTime."',`machine_press`='".$machine_press."',`machine_pct_full`='".$machine_pct_full."',`machine_cycles_return`='".$machine_cycles_return."',`machine_cycles_total`='".$machine_cycles_total."' WHERE `machine_serial`='$MachSerial'"; 

$sql = "INSERT INTO `cycles` (`cycle_sequence`,`cycle_timestamp`,`cycle_did`,`cycle_serial`,`cycle_03_INT`,`cycle_14_INT`,`cycle_15_INT`,`cycle_18_INT`)"; 
$sql = $sql . " VALUES ('$SeqNum','$UnixTime','$siteDID','$MachSerial','$machine_press','$machine_cycles_total','$machine_cycles_return','$machine_pct_full')"; 

對於任何類型的未知問題,我可以推薦PHP和SQL錯誤打開並使用一個名爲postman,我用它來測試我的API工具。您可以使用任何方法,頭文件和參數來模仿請求,並像提供者一樣發送「sms」或任何對您的API執行的操作。您可以看到應用程序拋出的錯誤。

編輯

我使用帶有'和db一個固定的版本測試腳本。

$Message = "value1:value2:value3"; 
$MachSerial = "someSerial"; 
$SeqNum = "someSeqNo"; 
$siteDID = "someDID"; 

$pdo = new PDO('mysql:host=someHost;dbname=someDb', 'someUser', 'somePass'); 

// Gather Variables 
$Message = preg_replace("/\s+/","%20", $Message); 

$UnixTime = time(); 
$cycle = explode(":", $Message); 
$machine_press = $cycle[0]; 
$machine_pct_full = (int)$machine_press/20; // <----- Note the casting to int. Else a warning is thrown. 
$machine_cycles_return = $cycle[1]; 
$machine_cycles_total = $cycle[2]; 

// Build SQL Statement to update static values in the machine table 
$sql = "UPDATE `machines` SET `machine_last_run`='$UnixTime',`machine_press`='$machine_press',`machine_pct_full`='$machine_pct_full',`machine_cycles_return`='$machine_cycles_return',`machine_cycles_total`='$machine_cycles_total' WHERE `machine_serial`='$MachSerial'"; 

try { 
    $pdo->query($sql); 
} catch (PDOException $e) { 
    echo 'Query failed: ' . $e->getMessage(); 
} 

$sql = "INSERT INTO `cycles` (`cycle_sequence`,`cycle_timestamp`,`cycle_did`,`cycle_serial`,`cycle_03_INT`,`cycle_14_INT`,`cycle_15_INT`,`cycle_18_INT`)"; 
$sql = $sql . "VALUES ('$SeqNum','$UnixTime','$siteDID','$MachSerial','$machine_press','$machine_cycles_total','$machine_cycles_return','$machine_pct_full')"; 

try { 
    $pdo->query($sql); 
} catch (PDOException $e) { 
    echo 'Query failed: ' . $e->getMessage(); 
} 

它完全有效。每插入一個循環並更新機器。在我通過添加包裝來修復它之前,我得到了很多錯誤。

+0

謝謝您的回覆。但似乎這不是代碼的問題。 –

+0

我發現了這個問題。問題不在於代碼,而在於它收到的文本消息。現在代碼不會忽略傳入消息中的空格。我應該在我的php代碼中添加什麼,以便忽略消息中的空格並將所有內容合併到一起,然後使用「:」分隔它(如代碼中所提到的) –

+0

您已經刪除了文本中的任何空格,但用編碼版本的空間。如果你想刪除它們,只需使用'$ str = preg_replace('/ \ s + /','',$ str);'。也許再次查看我編輯的答案中的代碼。 – Yolo

0

好吧,所以這是解決方案:

我更換了行:

$Message = preg_replace("/\s+/","%20", $Message); 

有:

$Message = preg_replace("/\s+/","", $Message); 

這將刪除所有的空格在我的文字消息,並使它成爲一個單一的斷開之前的字符串並將其分配給數據庫中的不同表。 我明白這個腳本並不是真正的問題,周圍沒有人會在回答之前知道實際問題。這就是爲什麼我發佈解決方案只是爲了更新這裏涉及的團隊。