gpt4 book ai didi

php - 在 PHP 中为 MVC Controller 合并 INSERT Mysql 查询

转载 作者:行者123 更新时间:2023-11-28 23:52:36 25 4
gpt4 key购买 nike

所以我已经坚持了很长一段时间,令人惊讶的是更新和删除功能工作得很好,但是我无法使 CREATE 功能正常工作。请看看它并告诉我我做错了什么

    <-------------- Entire model for admin panel-------------->>>>>>>> Connection to DB is working fine---------->>>>>>>>>>>

<?php
include_once "Model.php";

class ModelPages extends Model {

public function get($key) {

$sql = "SELECT * from pages where page_key = '$key'";
$row = '';

$page = Null;

foreach ($this->pdo->query($sql) as $row) {
$page = $row;
}

// echo "<pre>";
// var_dump($page);
// exit;
return $page;


}


public function getAll() {

$statement = $this->pdo->prepare("SELECT * from pages Where Id > 3");
$result = $statement->execute();
$pages = array();
if($result) {
$pages = $statement->fetchAll(PDO::FETCH_ASSOC);
}
return $pages;
}

public function updatePage($params=array()) {
if (!is_array($params)) {
return 'Params should be an array';
}

if (isset($params['table'])) {
$tableName = $params['table'];
} else {
$tableName = 'pages';
}

$pageId = isset($params['page_key']) ? $params['page_key'] : null;
$pageTitle = isset($params['page_title']) ? $params['page_title'] : null;
$pageBody = isset($params['page_body']) ? $params['page_body'] : null;

if ($pageId == null) {
return 'No page id provided';
}

$sql = "UPDATE " . $tableName . " SET
title = :title,
body = :body
WHERE page_key = :page_key";

$statement = $this->pdo->prepare($sql);

$statement->bindParam(':title', $pageTitle, PDO::PARAM_STR);
$statement->bindParam(':body', $pageBody, PDO::PARAM_STR);
$statement->bindParam(':page_key', $pageId, PDO::PARAM_INT);

$result = $statement->execute();

return $result;
}

public function deletePage($pageId) {
// build sql
$sql = "DELETE FROM pages WHERE id = " . intval($pageId);

$statement = $this->pdo->prepare($sql);
$result = $statement->execute();
return $result;
}

public function createPage($params=array()){
if (!is_array($params)) {
return 'Params should be an array';
}

if (isset($params['table'])) {
$tableName = $params['table'];
} else {
$tableName = 'pages';
}

$page_key = isset($params['page_key']) ? $params['page_key'] : 'page_key';
$pageTitle = isset($params['page_title']) ? $params['page_title'] : 'page_title';
$pageBody = isset($params['page_body']) ? $params['page_body'] : 'page_body';



$sql = "INSERT INTO " . $tablename ." SET page_key=:page_key, title=:title, body=:body ";

// prepare query for execution
$statement = $this->pdo->prepare($sql);

// bind the parameters
$statement->bindParam(':page_key', $_POST['page_key']);
$statement->bindParam(':title', $_POST['title']);
$statement->bindParam(':body', $_POST['body']);

// specify when this record was inserted to the database


// Execute the query
$result = $statement->execute();
return $result;
}
}



<?php

include 'controllers/controller.php';
include 'models/Model.php';
include 'models/ModelPages.php';


<------------------------ADMIN CONTROller----------------------->>>>>>>>>>>>


class Admin extends Controller {
function __construct() {

// create an instance of ModelPages
$ModelPages = new ModelPages();

if(isset($_POST['page_key'])) {
// TODO: update DB
$tableData['page_body'] = $_POST['body'];
$tableData['table'] = 'pages';
$tableData['page_title'] = $_POST['title'];
$tableData['page_key'] = $_POST['page_key'];


$response = $ModelPages->updatePage($tableData);

if ($response == TRUE) {
header("http://188.166.96.184/workspace/marem/AAAAA/index.php?page=admin&success=true");
}

}

if(isset($_GET['page_key'])) {
// by default we assume that the key_page exists in db
$error = false;
$page = $ModelPages->get($_REQUEST['page_key']);

// if page key does not exist set error to true
if($page === null) {
$error = true;
}

// prepare data for the template
$data = $page;
$data["error"] = $error;

// display
echo $this->render2(array(), 'header.php');
echo $this->render2(array(), 'navbar_admin.php');
echo $this->render2($data, 'admin_update_page.php');
echo $this->render2(array(), 'footer.php');

} else {
// case: delete_page
if(isset($_GET['delete_page'])) {
$response = $ModelPages->deletePage($_GET['delete_page']);
if($response == TRUE) {
header("http://188.166.96.184/workspace/marem/AAAAA/index.php?page=admin&deleted=true");
}

}
}

//Get table name and make connection
if(isset($_POST['submit'])) {

$page_key = $_POST['page_key'];
$page_title = $_POST['title'];
$page_body = $_POST['body'];

$response = $ModelPages->createPage();


if($response=TRUE){
header("http://188.166.96.184/workspace/marem/AAAAA/index.php?page=admin&created=true");

}



}
}


// load all pages from DB
$pages = $ModelPages -> getAll();

// display
echo $this->render2(array(), 'header_admin.php');
echo $this->render2(array(), 'navbar_admin.php');
echo $this->render2(array("pages"=> $pages), 'admin_view.php');
echo $this->render2(array(), 'footer.php');


}

}
?>

最佳答案

因为顶部有 if(isset($_POST['page_key']):

class Admin extends Controller {
function __construct() {
// create an instance of ModelPages
$ModelPages = new ModelPages();
if(isset($_POST['page_key'])) {
...
if ($response == TRUE) {
header("http://188.166.96.184/workspace/marem/AAAAA/index.php?
}

用于调用$response = $ModelPages->updatePage($tableData);

您的代码永远不会到达底部具有良好值的部分:

 if(!isset($_POST['page_key'])) {
...
$response = $ModelPages->createPage($tableData);

所以我的简单但不是最好的建议是在 POST 时使用额外的参数,比如 action。所以你可以检查:

 if(isset($_POST['action']) && $_POST['action']=='update') {
...
} elseif (isset($_POST['action']) && $_POST['action']=='create') {
...
} etc...

希望这对你现在有帮助 :-)

关于php - 在 PHP 中为 MVC Controller 合并 INSERT Mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32398617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com