gpt4 book ai didi

php - PDO:执行数据库更新时 Json 不呈现响应

转载 作者:行者123 更新时间:2023-11-29 09:55:04 24 4
gpt4 key购买 nike

我想在他查看页面时在数据库字段中注册当前登录的用户名。数据库是MySQL,语言是PHP(XAMPP本地服务器)

这是我正在使用的代码:

$ActiveUser = USER_NAME;
$id = $rec_id;
$db->rawQuery("UPDATE postagens SET post_user_posted='$ActiveUser' WHERE
post_id='$id'");

当我通过网络浏览器打开页面时,数据库已成功更新。但是,虽然数据库已正确更新,但页面返回以下错误消息:

!Error Processing Request.

此时,页面应该获取数据库信息并显示。但每当出现上述错误时,页面就不会从数据库加载信息。页面仅显示错误消息,字段为空。

我认为 json 渲染一定发生了一些事情。如果我删除 UPDATE 查询代码,则不会显示错误,并且数据库信息会正确显示。同时删除 json 返回的代码:

{"post_id":"1","post_title":"My first post.","post_text":"Lorem ipsum
dollor.","post_date":"2018-12-31
12:00:00","post_user_posted":"Welisson"}

当我运行更新查询时,json 没有返回(或返回空)

错误日志显示:

PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000]: General 
error
in C:\xampp\htdocs\projetosimples\app\models\PDODb.php:680
Stack trace:
0 C:\xampp\htdocs\projetosimples\app\models\PDODb.php(680):
PDOStatement-
>fetchAll(2)
1 C:\xampp\htdocs\projetosimples\app\models\PDODb.php(1611): PDODb-
>buildResult(Object(PDOStatement)) 2 C:\xampp\htdocs\projetosimples\app\controllers\PostagensController.php(93): PDODb->rawQuery('UPDATE postagen...')
3 C:\xampp\htdocs\projetosimples\system\Router.php(227): PostagensController->view('1')
4 C:\xampp\htdocs\projetosimples\system\Router.php(92): Router->run('postagens/view/...')
5 C:\xampp\htdocs\projetosimples\index.php(84): Router->init()
6 {main}
thrown in C:\xampp\htdocs\projetosimples\app\models\PDODb.php on line 680

PDODb.php 第 680 行左右:

/**
* Return query result
*
* @param PDOStatement $stmt
* @return array
*/
private function buildResult($stmt)
{
if ($this->useGenerator) {
return $this->buildResultGenerator($stmt);
} else {
return $stmt->fetchAll($this->returnType); //THIS IS THE LINE 680
}
}

PDODb.php 1611 左右的行:

/**
* Perform db query
*
* @param string $query
* @param array $params
* @return array
*/
public function rawQuery($query, $params = null)
{
$this->query = $query;
if (is_array($params)) {
$this->params = $params;
}
$stmt = $this->prepare();
if ($stmt) {
$stmt->execute();
$this->lastError = $stmt->errorInfo();
$this->lastErrorCode = $stmt->errorCode();
$result = $this->buildResult($stmt); //LINE 1611
} else {
$result = null;
}
$this->reset();
return $result;
}

PostagensController.php 第 93 行:

 function view( $rec_id = null , $value = null){
$db = $this->GetModel();
$fields = array( 'post_id', 'post_title', 'post_text', 'post_date', 'post_user_posted' );
if( !empty($value) ){
$db->where($rec_id, urldecode($value));
}
else{
$db->where('post_id' , $rec_id);
}
$record = $db->getOne( 'postagens', $fields );
if(!empty($record)){
$ActiveUser = ucwords(USER_NAME);
$id = $rec_id;
render_json($record); //THIS IS THE LINE 93
}
else{
if($db->getLastError()){
render_error($db->getLastError());
}
else{
render_error("Registro não encontrado",404);
}
}
}

Router.php第227行:

        // use the force layout when force layout is set
$controller->view->force_layout = $this->force_layout;


// Initialize Controller Class And Pass All Arguments to the Controller Action
call_user_func_array(array($controller,$action),$args); //LINE 227
}
else{
if($this->is_partial_view==true){
echo "<div class='alert alert-danger'><b>$controller_name</b> Was Not Found In Controller Directory. <b>Please Check </b>" . CONTROLLERS_DIR."</div>";
}
else{
$this->page_not_found("<b>$controller_name</b> Was Not Found In Controller Directory. <b>Please Check </b>" . CONTROLLERS_DIR);
}
}
}

路由器线路 92:

/**
* Start page Dispatch From Current URl
* @var string
*/
function init(){

$basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';

// for now, we are only interested with the path only.
$page_url = substr($_SERVER['REQUEST_URI'], strlen($basepath));
$path = parse_url( $page_url, PHP_URL_PATH );

if(!empty( $path )){
$this->run( $path ); //LINE 92
}
else{
self::$page_name = DEFAULT_PAGE;
self::$page_action = DEFAULT_PAGE_ACTION;

$controller_name = ucfirst(DEFAULT_PAGE)."Controller";
$controller = new IndexController;
$controller->{DEFAULT_PAGE_ACTION}();
}
}

完整查看功能:

/**
* View Record Action
* @return View
*/
function view( $rec_id = null , $value = null){
$db = $this->GetModel();
$fields = array( 'post_id', 'post_title', 'post_text', 'post_date', 'post_user_posted' );
if( !empty($value) ){
$db->where($rec_id, urldecode($value));
}
else{
$db->where('post_id' , $rec_id);
}
$record = $db->getOne( 'postagens', $fields );
if(!empty($record)){

$ActiveUser = USER_NAME;
$meu_id = $rec_id;
$db->rawQuery("UPDATE postagens SET post_user_posted='$ActiveUser' WHERE post_id='$meu_id'"); //MY UPDATE QUERY
render_json($records); //THE RENDER ONLY RETURN DATA IF I REMOVE THE UPDATE QUERY
}
else{
if($db->getLastError()){
render_error($db->getLastError());
}
else{
render_error("Registro não encontrado",404);
}
}
}

有人知道为什么 json 无法正确渲染吗?

最佳答案

尝试使用这个(注意大括号):

$db->rawQuery("UPDATE `postagens` SET post_user_posted='{$ActiveUser}' WHERE post_id='{$meu_id}'");

或者使用 PDO prepared statements哪些更具可读性。

一般来说,如果您想知道查询出了什么问题,可以将它们嵌入到 try/catch block 中,然后查看捕获的异常是什么。所以类似:

try {
$db->rawQuery("UPDATE postagens SET post_user_posted='{$ActiveUser}' WHERE post_id='{$meu_id}'");
render_json($records);
} catch(\Exception $e) {
// this is the error:
// $e->getMessage();
}
<小时/>

在一个不相关的注释中,您为变量混合了不同的命名约定,这是一种不好的做法!

关于php - PDO:执行数据库更新时 Json 不呈现响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53999479/

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