mysqli affected rows not working with INSERT
Hi, I have an app that access a MySQL database via a php script.
I noticed that INSERT was returning error but the data was being recorded successfully.
I finally traced the problem to be related with the line
echo $conn->affected_rows;
For some reason when it is an SQL INSERT it returns -11 but as I said the INSERTS executes successfully.
**The app requesting the service sends in sequence:**
char* txtSQL[]={"INSERT INTO activity (mac,jd,date,time,area,type,value) VALUES ('a9c4952de6b4',2458454,'2018-12-01','10:22','Area0002','h',130)",
"INSERT INTO activity (mac,jd,date,time,area,type,value) VALUES ('a9c4952de6b4',2458454,'2018-12-01','10:22','Area0002','h',130)",
"INSERT INTO activity (mac,jd,date,time,area,type,value) VALUES ('a9c4952de6b4',2458454,'2018-12-01','10:22','Area0002','h',130)",
"INSERT INTO activity (mac,jd,date,time,area,type,value) VALUES ('a9c4952de6b4',2458454,'2018-12-01','10:22','Area0002','h',130)",
"UPDATE activity set value=333 where value=130",
"SELECT sum(value) from activity where mac='a9c4952de6b4'",
"DELETE from activity WHERE value=333"};
**I set a monitor to check what was being returned and got:**
query=INSERT INTO activity (mac,jd,date,time,area,type,value) VALUES ('a9c4952de6b4',2458454,'2018-12-01','10:22','Area0002','h',130)
ttpResponse: - httpResponseCode: -11
query=INSERT INTO activity (mac,jd,date,time,area,type,value) VALUES ('a9c4952de6b4',2458454,'2018-12-01','10:22','Area0002','h',130)
HttpResponse: - httpResponseCode: -11
query=INSERT INTO activity (mac,jd,date,time,area,type,value) VALUES ('a9c4952de6b4',2458454,'2018-12-01','10:22','Area0002','h',130)
HttpResponse: - httpResponseCode: -11
query=INSERT INTO activity (mac,jd,date,time,area,type,value) VALUES ('a9c4952de6b4',2458454,'2018-12-01','10:22','Area0002','h',130)
HttpResponse: - httpResponseCode: -11
query=UPDATE activity set value=333 where value=130
HttpResponse: 4 * httpResponseCode: 201 ** -------------------------------------->As can be seem the INSERTS above returned error but worked OK
q**uery=SELECT sum(value) from activity where mac='a9c4952de6b4'
HttpResponse: 1379 * httpResponseCode: 200
query=DELETE from activity WHERE value=333
HttpResponse: 4 * httpResponseCode: 201
**The PHP script doing the job is as below:**
<?php
include('connection.php');
//these are just in case setting headers forcing it to always expire
header('Cache-Control: no-cache, must-revalidate');
error_log(print_r($_POST,TRUE));
if( isset($_POST['query']) && isset($_POST['key']) ){ //checks if the tag post is there and if its been a proper form post
header('Content-type: application/x-www-form-urlencoded');
if($_POST['key']==$SQLKEY){ //validates the SQL key
$query=urldecode($_POST['query']);
if(get_magic_quotes_gpc()){ //check if the worthless pile of crap magic quotes is enabled and if it is, strip the slashes from the query
$query=stripslashes($query);
}
$conn = new mysqli($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME); //connect
if($conn->connect_error){ //checks connection
header("HTTP/1.0 400 Bad Request");
echo "ERROR Database Connection Failed: " . $conn->connect_error, E_USER_ERROR; //reports a DB connection failure
} else {
$result=$conn->query($query); //runs the posted query
if($result === false){
header("HTTP/1.0 400 Bad Request"); //sends back a bad request error
echo "Wrong SQL: " . $query . " Error: " . $conn->error, E_USER_ERROR; //errors if the query is bad and spits the error back to the client
} else {
if (strlen(stristr($query,"SELECT"))>0) { //tests if it's a SELECT statement
$csv = ''; // bug fix Undefined variable: csv
while ($fieldinfo = $result->fetch_field()) {
$csv .= $fieldinfo->name.",";
}
$csv = rtrim($csv, ",")."\n";
//******************************** echo $csv; //prints header row
$csv = '';
$result->data_seek(0);
while($row = $result->fetch_assoc()){
foreach ($row as $key => $value) {
$csv .= $value.",";
}
$csv = rtrim($csv, ","); //."\n";
}
echo $csv; //prints all data rows
} else {
header("HTTP/1.0 201 Rows");
* echo $conn->affected_rows; //if the query is anything but a SELECT, it will return the number of affected rows (**INSERT IS RETURNING -11)**
* }
}
$conn->close(); //closes the DB
}
} else {
header("HTTP/1.0 400 Bad Request");
echo "-Bad Request"; //reports if the secret key was bad
}
} else {
header("HTTP/1.0 400 Bad Request");
echo "*Bad Request";
}
?>
Any help will be much appreciated.
Thanks
Paulo BorgesDiscussão (0)
Carregando comentários...