简体   繁体   中英

How do I code the php file to connect my multi-step form to MySQL database?

<?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);
?>`

//This is the test php coding that we used to connect our multi-step form to our database.

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

//This is the test coding which gives instruction as to what will the final step of the form do when we press the "submit" button to submit the data. When we tried out the form and submit the details using our two pages form, only the fields' data of the second page which is the final step of the form was stored in the database.

Which of the two coding is giving us the problem of not being able to store the fields' data in the previous forms sessions?

It's not preferable to save each step in the database and update on each step. Instead use session and cookie to manage user input, and on the final step, store them on the DB. Just keep in mind to kill the session afterward

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM