2016-01-24 31 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