php - Zend update() 更改 id,尽管 id 设置为 NULL 和 auto_increment

标签 php mysql zend-framework

更具体地说,我有一个用于创建和编辑事件的 Web 应用程序。在我的数据库表上进行新的截断后,当我创建一个事件时,它以 id = 1 开头(尽管我记得它应该以 0 开头),当我在我的数据库表中编辑特定事件(在本例中,其中 id=1)时phpmyadmin 当我检查表时,id = 1 的事件更改为 id = 0,当我尝试添加新事件时,它跳转到 id = 2。

数据库类:

class Application_Model_DbTable_Eventsdb extends Zend_Db_Table_Abstract {

protected $_name = 'events';

public function CreateEvent($category, $title, $subtitle, $video, $date, $ages, $resume, $description, $image1, $image2, $image3, $image4, $image5) {
    $data = array(
        "id" => NULL,
        "category" => $category,
        "title" => $title,
        "subtitle" => $subtitle,
        "video" => $video,
        "date" => $date,
        "ages" => $ages,
        "resume" => $resume,
        "description" => $description,
        "image1" => $image1,
        "image2" => $image2,
        "image3" => $image3,
        "image4" => $image4,
        "image5" => $image5
    );
    $this->insert($data);
    return TRUE;
}

public function UpdateEvent($eventID, $category, $title, $subtitle, $video, $date, $ages, $resume, $description, $image1, $image2, $image3, $image4, $image5) {
    $bind = array(
        "id" => NULL,
        "category" => $category,
        "title" => $title,
        "subtitle" => $subtitle,
        "video" => $video,
        "date" => $date,
        "ages" => $ages,
        "resume" => $resume,
        "description" => $description,
        "image1" => $image1,
        "image2" => $image2,
        "image3" => $image3,
        "image4" => $image4,
        "image5" => $image5
    );
    $where = "`id` = $eventID";
    $this->_db->update($this->_name, $bind, $where);

}

管理 Controller :

class AdminController extends Zend_Controller_Action {

public function createEventAction() {
    $eventdb = new Application_Model_DbTable_Eventsdb();
    $form = new Application_Form_CreateEvent();

    $this->view->event_form = $form;

    if ($this->getRequest()->isPost()) {
        if ($form->isValid($_POST)) {
            $formdata = $form->getValues();
            $confirmation = $eventdb->CreateEvent($formdata['category'], $formdata['title'], $formdata['subtitle'], $formdata['video'], $formdata['date'], $formdata['ages'], $formdata['resume'], $formdata['description'], $formdata['image1'], $formdata['image2'], $formdata['image3'], $formdata['image4'], $formdata['image5']);
            if ($confirmation) {
                $this->view->confirmation = "Evenimentul a fost introdus cu success.";
            }
        }
    }
}

    public function editEventAction() {
    $eventdb = new Application_Model_DbTable_Eventsdb();
    $form = new Application_Form_EditEvent();

    $this->view->tabere = $eventdb->getEventsbyCategory('Tabara');
    $this->view->ateliere = $eventdb->getEventsbyCategory('Atelier');
    $this->view->workshopuri = $eventdb->getEventsbyCategory('Workshop');
    $this->view->petreceri = $eventdb->getEventsbyCategory('Petrecere');
    $this->view->alte = $eventdb->getEventsbyCategory('Alt');

    if (isset($_GET['eventID'])) {
        $eventID = $_GET['eventID'];
        $event_data = $eventdb->getEventsbyId($eventID);
        $this->view->event_data = $event_data;
        $this->view->form = $form;
    }

    if ($this->getRequest()->isPost()) {
        if ($form->isValid($_POST)) {
            $renew_data = $form->getValues();
            $eventdb->UpdateEvent($eventID, $renew_data['category'], $renew_data['title'], $renew_data['subtitle'], $renew_data['video'], $renew_data['date'], $renew_data['ages'], $renew_data['resume'], $renew_data['description'], $renew_data['image1'], $renew_data['image2'], $renew_data['image3'], $renew_data['image4'], $renew_data['image5']);
            $this->_redirect('EditeazaEveniment');

        }
    }
}
}

最佳答案

在更新函数中,您已告诉 MySQL 将 id 设置为 NULL。它无法做到这一点,因此它会尝试做下一个最好的事情,在本例中将该值转换为 0。由于您不想更改 id 值,因此可以从 $ 中删除该键和值绑定(bind) 数组或将正确的 eventID 值放入其中。

关于php - Zend update() 更改 id,尽管 id 设置为 NULL 和 auto_increment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26827658/

相关文章:

php - .htaccess 拒绝访问特定文件?超过一个

php - 在 PHP 中组织/使用可重用的代码库

mysql - Spring:在项目中设置 MySQL 数据库

php - Zend_Mail and =0D=0A=3D=3D=3D=3D=3D

php - Zend Framework 多表查询

php - 在 php 中解析 json 时出错

php - 如何构建具有动态输入字段的表?

java - MySQL 查询使用公共(public)字符串方法返回字符串

MYSQL 查询解释选择

jquery - Bootstrap 侧边栏下拉菜单