将数据插入 MySQL 数据库时出现问题。出现以下错误:
SQLSTATE [HY000]:一般错误:1452 无法添加或更新子行:外键约束失败(
login
.employees
, CONSTRAINTemployees_ibfk_1
FOREIGN KEY (u_id
) REFERENCESusers
(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,));
}
}
?>