类型'String'是一个意外的参数,期望Mysqli [mysqli_real_escape_string] [重复]
问题描述:
This question already has an answer here:
I'm trying to persist an angularjs element in my database.
I'm using mySQL and PHP.
When I try to persist, I obtain this error: Warning: 'dabdsa' of type 'String' is an unexpected argument, expected Mysqli [mysqli_real_escape_string]
Warning: function 'mysqli_real_escape_string' has 2 required arguments, but only 1 were provided [mysqli_real_escape_string]
I dont know what is happening.
My app.js code is:
var app = angular.module("TestIdoneidadApp", []);
app.controller("TIController", ['$scope','$http', function($scope, $http) {
$scope.gestor= '';
$scope.entidad= '';
$scope.save=function(){
$scope.xml_object = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><testIdoneidad>';
$scope.xml_object += '<gestor>' + $scope.ti.gestor + '</gestor>';
$scope.xml_object += '<entidad>' + $scope.ti.entidad + '</entidad>';
$scope.xml_object += '</testIdoneidad>';
$http.post("insert.php", {'xml_object':$scope.xml_object});
}
}]);
My insert.php code is:
<?php header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Authorization, Content-Type');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE');
$servername = "localhost";
$username = "msandbox";
$password = "msandbox";
$dbname = "angular_db";
$port = "5631";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname, $port);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$data = json_decode(file_get_contents("php://input"));
$xml_object = mysqli_real_escape_string($data->xml_object);
$sql = "INSERT INTO test_idoneidad (xml_object)
VALUES ('".$xml_object."')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
The connection with database is correct, and if i try to insert something like "test" in the Value, this works.
Can someone help me?
Thank you in advice.
</div>
答
Change your code to
$xml_object = mysqli_real_escape_string($conn, $data->xml_object);