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,));
}
}
?>