我尝试为自己的MySQL数据库创建自定义API。我有两个表:
CREATE TABLE `heroes` (
`id` int(11) NOT NULL,
`name` varchar(200) NOT NULL,
`realname` varchar(200) NOT NULL,
`rating` int(11) NOT NULL,
`teamaffiliation` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `remarks` (
`id` int(11) NOT NULL,
`author` varchar(256) NOT NULL,
`comment` varchar(10000) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
id
和PRIMARY KEY
均为AUTO_INCREMENT
我的API执行基本的CRUD操作,读取工作正常,但是我在remarks
表上创建时遇到问题,但找不到错误
DbOperation.php:
function createHero($name, $realname, $rating, $teamaffiliation){
$stmt = $this->con->prepare("INSERT INTO heroes (name, realname, rating, teamaffiliation) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssis", $name, $realname, $rating, $teamaffiliation);
if($stmt->execute())
return true;
return false;
}
function createRemark($author, $comment){
$stmt = $this->con->prepare("INSERT INTO remarks (author, comment) VALUES (?, ?)");
$stmt->bind_param("ssis", $author, $comment);
if($stmt->execute())
return true;
return false;
}
function getHeroes(){
$stmt = $this->con->prepare("SELECT id, name, realname, rating, teamaffiliation FROM heroes");
$stmt->execute();
$stmt->bind_result($id, $name, $realname, $rating, $teamaffiliation);
$heroes = array();
while($stmt->fetch()){
$hero = array();
$hero['id'] = $id;
$hero['name'] = $name;
$hero['realname'] = $realname;
$hero['rating'] = $rating;
$hero['teamaffiliation'] = $teamaffiliation;
array_push($heroes, $hero);
}
return $heroes;
}
function getRemarks(){
$stmt = $this->con->prepare("SELECT id, author, comment FROM remarks");
$stmt->execute();
$stmt->bind_result($id, $author, $comment);
$remarks = array();
while($stmt->fetch()){
$remark = array();
$remark['id'] = $id;
$remark['author'] = $author;
$remark['comment'] = $comment;
array_push($remarks, $remark);
}
return $remarks;
}
Api.php:
case 'createhero':
isTheseParametersAvailable(array('name','realname','rating','teamaffiliation'));
$db = new DbOperation();
$result = $db->createHero(
$_POST['name'],
$_POST['realname'],
$_POST['rating'],
$_POST['teamaffiliation']
);
if($result){
$response['error'] = false;
$response['message'] = 'Hero addedd successfully';
$response['heroes'] = $db->getHeroes();
}else{
$response['error'] = true;
$response['message'] = 'Some error occurred please try again';
}
break;
case 'createremark':
isTheseParametersAvailable(array('author','comment'));
$db = new DbOperation();
$result = $db->createRemark(
$_POST['author'],
$_POST['comment']
);
if($result){
$response['error'] = false;
$response['message'] = 'Remark added successfully';
$response['remarks'] = $db->getRemarks();
}else{
$response['error'] = true;
//and we have the error message
$response['message'] = 'Some error occured, please try again';
}
break;
case 'getheroes':
$db = new DbOperation();
$response['error'] = false;
$response['message'] = 'Request successfully completed';
$response['heroes'] = $db->getHeroes();
break;
case 'getremarks':
$db = new DbOperation();
$response['error'] = false;
$response['message'] = 'Request successfully completed';
$response['remarks'] = $db->getRemarks();
break;
唯一不起作用的部分是创建备注的部分...
它向我显示:{"error":true,"message":"Some error occured, please try again"}
我没有发现什么问题...