如何编写php文件以将我的多步骤表单连接到MySQL数据库?

时间:2012-11-28 08:26:27

标签: php silverstripe

<?php
class JpResearchSurveyDbPage extends DataObject{
static $db = array(
'year_living' => 'Varchar(200)',
'other_public_transport' => 'Varchar(50)',
'year_living1' => 'Varchar(200)',
'type_of_mode_unfamiliar_services' => 'Varchar(50)'
);
enter code here
}
$host="localhost"; 
$username="sgevh_admin"; 
$password="q1w2e3r4t5";  
$db_name="sgevh_test";
$con = mysql_connect("$host","$username","$password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("$db_name", $con);

$sql="INSERT INTO year (aaa,bbb,ccc,eee)
VALUES
('$_POST[year_living]','$_POST[other_public_transport]','$_POST[type_of_mode_unfamiliar_services]','$_POST[year_living1]')";

    //$sql="INSERT INTO month (ccc)
    //VALUES
    //('$_POST[type_of_mode_unfamiliar_services]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>`

//这是我们用来将多步骤表单连接到数据库的测试php编码。

<?php
class JpResearchSurveyMultiForm extends MultiForm{
public static $start_step = 'JpResearchSurveyFirstStep';
public function finish($data, $form){
parent::finish($data, $form);
$firstStep = $this->getSavedStepByClass('JpResearchSurveyFirstStep');
$_JpResearchSurveyFirstPage = new JpResearchSurveyDbPage();
$firstStep->saveInto($_JpResearchSurveyFirstPage);

$secondStep = $this->getSavedStepByClass('JpResearchSurveySecondStep');
$secondStep->saveInto($_JpResearchSurveyFirstPage);

$_JpResearchSurveyFirstPage->write();
return $this->controller->customise(array(
'Form' => false,
'Content' => 'Thanks for registering!'
))->renderWith('Page');
}
}
?>

//这是测试编码,它提供了当我们按“提交”按钮提交数据时表格的最后一步将做什么的说明。当我们尝试使用我们的两页表单提交表单并提交详细信息时,只有第二页的字段数据(表单的最后一步)存储在数据库中。

这两个编码中的哪一个会给我们带来无法在以前的表单会话中存储字段数据的问题?

1 个答案:

答案 0 :(得分:0)

保存数据库中的每个步骤并在每个步骤上进行更新是不可取的。而是使用会话和cookie来管理用户输入,并在最后一步,将它们存储在数据库中。请记住以后杀死会话