PHP/MySQL - quot;BEGIN...COMMITquot; Not Working(PHP/MySQL - “开始...提交不工作)
问题描述
我正在寻找一种在单个查询中将数据插入两个数据库表的方法,这样如果一个失败,则不会保存(我不想要孤立的数据).我遇到了一个 Stack Overflow 问题,该问题向我展示了如何使用 BEGIN...COMMIT 来完成此操作,但它根本不起作用.
I was searching for a way to insert data into two database tables in a single query in such a way that if one failed, neither saved (I don't want orphaned data). I came across a Stack Overflow question that showed me how to use BEGIN...COMMIT to accomplish this, but it simply is not working.
这是我设置的查询:
$query = "BEGIN;
INSERT INTO content_subpages (title, url_referer) VALUES ('$pagetitle','$url_referer');
INSERT INTO ccm_main_menu (sub_item, sub_item_link,sub_item_sort_order) VALUES ('$pagetitle','$url_referer','$newsort');
COMMIT;";
mysql_query($query) or die (mysql_error());
我收到以下错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 'INSERT INTO content_subpages (title, url_referer) VALUES ('TESTING','testing') 附近使用的正确语法;' 在第 2 行
这是我第一次使用 BEGIN...COMMIT,所以我可能做错了什么是合理的,但我遵循了我提到的 Stack Overflow 问题的选定答案给出的 SQL Fiddle 示例的语法(http://stackoverflow.com/questions/12649706/mysql-insert-into-multiple-tables-in-same-query-with-begincommit),但它仍然无法正常工作.
This is my first time using BEGIN...COMMIT, so it's reasonable that I might be doing something wrong, but I followed the syntax of the SQL Fiddle example given by the selected answer to the Stack Overflow question I mentioned (http://stackoverflow.com/questions/12649706/mysql-insert-into-multiple-tables-in-same-query-with-begincommit), but it still won't work.
如果我可以在没有 BEGIN...COMMIT 的情况下轻松实现全有或全无"多重 INSERT 结果,那将是一个可以接受的解决方案.
If I can easily achieve the "all-or-nothing" multiple INSERT result without BEGIN...COMMIT, that would be an acceptable solution.
提前致谢
推荐答案
尝试将这些行分成多个 php 语句:
Try breaking the lines into multiple php statements:
$query = "BEGIN";
mysql_query($query) or die (mysql_error());
$query = "INSERT INTO content_subpages (title, url_referer) VALUES ('$pagetitle','$url_referer')";
mysql_query($query) or die (mysql_error());
$query = "INSERT INTO ccm_main_menu (sub_item, sub_item_link,sub_item_sort_order) VALUES ('$pagetitle','$url_referer','$newsort')";
mysql_query($query) or die (mysql_error())
$query = "COMMIT";
mysql_query($query) or die (mysql_error());
这篇关于PHP/MySQL - “开始...提交"不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP/MySQL - “开始...提交"不工作
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01