2016-01-24 63 views
0

我想更新兩個表,名爲:routing和routing_has_work_center如何更新mysql中的2個表?

在路由表中,用戶可以在routing_has_work_center表中編輯描述,用戶可以編輯production_hour。

<?php 
session_start(); 
// include a php file that contains the common database connection codes 
include ("dbFunctions.php"); 

//retrieve computer details from the textarea on the previous page 
$description = $_POST['description']; 
$production_hour=$_POST['$production_hour']; 

//retrieve id from the hidden form field of the previous page 
$theID = $_POST['routing_id']; 

$msg = ""; 

//build a query to update the table 
//update the record with the details from the form 
$queryUpdate = "UPDATE routing SET description='$description' WHERE  routing_id = $theID"; 


//execute the query 
$resultUpdate = mysqli_query($link, $queryUpdate) or die(mysqli_error($link)); 

//if statement to check whether the update is successful 
//store the success or error message into variable $msg 
if ($resultUpdate) { 
$msg = "Record updated successfully!"; 
} else { 
$msg = "Record not updated!"; 
} 
?> 

我有這樣上面的代碼,但是當我更新了生產時間,它仍然是相同的,並在routing_has_work_center數據庫沒有更新。

我是否必須在查詢中添加其他內容?

+0

您是否聽說過SQL注入? –

+2

你需要[與Bobby Tables的父母談談](http://bobby-tables.com/)。 –

+0

發佈代碼中只有一行使用'production_hour' - 即'$ production_hour = $ _ POST ['$ production_hour'];' - 那麼您會期待什麼? –

回答

0

檢查。

$queryUpdate = "UPDATE routing_has_work_center SET production_hour='$production_hour' WHERE routing_id = '$theID' "; 
0

如果你想更新2個表,你需要兩個UPDATE語句。 你有一個。你錯過了routing_has_work_center

嘗試這樣:

$update_routing_center = 'UPDATE routing_has_work_center SET production_hour = $production_hour WHERE routing_id = $theID"` 

把這一聲明通過mysqli_prepare($link, $update_routing_center); 之後,您可以運行該語句到數據庫中。


編輯:修改了它accomidate埃德治癒的評論

+1

請不要像另一個那樣運行這個查詢 - 請參閱http://php.net/manual/en/mysqli.prepare.php以避免SQL注入 –

+1

因爲這個建議提供了一個基本上破解的SQL注入解決方案:-1 –

+0

感謝您的警告。我對編程還不熟悉,所以我仍然在學習一些安全概念。 @NielsKeurentjes – Rhuarc13