gpt4 book ai didi

php - 将插入查询更改为更新查询

转载 作者:行者123 更新时间:2023-11-29 10:34:58 25 4
gpt4 key购买 nike

我正在 OpenCart MVC 中工作,但在更新数据库记录时遇到问题。我有一个显示数据库表中所有记录的列表。每条记录都有一个带有编辑选项的下拉菜单。单击编辑时,将打开填写的表单,并使用现有值填充输入(到目前为止一切都很好!),当输入新信息时,用户应该能够单击保存按钮并更新数据库。目前保存按钮查询是插入查询。所以我想它没有写,因为它已经有记录了。我也不想添加新按钮(即更新) 我希望能够使用现有的保存按钮。我如何更改模型以反射(reflect)这一点?我是否有插入和更新查询以及某种检查?我要在 Controller 上执行此操作吗?我对如何做到这一点有点困惑。

型号:

public function addCampaign($data) {

$this->db->query("INSERT INTO " . DB_PREFIX . "campaigns SET campaign_name = '" . $this->db->escape($data['campaign_name']) . "', campaign_giving_goal = '" . (float)$data['campaign_giving_goal']
. "', code = '" . $this->db->escape($data['code']) . "', campaign_active = '" . $this->db->escape($data['campaign_active']) . "', campaign_giving_count_goal = '" . (float)$data['campaign_giving_count_goal'] . "', campaign_owner = '" . $this->db->escape($data['campaign_owner']). "', date_beginning = '" . $this->db->escape($data['date_beginning']). "', date_ending = '" . $this->db->escape($data['date_ending']). "'");

$this->cache->delete('campaign');

return $campaign_id;
}

控制:

public function add() {
$this->load->language('campaigns/campaign');

$this->document->setTitle($this->language->get('heading_title'));

$this->load->model('campaigns/campaign');

if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {


$this->model_campaigns_campaign->addCampaign($this->request->post);
$this->model_campaigns_campaign->addComponent($this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$url = '';

if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}

if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}

$this->response->redirect($this->url->link('campaigns/campaign', 'token=' . $this->session->data['token'] . $url, true));
}

$this->getForm();

}

查看:

<div class="cancel-save">
<button type="submit" form="form-campaign" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary" onclick="submitForms()"><i class="fa fa-save"></i></button>

<a href="<?php echo $cancel; ?>" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-default"><i class="fa fa-reply"></i></a>
</div>

最佳答案

试试这个。在 Controller 上像这样修改。

public function add() {
$this->load->language('campaigns/campaign');

$this->document->setTitle($this->language->get('heading_title'));

$this->load->model('campaigns/campaign');

if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {
if (!isset($this->request->get['campaign_id'])) {
$this->model_campaigns_campaign->addCampaign($this->request->post);
$this->model_campaigns_campaign->addComponent($this->request->post);
} else {
$this->model_campaigns_campaign->updateCampaign($this->request->get['campaign_id'], $this->request->post);
$this->model_campaigns_campaign->updateComponent($this->request->get['campaign_id'], $this->request->post);
}
$this->session->data['success'] = $this->language->get('text_success');
$url = '';

if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}

if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}

$this->response->redirect($this->url->link('campaigns/campaign', 'token=' . $this->session->data['token'] . $url, true));
}

$this->getForm();

}

然后在模型上添加此代码

public function updateCampaign($campaign_id, $data) {

$this->db->query("UPDATE " . DB_PREFIX . "campaigns SET campaign_name = '" . $this->db->escape($data['campaign_name']) . "', campaign_giving_goal = '" . (float)$data['campaign_giving_goal']
. "', code = '" . $this->db->escape($data['code']) . "', campaign_active = '" . $this->db->escape($data['campaign_active']) . "', campaign_giving_count_goal = '" . (float)$data['campaign_giving_count_goal'] . "', campaign_owner = '" . $this->db->escape($data['campaign_owner']). "', date_beginning = '" . $this->db->escape($data['date_beginning']). "', date_ending = '" . $this->db->escape($data['date_ending']). "' WHERE campaign_id = '" . (int)$campaign_id . "'");

$this->cache->delete('campaign');
}

我无法提供 updateComponen 模型,因为你没有编写它。也许您可以使用 updateCampaign 代码作为引用来编写它。

关于php - 将插入查询更改为更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46591577/

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