0

将数据插入 MySQL 数据库时出现问题。出现以下错误:

SQLSTATE [HY000]:一般错误:1452 无法添加或更新子行:外键约束失败(login. employees, CONSTRAINT employees_ibfk_1FOREIGN KEY ( u_id) REFERENCES users( id) ON DELETE CASCADE ON UPDATE CASCADE)

我有一个名为employees

+-------------+
|  employees  |
+-------------+
| id          |
| u_id        |  foreign key of table `user` field `id`
| firstname   |
| lastname    |
| city        |
| designation |
+-------------+

我的表单代码:

class Application_Form_Login extends Zend_Form
{
public function init()
{
    $this->setName('Login');
    $this->setMethod('post');

    $username = $this->createElement('text','username');
    $username->setLabel('Username: *')
            ->setRequired(true)
            ->setFilters(array('StringTrim','StringToLower'))
            ->getValidators(array('stringLength',false,array(0,50)));

    $password = $this->createElement('password','password');
    $password->setLabel('Password: *')
            ->setRequired(true)
            ->setFilters(array('StringTrim'))
            ->getValidators(array('stringLength',false,array(0,50)));

    $submit = $this->createElement('submit','Sign in');
    $submit->setLabel('Sign in')
            ->setIgnore(true);

    $this->addElements(array($username,
                        $password,
                        $submit,));
}
}

这是我的表模型:

class Application_Model_Dbtables_Employees extends Zend_Db_Table
{
protected $_name = 'employees';
}  

这是我的控制器:

class EmployeeController extends Zend_Controller_Action
{

public function init()
{
    /* Initialize action controller here */
}

public function indexAction()
{
    // action body
}

public function addAction()
{
    $emp = new Application_Model_Dbtables_Employees();
    $form = new Application_Form_employee();
    $this->view->form = $form;

    if($this->getRequest()->isPost())
    {
        $formdata = $this->_request->getPost();

          if($form->isValid($formdata))
          {
            unset($formdata['addemployee']);
            $emp->insert($formdata);
            $this->_redirect('Auth/home');
          }
          else
          {
            $this->view->errMsg = "Cound not able to insert data";
          }
    }
}
}

我的视图文件:

<?php
if(isset($this->errMsg)) {
echo $this->errMsg;
}
?>

<?php 
 echo $this->form;
?>

我的代码员工

<?php
class Application_Form_employee extends Zend_Form
{
public function init()
{
    $this->setMethod('post');
    $this->setName('addemployee');

    $firstname = $this->createElement('text', 'firstname');
    $firstname->setLabel('Firstname: *')
                ->setRequired(true)
                ->setFilters(array('StringToLower'));

    $lastname = $this->createElement('text', 'lastname');
    $lastname->setLabel('Lastname: *')
                ->setRequired(true)
                ->setFilters(array('StringToLower'));

    $city = $this->createElement('select', 'city');
    $city->setLabel('city: ')
        ->setRequired(true)
        ->setMultiOptions(array(
                '' => '---select city---',
                'ahmedabad' => 'Ahmedabad',
                'Vadodara' => 'Vadodara',
                'Anand' => 'Anand',
                'Surat' => 'Surat'));
    $designation = $this->createElement('select', 'designation');
    $designation->setLabel('Designation: ')
                ->setMultiOptions(array(
                        'java' => 'Java developer',
                        'php' => 'PHP developer',
                        'unix' => 'Unix developer'));

    $submit = $this->createElement('submit', 'addemployee');
    $submit->setLabel('Add Employee')
            ->setIgnore(true);

    $this->addElements(array(
            $firstname,
            $lastname,
            $city,
            $designation,
            $submit,));
}
}
?>
4

1 回答 1

0

这是我能想到的一种使它起作用的方法。基本上,当在员工表中插入新条目时,您有一个限制,即数据库需要相应的外键 (u_id)。您传递了表单数据,但没有传递 u_id。因此,在您的 Application_Form_employee 表单中,您可以添加一个隐藏字段来保存插入记录的用户 ID:

$uId = $this->createElement('hidden', 'u_id');
$uId->setValue(Zend_Auth::getInstance()->getIdentity()->id);
//And when you add elements to your form, add this field
    $this->addElements(array(
            $firstname,
            $lastname,
            $city,
            $designation,
            $uId,
            $submit,));

我认为这可以解决问题。

于 2012-08-22T12:20:31.313 回答