简体   繁体   English

如何在修复中添加功能 - 在 model php 文件中的特定 ID 中更新数据库表中的单元格

[英]How to add a fuction to Repair - update a cell in database table in specific id in model php file

I make a remake of an extension so I stuck in the last work that I must to do.我重新制作了一个扩展,所以我坚持了我必须做的最后一项工作。 I created a twig for admin which displays a list that take data from a table in the database.我为管理员创建了一个 twig,它显示了一个从数据库中的表中获取数据的列表。 In each row of the list I created a button that I want to fix specific cell of the table.在列表的每一行中,我创建了一个按钮,我想修复表格的特定单元格。 So each line has its own id.所以每一行都有自己的id。

See the image from page list:查看页面列表中的图像:

在此处输入图像描述

I made the controller file but I stuck in model file that must do the rest work.我制作了 controller 文件,但我卡在了 model 文件中,该文件必须执行 rest 工作。

In the following code I have to change something so that by pressing the repair button it reads the specific id.在下面的代码中,我必须更改某些内容,以便通过按下修复按钮读取特定的 ID。

public function repairSaveCarts($link_id=" ") {
        $this->db->query("SELECT * FROM " . DB_PREFIX . "save_cart WHERE link_id = '" . (int)$link_id . "'");

        $db_id = $this->db->getLastId();
            $end_url = base64_encode(serialize($db_id));
            $url = $this->config->get('config_url').$end_url;
            
            $this->db->query("UPDATE " . DB_PREFIX . "save_cart SET shorturl = '" . $this->db->escape($url) . "' WHERE link_id = '" . (int)$db_id . "'");
    }

I thing that the change must be done in $db_id = $this->db->getLastId();我认为必须在$db_id = $this->db->getLastId();中进行更改line but I'm not sure.行,但我不确定。 Someone that can help please.有人可以帮忙请。

I make it work.我让它工作。 I had a small syntax error in request->get | post我在request->get | post中有一个小的语法错误。 request->get | post at controller and for this reason the model didn't get the correct id from button.在 controller request->get | post ,因此 model 没有从按钮获得正确的 ID。

public function repairSaveCarts($link_id) {
        
        $end_url = base64_encode(serialize((int)$link_id));
        
        $url = $this->config->get('config_url').$end_url;
        
        $this->db->query("UPDATE " . DB_PREFIX . "save_cart SET shorturl = '" . $this->db->escape($url) . "', date_added = NOW() WHERE link_id = '" . (int)$link_id . "'");
        
    
        
        return $query->row;
    }

One thing I notice with serialize is that when the model got the id from the controller it received it as a spring value and not as an int value.我注意到serialize的一件事是,当 model 从 controller 获得 id 时,它作为spring值而不是int值接收它。 The result was to give as example for结果是举个例子

id 10 ---> serialize value= s:2:"10";

To fix this i add in model the (int) value after serialize and now give me the correct results为了解决这个问题,我在 model 中添加serialize后的(int)值,现在给我正确的结果

id 10 ---> serialize value= i:10; . .

The only problem now is that when the module save to database from frond-end page give me the full URL site:现在唯一的问题是,当模块从前端页面保存到数据库时,请给我完整的 URL 站点:

https://www.example.com/aToxMDs=

but when in admin side the functionRepair save to database give me only:但是当在管理员方面,functionRepair 保存到数据库只给我:

aToxMDs=

The line $url = $this->config->get('config_url').$end_url;$url = $this->config->get('config_url').$end_url; seems that doesn't work.似乎这行不通。

What i must edit to add the store url before the $end_url ???我必须编辑什么才能在$end_url ???

Here's a revised version of the function for your model class:这是 model class 的 function 的修订版:

public function repairSaveCarts($link_id, $base) {
    $end_url = base64_encode(serialize($link_id));
    $url = $base.$end_url;
        
    $this->db->query("UPDATE " . DB_PREFIX . "save_cart SET shorturl = '" . $this->db->escape($url) . "' WHERE link_id = '" . (int)$link_id . "'");
}

You should also cater for HTTPS not just HTTP, so add the following to the controller to get the site Url and pass that value to the 'repair' function. You should also cater for HTTPS not just HTTP, so add the following to the controller to get the site Url and pass that value to the 'repair' function. This should be done just before the existing call to the model function:这应该在对 model function 的现有调用之前完成:

// check if the request was made over HTTPS or not
if ($this->request->server['HTTPS']) {
    $server = $this->config->get('config_ssl');
} else {
    $server = $this->config->get('config_url');
}

$this->whateveryourmodelnameis->repairSaveCarts($link_id, $server);

And finally i make it.最后我成功了。 I replace this:我替换这个:

public function repairSaveCarts($link_id, $base) {
    $end_url = base64_encode(serialize($link_id));
    $url = $base.$end_url;
        
    $this->db->query("UPDATE " . DB_PREFIX . "save_cart SET shorturl = '" . $this->db->escape($url) . "' WHERE link_id = '" . (int)$link_id . "'");
}

with this on model file:在 model 文件上有这个:

public function repairSaveCarts($link_id) {
    $end_url = base64_encode(serialize((int)$link_id));
    if ($this->request->server['HTTPS']) {
    $url = HTTPS_CATALOG . $end_url;
    } else {
    $url = HTTP_CATALOG . $end_url;
    }
        
    $this->db->query("UPDATE " . DB_PREFIX . "save_cart SET shorturl = '" . $this->db->escape($url) . "', date_added = NOW() WHERE link_id = '" . (int)$link_id . "'");
}

Thank you Daniel for your help.谢谢丹尼尔的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在PHP的更新功能中什么也不更新? - How to update nothing in an update fuction in PHP? PHP-如何将 html 表格单元格值添加到 MYSQL 数据库 - PHP- How to add html table cell values to MYSQL Database 如何在数据库表的同一行中添加创建后复制 $URL 的功能 - How to add a fuction to copy $URL after creation in the same row of database table PHP更新来自id的mysql数据库表 - PHP Update mysql Database Table from id 如何在特定时间运行PHP FILE并自动更新数据库字段 - How to run PHP FILE for specific time and update Database fields automatically 使用AJAX在PHP文件中调用函数,该ID具有数据库中记录的特定ID以更新记录 - Calling a Function in PHP file, using AJAX, with an ID specific to a record in Database to update record MySQL / PHP使用更新将字符串添加到数据库中的单元格而不会覆盖 - MySQL/PHP Add string to cell in database using Update without overwriting 使用 php 更新数据库中特定 id 的输入详细信息 - update entered details on specific id in database using php 如何获取表中特定记录的ID - PHP - How to get id of specific record in a table - PHP 如何通过单击超链接表格单元格并使用单击的 ID 从 SQL 数据库中的 model 填充数据来使用引导程序 4 打开模式 - How do I open modal using bootstrap 4 by clicking a hyperlinked table cell and populate data in the model from SQL database using the clicked ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM