需要有关处理 PHP 文件中的操作 "read" 以处理 CRUD 操作的建议
Need advice on handling action "read" in PHP file for handling CRUD operations
我正在尝试设置一个 PHP 文件来处理特定类型数据的 crud 操作 - "land contracts"。当前有 4 种情况我想从我的数据库中 read:
- 当设置
acion="read"
和land_contract_id
时,则回显土地合同
- 当
acion="create"
时,然后在创建土地合同后回显它
- 当
acion="update"
时,更新土地合同后回显
- 当
acion="read"
和land_contract_id
未设置时,则echo all land contract
我当前的设置适用于情况 1、2 和 3。但是,我无法在没有参数的情况下调用我的读取函数。即在情况 4 中,当 $land_contract_id 为 not 时。
当我想从我的数据库中读取时,我应该如何调整我的设置以在所有情况下工作?
下面是我设置的简化版本。
<?php
$action = isset($_POST["action"]) ? $_POST["action"] : '';
$land_contract_id = isset($_POST["land_contract_id"]) ? $_POST["land_contract_id"] : '';
switch ($action) {
case "create":
// Here I'm creating a new land contract and getting the last inserted id with $pdo->lastInsertId()
read($last_inserted_land_contract_id);
break;
case "read":
if (isset($_POST['land_contract_id'])) {
read($land_contract_id);
}
break;
case "update":
// Here I'm updating an existing land contract
read($land_contract_id);
break;
case "delete":
break;
}
function read(land_contract_id) {
// Fetching land contract from database and echoing as JSON
}
?>
--更新--
完整代码如下。根据@Jeff 的回答更新。函数 read 中的行 $stmt = $pdo->prepare($sql);
给了我 Fatal error: Uncaught Error: Call to a member function prepare() on null
.
<?php
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Origin: *");
require_once("./pdo_connect.php");
$action = isset($_POST["action"]) ? $_POST["action"] : '';
$land_contract_id = isset($_POST["land_contract_id"]) ? $_POST["land_contract_id"] : '';
$land_contract_name = isset($_POST["land_contract_name"]) ? $_POST["land_contract_name"] : '';
$location_id = isset($_POST["location_id"]) ? $_POST["location_id"] : '';
$land_contract_link = isset($_POST["land_contract_link"]) ? $_POST["land_contract_link"] : '';
$land_contract_notes = isset($_POST["land_contract_notes"]) ? $_POST["land_contract_notes"] : '';
$land_owner_id = isset($_POST["land_owner_id"]) ? $_POST["land_owner_id"] : '';
$land_contract_start_date = isset($_POST["land_contract_start_date"]) ? $_POST["land_contract_start_date"] : '';
$land_contract_end_date = isset($_POST["land_contract_end_date"]) ? $_POST["land_contract_end_date"] : '';
$land_contract_terminated = isset($_POST["land_contract_terminated"]) ? $_POST["land_contract_terminated"] : 'false';
$land_contract_payment_interval = isset($_POST["land_contract_payment_interval"]) ? $_POST["land_contract_payment_interval"] : '';
$land_contract_price_type = isset($_POST["land_contract_price_type"]) ? $_POST["land_contract_price_type"] : '';
$land_contract_fixed_annual_price = isset($_POST["land_contract_fixed_annual_price"]) ? $_POST["land_contract_fixed_annual_price"] : '';
$land_contract_variable_annual_price_year = isset($_POST["land_contract_variable_annual_price_year"]) ? $_POST["land_contract_variable_annual_price_year"] : '';
switch ($action) {
case "create":
// Insert land_contract to database
$stmt1 = $pdo->prepare("INSERT INTO land_contract (land_contract_name, location_id, land_contract_link, land_contract_notes, land_owner_id, land_contract_start_date, land_contract_end_date, land_contract_terminated, land_contract_payment_interval, land_contract_price_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt1->execute([$land_contract_name, $location_id, $land_contract_link, $land_contract_notes, $land_owner_id, $land_contract_start_date, $land_contract_end_date, $land_contract_terminated, $land_contract_payment_interval, $land_contract_price_type]);
// Get id of last inserted land_contract
$last_inserted_land_contract_id = $pdo->lastInsertId();
// Check if there are any variable annual prices and if so, insert them into land_contract_annual_price
if ( isset( $_POST['land_contract_variable_annual_price_year'] ) ) {
$years = $_POST ['land_contract_variable_annual_price_year'] ;
if( is_array( $years ) ) {
foreach ( $years as $year => $price ) {
$price_with_decimals = number_format((float)$price, 2, '.', '');
// Insert the variable annual prices into land_contract_annual_price
$stmt2 = $pdo->prepare("INSERT INTO land_contract_annual_price (land_contract_annual_price_year, land_contract_annual_price_amount, land_contract_id) VALUES (?, ?, ?)");
$stmt2->execute([$year, $price_with_decimals, $last_inserted_land_contract_id]);
}
}
}
read($last_inserted_land_contract_id);
// Reset statements
$stmt1 = null;
$stmt2 = null;
break;
case "read":
if ( isset($_POST['land_contract_id']) ) {
read($land_contract_id);
}
else {
read();
}
break;
case "update":
$row = [
'land_contract_id' => $land_contract_id,
'land_contract_name' => $land_contract_name,
'location_id' => $location_id,
'land_contract_link' => $land_contract_link,
'land_contract_notes' => $land_contract_notes,
'land_owner_id' => $land_owner_id,
'land_contract_start_date' => $land_contract_start_date,
'land_contract_end_date' => $land_contract_end_date,
'land_contract_terminated' => $land_contract_terminated,
'land_contract_payment_interval' => $land_contract_payment_interval,
'land_contract_price_type' => $land_contract_price_type,
'land_contract_fixed_annual_price' => $land_contract_fixed_annual_price,
'land_contract_variable_annual_price_year' => $land_contract_variable_annual_price_year
];
$stmt = $pdo->prepare("UPDATE land_contract SET land_contract_name=:land_contract_name, location_id=:location_id, land_contract_link=:land_contract_link, land_contract_notes=:land_contract_notes, land_owner_id=:land_owner_id, land_contract_start_date=:land_contract_start_date, land_contract_end_date=:land_contract_end_date, land_contract_terminated=:land_contract_terminated, land_contract_payment_interval=:land_contract_payment_interval, land_contract_price_type=:land_contract_price_type, land_contract_fixed_annual_price=:land_contract_fixed_annual_price, land_contract_variable_annual_price_year=:land_contract_variable_annual_price_year WHERE land_contract_id=:land_contract_id");
$stmt->execute($row);
read($land_contract_id);
$stmt = null;
break;
case "delete":
$stmt = $pdo->prepare("DELETE FROM land_contract WHERE land_contract_id = ?");
$stmt->execute([$land_contract_id]);
$stmt = null;
break;
}
function read($land_contract_id = null) {
$params = [];
$array = [];
$sql = "SELECT lc.*,
py.land_contract_annual_price_year AS `year`,
py.land_contract_annual_price_amount AS `amount`
FROM land_contract AS lc
LEFT JOIN land_contract_annual_price AS py
ON py.land_contract_id = lc.land_contract_id
";
if ($land_contract_id) {
$sql .= 'WHERE lc.land_contract_id = ?';
$params[] = $land_contract_id;
}
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
while ($row = $stmt->fetch()) {
// Fields we want to extract from the select statement into the array
$select_fields = ['land_contract_id', 'land_contract_name', 'location_id', 'land_contract_link', 'land_contract_notes', 'land_owner_id',
'land_contract_start_date', 'land_contract_end_date', 'land_contract_terminated', 'land_contract_payment_interval',
'land_contract_price_type', 'land_contract_fixed_annual_price '];
if (!isset($array[$row['land_contract_id']])) {
// initialize the subarray if it has not been set already
$array[$row['land_contract_id']] = array_intersect_key($row, array_flip($select_fields));
if ($row['year'] != null) {
$array[$row['land_contract_id']]['land_contract_annual_prices'] = [];
} else {
$array[$row['land_contract_id']]['land_contract_annual_price'] = $row['land_contract_fixed_annual_price'];
}
}
if ($row['year'] != null) {
$array[$row['land_contract_id']]['land_contract_annual_prices'][] = ['year' => $row['year'], 'amount' => $row['amount']];
}
}
if (empty($array)) {
echo "No results";
exit;
}
echo json_encode($array, JSON_UNESCAPED_UNICODE);
$stmt = null;
}
?>
您需要修改 read()
函数以使 $land_contract_id
可选:
function read($land_contract_id = null) {
$sql = "SELECT * FROM land_contracts";
if($land_contract_id) {
$sql .= " WHERE land_contract_id = {$land_contract_id}";
}
$result = $database->select_rows($sql);
}
现在,$land_contract_id
是可选的。如果您将值传递给 read()
,它将仅 return 选定的合同,否则如果您将其保留 null/empty,则它将 return 所有合同。
我正在尝试设置一个 PHP 文件来处理特定类型数据的 crud 操作 - "land contracts"。当前有 4 种情况我想从我的数据库中 read:
- 当设置
acion="read"
和land_contract_id
时,则回显土地合同 - 当
acion="create"
时,然后在创建土地合同后回显它 - 当
acion="update"
时,更新土地合同后回显 - 当
acion="read"
和land_contract_id
未设置时,则echo all land contract
我当前的设置适用于情况 1、2 和 3。但是,我无法在没有参数的情况下调用我的读取函数。即在情况 4 中,当 $land_contract_id 为 not 时。
当我想从我的数据库中读取时,我应该如何调整我的设置以在所有情况下工作?
下面是我设置的简化版本。
<?php
$action = isset($_POST["action"]) ? $_POST["action"] : '';
$land_contract_id = isset($_POST["land_contract_id"]) ? $_POST["land_contract_id"] : '';
switch ($action) {
case "create":
// Here I'm creating a new land contract and getting the last inserted id with $pdo->lastInsertId()
read($last_inserted_land_contract_id);
break;
case "read":
if (isset($_POST['land_contract_id'])) {
read($land_contract_id);
}
break;
case "update":
// Here I'm updating an existing land contract
read($land_contract_id);
break;
case "delete":
break;
}
function read(land_contract_id) {
// Fetching land contract from database and echoing as JSON
}
?>
--更新--
完整代码如下。根据@Jeff 的回答更新。函数 read 中的行 $stmt = $pdo->prepare($sql);
给了我 Fatal error: Uncaught Error: Call to a member function prepare() on null
.
<?php
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Origin: *");
require_once("./pdo_connect.php");
$action = isset($_POST["action"]) ? $_POST["action"] : '';
$land_contract_id = isset($_POST["land_contract_id"]) ? $_POST["land_contract_id"] : '';
$land_contract_name = isset($_POST["land_contract_name"]) ? $_POST["land_contract_name"] : '';
$location_id = isset($_POST["location_id"]) ? $_POST["location_id"] : '';
$land_contract_link = isset($_POST["land_contract_link"]) ? $_POST["land_contract_link"] : '';
$land_contract_notes = isset($_POST["land_contract_notes"]) ? $_POST["land_contract_notes"] : '';
$land_owner_id = isset($_POST["land_owner_id"]) ? $_POST["land_owner_id"] : '';
$land_contract_start_date = isset($_POST["land_contract_start_date"]) ? $_POST["land_contract_start_date"] : '';
$land_contract_end_date = isset($_POST["land_contract_end_date"]) ? $_POST["land_contract_end_date"] : '';
$land_contract_terminated = isset($_POST["land_contract_terminated"]) ? $_POST["land_contract_terminated"] : 'false';
$land_contract_payment_interval = isset($_POST["land_contract_payment_interval"]) ? $_POST["land_contract_payment_interval"] : '';
$land_contract_price_type = isset($_POST["land_contract_price_type"]) ? $_POST["land_contract_price_type"] : '';
$land_contract_fixed_annual_price = isset($_POST["land_contract_fixed_annual_price"]) ? $_POST["land_contract_fixed_annual_price"] : '';
$land_contract_variable_annual_price_year = isset($_POST["land_contract_variable_annual_price_year"]) ? $_POST["land_contract_variable_annual_price_year"] : '';
switch ($action) {
case "create":
// Insert land_contract to database
$stmt1 = $pdo->prepare("INSERT INTO land_contract (land_contract_name, location_id, land_contract_link, land_contract_notes, land_owner_id, land_contract_start_date, land_contract_end_date, land_contract_terminated, land_contract_payment_interval, land_contract_price_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt1->execute([$land_contract_name, $location_id, $land_contract_link, $land_contract_notes, $land_owner_id, $land_contract_start_date, $land_contract_end_date, $land_contract_terminated, $land_contract_payment_interval, $land_contract_price_type]);
// Get id of last inserted land_contract
$last_inserted_land_contract_id = $pdo->lastInsertId();
// Check if there are any variable annual prices and if so, insert them into land_contract_annual_price
if ( isset( $_POST['land_contract_variable_annual_price_year'] ) ) {
$years = $_POST ['land_contract_variable_annual_price_year'] ;
if( is_array( $years ) ) {
foreach ( $years as $year => $price ) {
$price_with_decimals = number_format((float)$price, 2, '.', '');
// Insert the variable annual prices into land_contract_annual_price
$stmt2 = $pdo->prepare("INSERT INTO land_contract_annual_price (land_contract_annual_price_year, land_contract_annual_price_amount, land_contract_id) VALUES (?, ?, ?)");
$stmt2->execute([$year, $price_with_decimals, $last_inserted_land_contract_id]);
}
}
}
read($last_inserted_land_contract_id);
// Reset statements
$stmt1 = null;
$stmt2 = null;
break;
case "read":
if ( isset($_POST['land_contract_id']) ) {
read($land_contract_id);
}
else {
read();
}
break;
case "update":
$row = [
'land_contract_id' => $land_contract_id,
'land_contract_name' => $land_contract_name,
'location_id' => $location_id,
'land_contract_link' => $land_contract_link,
'land_contract_notes' => $land_contract_notes,
'land_owner_id' => $land_owner_id,
'land_contract_start_date' => $land_contract_start_date,
'land_contract_end_date' => $land_contract_end_date,
'land_contract_terminated' => $land_contract_terminated,
'land_contract_payment_interval' => $land_contract_payment_interval,
'land_contract_price_type' => $land_contract_price_type,
'land_contract_fixed_annual_price' => $land_contract_fixed_annual_price,
'land_contract_variable_annual_price_year' => $land_contract_variable_annual_price_year
];
$stmt = $pdo->prepare("UPDATE land_contract SET land_contract_name=:land_contract_name, location_id=:location_id, land_contract_link=:land_contract_link, land_contract_notes=:land_contract_notes, land_owner_id=:land_owner_id, land_contract_start_date=:land_contract_start_date, land_contract_end_date=:land_contract_end_date, land_contract_terminated=:land_contract_terminated, land_contract_payment_interval=:land_contract_payment_interval, land_contract_price_type=:land_contract_price_type, land_contract_fixed_annual_price=:land_contract_fixed_annual_price, land_contract_variable_annual_price_year=:land_contract_variable_annual_price_year WHERE land_contract_id=:land_contract_id");
$stmt->execute($row);
read($land_contract_id);
$stmt = null;
break;
case "delete":
$stmt = $pdo->prepare("DELETE FROM land_contract WHERE land_contract_id = ?");
$stmt->execute([$land_contract_id]);
$stmt = null;
break;
}
function read($land_contract_id = null) {
$params = [];
$array = [];
$sql = "SELECT lc.*,
py.land_contract_annual_price_year AS `year`,
py.land_contract_annual_price_amount AS `amount`
FROM land_contract AS lc
LEFT JOIN land_contract_annual_price AS py
ON py.land_contract_id = lc.land_contract_id
";
if ($land_contract_id) {
$sql .= 'WHERE lc.land_contract_id = ?';
$params[] = $land_contract_id;
}
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
while ($row = $stmt->fetch()) {
// Fields we want to extract from the select statement into the array
$select_fields = ['land_contract_id', 'land_contract_name', 'location_id', 'land_contract_link', 'land_contract_notes', 'land_owner_id',
'land_contract_start_date', 'land_contract_end_date', 'land_contract_terminated', 'land_contract_payment_interval',
'land_contract_price_type', 'land_contract_fixed_annual_price '];
if (!isset($array[$row['land_contract_id']])) {
// initialize the subarray if it has not been set already
$array[$row['land_contract_id']] = array_intersect_key($row, array_flip($select_fields));
if ($row['year'] != null) {
$array[$row['land_contract_id']]['land_contract_annual_prices'] = [];
} else {
$array[$row['land_contract_id']]['land_contract_annual_price'] = $row['land_contract_fixed_annual_price'];
}
}
if ($row['year'] != null) {
$array[$row['land_contract_id']]['land_contract_annual_prices'][] = ['year' => $row['year'], 'amount' => $row['amount']];
}
}
if (empty($array)) {
echo "No results";
exit;
}
echo json_encode($array, JSON_UNESCAPED_UNICODE);
$stmt = null;
}
?>
您需要修改 read()
函数以使 $land_contract_id
可选:
function read($land_contract_id = null) {
$sql = "SELECT * FROM land_contracts";
if($land_contract_id) {
$sql .= " WHERE land_contract_id = {$land_contract_id}";
}
$result = $database->select_rows($sql);
}
现在,$land_contract_id
是可选的。如果您将值传递给 read()
,它将仅 return 选定的合同,否则如果您将其保留 null/empty,则它将 return 所有合同。