执行INSERT语句后,没有引发任何错误,并且我的数据库表为空,请有人帮助!
用于调用INSERT语句的服务文件
roomservice.php
<?php
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST');
if(isset($_POST['hotelName']) && isset($_POST['hasTV']) &&
isset($_POST['beds']) && isset($_POST['price'])){
$roomName = $_POST['hotelName'];
$hasTV = $_POST['hasTV'];
$beds = $_POST['beds'];
$price = $_POST['price'];
echo addRoom($roomName,$hasTV,$beds,$price);
}
function addRoom($hotelName, $hasTV, $beds , $price){
global $conn;
$rarray = array();
$stmt = $conn->prepare("INSERT INTO rooms (hotelname, hastv, beds, price) VALUES (?, ?, ?, ?)");
$stmt->bind_param("sss", $hotelName, $hasTV, $beds , $price);
if($stmt->execute()){
$rarray['sucess'] = "okej";
}else{
$rarray['error'] = "Database connection error";
}
return json_encode($rarray);
}
?>
角度组件文件,其中我将服务文件与通过表单传递的数据一起调用。
addroom.component.ts
public addRoom() {
const body = new HttpParams()
.set('hotelname', this.roomForm.value.hotelName)
.set('hastv', this.roomForm.value.hasTV)
.set('beds', this.roomForm.value.beds)
.set('price', this.roomForm.value.price);
console.log(body.toString());
this._api.post('roomservice.php', body.toString()).subscribe((data: any) => {
console.log(data);
// localStorage.setItem('token', JSON.parse(data['_body']).token);
this._router.navigate(['./']);
}, (error) => {
const obj = JSON.parse(error['_body']).error;
const element = <HTMLElement> document.getElementsByClassName('alert')[0];
element.style.display = 'block';
element.innerHTML = obj.split('\\r\\n').join('<br/>').split('\"').join('');
});
}
}
房间表
客户回应
答案 0 :(得分:-2)
您试图将4个参数传递给bind函数,但是您仅输入了3个参数。这就是为什么它不起作用。
解决方案是更改这两行:
$stmt = $conn->prepare("INSERT INTO rooms (hotelname, hastv, beds, price) VALUES (?, ?, ?, ?)");
$stmt->bind_param("sss", $hotelName, $hasTV, $beds , $price);
收件人:
$stmt = $conn->prepare("INSERT INTO `rooms` (`hotelname`, `hastv`, `beds`, `price`) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $hotelName, $hasTV, $beds , $price);