Grand Error

hello all

i try to build kind of a blog

when i try to create new post i got this page

anyone pls?

CDbException

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (blog.tbl_post, CONSTRAINT FK_post_author FOREIGN KEY (author_id) REFERENCES tbl_user (id) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO tbl_post (title, content, tags, status, author_id, update_time, create_time) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6)

C:\xampp\htdocs\yii\framework\db\CDbCommand.php(354)

342 return $n;

343 }

344 catch(Exception $e)

345 {

346 if($this->_connection->enableProfiling)

347 Yii::endProfile(‘system.db.CDbCommand.execute(’.$this->getText().’)’,‘system.db.CDbCommand.execute’);

348 $errorInfo = $e instanceof PDOException ? $e->errorInfo : null;

349 $message = $e->getMessage();

350 Yii::log(Yii::t(‘yii’,‘CDbCommand::execute() failed: {error}. The SQL statement executed was: {sql}.’,

351 array(’{error}’=>$message, ‘{sql}’=>$this->getText().$par)),CLogger::LEVEL_ERROR,‘system.db.CDbCommand’);

352 if(YII_DEBUG)

353 $message .= '. The SQL statement executed was: '.$this->getText().$par;

354 throw new CDbException(Yii::t(‘yii’,‘CDbCommand failed to execute the SQL statement: {error}’,

355 array(’{error}’=>$message)),(int)$e->getCode(),$errorInfo);

356 }

357 }

358

