Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SQL中的事务已提交,但只有1个查询正确完成_Php_Sql - Fatal编程技术网

Php SQL中的事务已提交,但只有1个查询正确完成

Php SQL中的事务已提交,但只有1个查询正确完成,php,sql,Php,Sql,我有两个表,在其中一个表中,我希望在特定行中增加一个计数器,同时在另一个表中进行插入。我使用的是php和MySQL,引擎是InnoDB 这是我的密码: function incrementCommentCounter ($id,$photoId,$text,$date) { $query1= "UPDATE photos SET comments=comments+1 WHERE IdPhoto=$photoId"; //$query2= "INSERT INTO allComments (Id

我有两个表,在其中一个表中,我希望在特定行中增加一个计数器,同时在另一个表中进行插入。我使用的是php和MySQL,引擎是InnoDB

这是我的密码:

function incrementCommentCounter ($id,$photoId,$text,$date) {
$query1= "UPDATE photos SET comments=comments+1 WHERE IdPhoto=$photoId";
//$query2= "INSERT INTO allComments (IdPhoto,IdUser,comment,date) VALUES ('%s',1,'%s',%s)";

//check if a user ID is passed
//if (!$id) errorJson('Authorization required');

mysql_query("START TRANSACTION");

$result1=mysql_query($query1);
$result2=mysql_query("INSERT INTO allComments (IdPhoto,IdUser,comment,dates) VALUES ('%s',1,'%s','%s')",$photoId,$text,$date);

if (!$result1 || !$result2) {
    mysql_query("ROLLBACK");
    errorJson($result2['error']);
} else {
    mysql_query("COMMIT");
    print json_encode(array('successful'=>1));
}
}

我做错了什么

谢谢

编辑:


修正了一些打字错误。现在,第二个查询从未正确执行,因此没有一个被执行。但是第二个查询有什么问题?

本机PHP函数
mysql\u error
可能会为您的查询中可能出现的语法错误(以及其他类型的问题)提供有价值的提示

此外,请考虑使用MySQL或PdoyMySQL作为旧的MySQL扩展,不适用于PHP5.5.0. 在第二个查询中,您可能想使用sprintf之类的字符串替换函数

$query = sprintf("INSERT INTO allComments (IdPhoto,IdUser,comment,dates) VALUES ('%s',1,'%s','%s')", $photoId, $text, $date);
$result = mysql_query($query);

您使用的是什么数据库引擎?如果是MySQL,那么MyISAM不支持事务,而InnoDB支持。。。。因此,这种信息产生了巨大的影响difference@MarkBaker不,是InnoDB。你能举一个例子说明你的第二条评论吗?你得到的是返回的errorJson还是成功的返回?如果代码采用的是
的哪条路径?@MarkBaker我得到了成功的返回。@劳伦斯检查我的编辑。