【发布时间】:2012-08-15 17:49:05
【问题描述】:
将数据插入 MySQL 数据库时出现问题。出现以下错误:
SQLSTATE[HY000]: 一般错误: 1452 无法添加或更新子行: 外键约束失败 (
login.employees, CONSTRAINTemployees_ibfk_1FOREIGN KEY (u_id) REFERENCESusers(id) 更新级联删除级联)
我有一张名为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,));
}
}
?>
【问题讨论】:
-
请发布 Application_Form_employee 代码
-
添加了员工表单代码。
-
在您的控制器中,您不会在 u_id 字段中存储任何内容。那个领域应该是什么?
-
当任何用户登录时。。 .thay 有 id(u_id)。并添加员工(在员工表中)。因为用户可以添加超过 1 名员工,并且许多员工只有一张表。所以其他用户也添加了他们的员工。登录后,我将数据存储在 zend_auth_storage_session() 中并获取用户的行和 id。
标签: php zend-framework