[英]After Update Query Executed Affected rows returning 0 in mysql
I'm trying to update single column in a row from MySQL table. 我正在尝试从MySQL表中更新一行。 After the UPDATE
query is executed an UPDATE
is happening in MySQL Table. 执行UPDATE
查询后,MySQL表中将发生UPDATE
。 But $stmt->affected_rows
is returning 0
. 但是$stmt->affected_rows
返回0
。 Why is it returning zero? 为什么返回零?
function updateSignUp($status,$recordId){
$prepareStmt='UPDATE <DBNAME>.<TABLENAME> SET status=? WHERE id=?;';
$mysqli=$this->connectDB(<DBNAME>);#connectDB user written function
if ($stmt=$mysqli->prepare($prepareStmt)){
$stmt->bind_param('ii', $status, $recordId);
if (!$stmt->execute()) {
$stmt->close();
$mysqli->close();
return $this->errormsg('FAILURE!','Staff SignUp cannot Perform at this moment.');#errormsg user written function
}elseif($stmt->affected_rows>0){
$stmt->close();
$mysqli->close();
return $this->errormsg('SUCCESS!','Staff SignUp Done.',2);
}else{
$stmt->close();
$mysqli->close();
return $this->errormsg('WARNING!','Staff SignUp Not Done.',4);
}
}else{ return $this->errormsg('PREPARE FAILED:','(' . $mysqli->errno . ') ' . $mysqli->error); }
}
echo updateSignUp(0,15);
Result Displayed: WARNING! 结果显示:警告! Staff SignUp Not Done. 员工注册未完成。
Expecting Result: SUCCESS! 预期结果:成功! Staff SignUp Done. 员工注册完成。
Note: 注意:
Actual Table Looks Like: 实际表看起来像:
+------------------------+---------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+---------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| ldapusername | varchar(30) | YES | | NULL | |
| email | varchar(50) | NO | | NULL | |
| firstname | varchar(20) | YES | | NULL | |
| lastname | varchar(20) | YES | | NULL | |
| designation | varchar(30) | YES | | NULL | |
| username | varchar(30) | NO | | NULL | |
| role | int(5) | NO | | NULL | |
| manager | int(11) | NO | | NULL | |
| currency | int(11) | NO | | NULL | |
| country | int(11) | YES | | NULL | |
| payee_id | varchar(50) | YES | | NULL | |
| bank_acc_no | varchar(30) | YES | | NULL | |
| bank_name | varchar(50) | YES | | NULL | |
| bank_branch | varchar(50) | YES | | NULL | |
| bank_swift_code | varchar(30) | YES | | NULL | |
| ext_no | int(10) | YES | | NULL | |
| department | int(5) | NO | | NULL | |
| marital_status | int(5) | NO | | NULL | |
| monthly_limit | decimal(15,2) | YES | | NULL | |
| communication_limit | decimal(15,2) | YES | | NULL | |
| medical_per_bill | decimal(15,2) | YES | | NULL | |
| medical_per_annum | decimal(15,2) | YES | | NULL | |
| medical_cur_year_total | decimal(15,2) | YES | | NULL | |
| dental_per_bill | decimal(15,2) | YES | | NULL | |
| dental_per_annum | decimal(15,2) | YES | | NULL | |
| dental_cur_year_total | decimal(15,2) | YES | | NULL | |
| doj | date | YES | | NULL | |
| status | int(5) | NO | | 2 | |
| members_link_id | int(11) | YES | | NULL | |
| created_on | timestamp | NO | | CURRENT_TIMESTAMP | |
| modified_by | int(10) | YES | | NULL | |
| modified_on | timestamp | YES | | NULL | on update CURRENT_TIMESTAMP |
+------------------------+---------------+------+-----+-------------------+-----------------------------+
Did you check with a SELECT
statement if the row you want to update exists and has another value before the update than after it? 您是否用SELECT
语句检查了要更新的行是否存在,并且在更新之前和之后都有另一个值? Because when the value is the same before and after the update mysql will return zero affected rows. 因为当更新前后值相同时,mysql将返回零受影响的行。 See this similiar SO question . 看到这个类似的问题 。
edit: If you want to get all rows found (not only the real changed rows) you could try the connection flag FLAG_FOUND_ROWS
(see MYSQL Doc ). 编辑:如果要获取所有行(不仅是实际更改的行),可以尝试使用连接标志FLAG_FOUND_ROWS
(请参见MYSQL Doc )。
edit2: Okay new idea: The mistake maybe in the if/elseif/else statement that the affected rows is not read probably edit2:好的新想法:错误可能在if / elseif / else语句中,可能未读取受影响的行
Try: 尝试:
$success = $stmt->execute();
if(!success) {
$stmt->close();
$mysqli->close();
return $this->errormsg('FAILURE!','Staff SignUp cannot Perform at this moment.');#errormsg user written function
} else {
if($stmt->affected_rows>0) {
$stmt->close();
$mysqli->close();
return $this->errormsg('SUCCESS!','Staff SignUp Done.',2);
} else {
$stmt->close();
$mysqli->close();
return $this->errormsg('WARNING!','Staff SignUp Not Done.',4);
}
}
Thank you for all who tried to solve my issue. 感谢所有试图解决我的问题的人。 I was using jQuery
AJAX
and bootstrapvalidator
to render HTML form
and submit form using POST
. 我正在使用jQuery
AJAX
和bootstrapvalidator
来呈现HTML form
并使用POST
提交表单 。 This same issue simulated in another project and noticed that AJAX call triggered twice for single form submit . 在另一个项目中模拟了同一问题,并注意到AJAX调用针对单一表单提交触发了两次 。
Form Submit JS: 表单提交JS:
.on('success.form.bv', function(e){
e.preventDefault();
var $form = $("#signupapprovalform"), url = $form.attr('action');
$.post(url,$form.serialize()).done(function(dte){ $("#signup-content").html(dte); });
});
To Solve issue : I just added
e.stopImmediatePropagation();
解决问题 :我刚刚添加了e.stopImmediatePropagation();
next toe.preventDefault();
在e.preventDefault();
旁边e.preventDefault();
. 。
Actually no issue with PHP and mysql. 实际上,PHP和mysql没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.