359 /**

360 * Executes the SQL statement and returns query result.

361 * This method is for executing an SQL query that returns result set.

362 * @param array $params input parameters (name=>value) for the SQL execution. This is an alternative

363 * to {@link bindParam} and {@link bindValue}. If you have multiple input parameters, passing

364 * them in this way can improve the performance. Note that if you pass parameters in this way,

365 * you cannot bind parameters or values using {@link bindParam} or {@link bindValue}, and vice versa.

366 * binding methods and the input parameters this way can improve the performance.

Stack Trace

#0

  • C:\xampp\htdocs\yii\framework\db\ar\CActiveRecord.php(1014): CDbCommand->execute()

#1

  • C:\xampp\htdocs\yii\framework\db\ar\CActiveRecord.php(787): CActiveRecord->insert(null)

#2

– C:\xampp\htdocs\blog\protected\controllers\PostController.php(89): CActiveRecord->save()

84 // $this->performAjaxValidation($model);

85

86 if(isset($_POST[‘Post’]))

87 {

88 $model->attributes=$_POST[‘Post’];

89 if($model->save())

90 $this->redirect(array(‘view’,‘id’=>$model->id));

91 }

92

93 $this->render(‘create’,array(

94 ‘model’=>$model,

#3

  • C:\xampp\htdocs\yii\framework\web\actions\CInlineAction.php(50): PostController->actionCreate()

#4

  • C:\xampp\htdocs\yii\framework\web\CController.php(309): CInlineAction->runWithParams(array("r" => "post/create"))

#5

  • C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(134): CController->runAction(CInlineAction)

#6

  • C:\xampp\htdocs\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()

#7

  • C:\xampp\htdocs\yii\framework\web\CController.php(1146): CFilter->filter(CFilterChain)

#8

  • C:\xampp\htdocs\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)

#9

  • C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(131): CInlineFilter->filter(CFilterChain)

#10

  • C:\xampp\htdocs\yii\framework\web\CController.php(292): CFilterChain->run()

#11

  • C:\xampp\htdocs\yii\framework\web\CController.php(266): CController->runActionWithFilters(CInlineAction, array("accessControl"))

#12

  • C:\xampp\htdocs\yii\framework\web\CWebApplication.php(276): CController->run("create")

#13

  • C:\xampp\htdocs\yii\framework\web\CWebApplication.php(135): CWebApplication->runController("post/create")

#14

  • C:\xampp\htdocs\yii\framework\base\CApplication.php(162): CWebApplication->processRequest()

#15

– C:\xampp\htdocs\blog\index.php(13): CApplication->run()

08 defined(‘YII_DEBUG’) or define(‘YII_DEBUG’,true);

09 // specify how many levels of call stack should be shown in each log message

10 defined(‘YII_TRACE_LEVEL’) or define(‘YII_TRACE_LEVEL’,3);

11

12 require_once($yii);

13 Yii::createWebApplication($config)->run();

The Foreign Key constraint fails. Maybe you need to add author into tbl_user table first?

ok, so i add the author_id to tbl_user

than i got this massage again

CDbException

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (blog.tbl_post, CONSTRAINT FK_post_author FOREIGN KEY (author_id) REFERENCES tbl_user (id) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO tbl_post (title, content, tags, status, author_id, update_time, create_time) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6)

C:\xampp\htdocs\yii\framework\db\CDbCommand.php(354)

342 return $n;

343 }

344 catch(Exception $e)

345 {

346 if($this->_connection->enableProfiling)

347 Yii::endProfile(‘system.db.CDbCommand.execute(’.$this->getText().’)’,‘system.db.CDbCommand.execute’);

348 $errorInfo = $e instanceof PDOException ? $e->errorInfo : null;

349 $message = $e->getMessage();

350 Yii::log(Yii::t(‘yii’,‘CDbCommand::execute() failed: {error}. The SQL statement executed was: {sql}.’,

351 array(’{error}’=>$message, ‘{sql}’=>$this->getText().$par)),CLogger::LEVEL_ERROR,‘system.db.CDbCommand’);

352 if(YII_DEBUG)

353 $message .= '. The SQL statement executed was: '.$this->getText().$par;

354 throw new CDbException(Yii::t(‘yii’,‘CDbCommand failed to execute the SQL statement: {error}’,

355 array(’{error}’=>$message)),(int)$e->getCode(),$errorInfo);

356 }

357 }

358

359 /**

360 * Executes the SQL statement and returns query result.

361 * This method is for executing an SQL query that returns result set.

362 * @param array $params input parameters (name=>value) for the SQL execution. This is an alternative

363 * to {@link bindParam} and {@link bindValue}. If you have multiple input parameters, passing

364 * them in this way can improve the performance. Note that if you pass parameters in this way,

365 * you cannot bind parameters or values using {@link bindParam} or {@link bindValue}, and vice versa.

366 * binding methods and the input parameters this way can improve the performance.

Stack Trace

#0

  • C:\xampp\htdocs\yii\framework\db\ar\CActiveRecord.php(1014): CDbCommand->execute()

#1

  • C:\xampp\htdocs\yii\framework\db\ar\CActiveRecord.php(787): CActiveRecord->insert(null)

#2

– C:\xampp\htdocs\blog\protected\controllers\PostController.php(89): CActiveRecord->save()

84 // $this->performAjaxValidation($model);

85

86 if(isset($_POST[‘Post’]))

87 {

88 $model->attributes=$_POST[‘Post’];

89 if($model->save())

90 $this->redirect(array(‘view’,‘id’=>$model->id));

91 }

92

93 $this->render(‘create’,array(

94 ‘model’=>$model,

#3

  • C:\xampp\htdocs\yii\framework\web\actions\CInlineAction.php(50): PostController->actionCreate()

#4

  • C:\xampp\htdocs\yii\framework\web\CController.php(309): CInlineAction->runWithParams(array("r" => "post/create"))

#5

  • C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(134): CController->runAction(CInlineAction)

#6

  • C:\xampp\htdocs\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()

#7

  • C:\xampp\htdocs\yii\framework\web\CController.php(1146): CFilter->filter(CFilterChain)

#8

  • C:\xampp\htdocs\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)

#9

  • C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(131): CInlineFilter->filter(CFilterChain)

#10

  • C:\xampp\htdocs\yii\framework\web\CController.php(292): CFilterChain->run()

#11

  • C:\xampp\htdocs\yii\framework\web\CController.php(266): CController->runActionWithFilters(CInlineAction, array("accessControl"))

#12

  • C:\xampp\htdocs\yii\framework\web\CWebApplication.php(276): CController->run("create")

#13

  • C:\xampp\htdocs\yii\framework\web\CWebApplication.php(135): CWebApplication->runController("post/create")

#14

  • C:\xampp\htdocs\yii\framework\base\CApplication.php(162): CWebApplication->processRequest()

#15

– C:\xampp\htdocs\blog\index.php(13): CApplication->run()

08 defined(‘YII_DEBUG’) or define(‘YII_DEBUG’,true);

09 // specify how many levels of call stack should be shown in each log message

10 defined(‘YII_TRACE_LEVEL’) or define(‘YII_TRACE_LEVEL’,3);

11

12 require_once($yii);

13 Yii::createWebApplication($config)->run();

Hi,

that’s not what Roman said: the post author must exist in the table tbl_user before you create occurrence for this author in tbl_post.

You need to set author_id attribute in your PostController:


// $this->performAjaxValidation($model);


if(isset($_POST['Post']))

{

	$model->attributes=$_POST['Post'];

	$model->author_id = 1; // You need to set the author's ID here!

	if($model->save())

        	$this->redirect(array('view','id'=>$model->id));

}