在一个mysql查询中更新两个表(updating two tables in one mysql query)

编程入门 行业动态 更新时间:2024-10-25 00:29:21
在一个mysql查询中更新两个表(updating two tables in one mysql query)

我在AJAX调用结束时有一个很好的一点点php / mysql工作,它会更新一个表。 我现在需要更改此更新,以便帖子的PART更新一个表,另一个部分更新另一个表。 我不清楚如何正确地做到这一点。 这是我迄今为止所做的工作的代码:

if(isset($_POST['p2From'])) { $userInput = $_POST['p2From']; if(trim($userInput) == "") { $userInput = NULL; } try { $stmt = $conn->prepare("UPDATE $database.app_$applicationKey SET `p2From` = :userinput, `lastModified` = :time WHERE `appID` = :appid"); $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 8); $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11); $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11); $stmt->execute(); } catch(PDOException $e) { catchMySQLerror($e->getMessage()); } }

我想要做的是有效地实现这一目标:

if(isset($_POST['p2From'])) { $userInput = $_POST['p2From']; if(trim($userInput) == "") { $userInput = NULL; } try { $stmt = $conn->prepare("UPDATE $database.app_AIGtabs SET `p2From` = :userinput WHERE `appID` = :appid"); $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 8); $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11); $stmt->execute(); } catch(PDOException $e) { catchMySQLerror($e->getMessage()); } }

if(isset($_POST['p2From'])) { $userInput = $_POST['p2From']; if(trim($userInput) == "") { $userInput = NULL; } try { $stmt = $conn->prepare("UPDATE $database.app_$applicationKey SET `lastModified` = :time WHERE `appID` = :appid"); $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11); $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11); $stmt->execute(); } catch(PDOException $e) { catchMySQLerror($e->getMessage()); } }

但是,我想在一个更新查询中实现这一切,而不是两个。 我确信这是可能的,虽然我不清楚如何在一个mysql查询中执行此操作。 有人可以告诉我如何实现这一目标吗?

I've had a nice little bit of php/mysql working at the end of an AJAX call, which updates a table. I now need to change this update so that PART of the post updates one table and the other part updates another table. I'm unclear how to do this correctly. This is the code as to what I have had working to date:

if(isset($_POST['p2From'])) { $userInput = $_POST['p2From']; if(trim($userInput) == "") { $userInput = NULL; } try { $stmt = $conn->prepare("UPDATE $database.app_$applicationKey SET `p2From` = :userinput, `lastModified` = :time WHERE `appID` = :appid"); $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 8); $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11); $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11); $stmt->execute(); } catch(PDOException $e) { catchMySQLerror($e->getMessage()); } }

What I want to do is effectively achieve this:

if(isset($_POST['p2From'])) { $userInput = $_POST['p2From']; if(trim($userInput) == "") { $userInput = NULL; } try { $stmt = $conn->prepare("UPDATE $database.app_AIGtabs SET `p2From` = :userinput WHERE `appID` = :appid"); $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 8); $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11); $stmt->execute(); } catch(PDOException $e) { catchMySQLerror($e->getMessage()); } }

And

if(isset($_POST['p2From'])) { $userInput = $_POST['p2From']; if(trim($userInput) == "") { $userInput = NULL; } try { $stmt = $conn->prepare("UPDATE $database.app_$applicationKey SET `lastModified` = :time WHERE `appID` = :appid"); $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11); $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11); $stmt->execute(); } catch(PDOException $e) { catchMySQLerror($e->getMessage()); } }

However I'd like to ultimetly achieve this all in one update query as opposed to two. I'm sure this is possible, though I am very unclear how to perform this in one mysql query. Can someone show me how to achieve this?

最满意答案

使用多表更新语法: https : //dev.mysql.com/doc/refman/5.6/en/update.html

if(isset($_POST['p2From'])) { $userInput = $_POST['p2From']; if(trim($userInput) == "") { $userInput = NULL; } try { $stmt = $conn->prepare(" UPDATE $database.app_$applicationKey JOIN $database.app_AIGtabs on $database.app_$applicationKey.`appID` = $database.app_AIGtabs.`appID` SET `lastModified` = :time, `p2From` = :userinput WHERE $database.app_$applicationKey.`appID` = :appid;"); $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 8); $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11); $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11); $stmt->execute(); } catch(PDOException $e) { catchMySQLerror($e->getMessage()); } }

Use the Multiple-table Update syntax: https://dev.mysql.com/doc/refman/5.6/en/update.html

if(isset($_POST['p2From'])) { $userInput = $_POST['p2From']; if(trim($userInput) == "") { $userInput = NULL; } try { $stmt = $conn->prepare(" UPDATE $database.app_$applicationKey JOIN $database.app_AIGtabs on $database.app_$applicationKey.`appID` = $database.app_AIGtabs.`appID` SET `lastModified` = :time, `p2From` = :userinput WHERE $database.app_$applicationKey.`appID` = :appid;"); $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 8); $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11); $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11); $stmt->execute(); } catch(PDOException $e) { catchMySQLerror($e->getMessage()); } }

更多推荐

本文发布于:2023-04-27 14:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   mysql   updating   query   tables

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!