简体   繁体   中英

Save data to a database from a multi-page form Wordpress

how to save data to a database from a multi-page form using sessions in wordpress?

session_start();
foreach ($_POST as $key => $value) { 
    $_SESSION['post'][$key] = $value;
}
extract($_SESSION['post']);
if (isset($_POST["submit"])) {
    extract($_SESSION['post']);
    //global $wpdb;
    //$wpdb->insert("tablename", array("data" => $_SESSION["data"], array("%s"));
    include 'dbconnect.php';
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO tablename ( data ) VALUES ( '{$data}' )";
        $conn ->exec($sql);
    }
        catch(PDOException $e)
    {
        echo "Connection failed: " . $e->getMessage();
    }
        $conn = null;
        session_destroy();
    }

Trying to save a multi-page form to a database. I have no issues saving a single page form. However, once I try to use $_SESSION it will no longer save the data. Thank you in advance UPDATE: I could not get it to work using $wpdb; but it works just fine using PDO.

I would generate the second page's form with hidden inputs with the values you get from the first form. Then save all the values to the database after checking them in the 2nd form.

I would use three tables in your database. One with all clients that took part in the survey with very little information (like a timestamp, name or sth), one with all questions (question text, page they are listed on etc) and a third one with the userID and quesionID and the answer. This construct is contra-intuitive for debugging but is still the better option.

For example can you just issue SELECT * FROM questions WHERE page=2 and you get all the questions on page two.
But the big advantage for you here is that you can safe the answers to the answer-table independently from the other answers.

You can access all answers by a particular user by SELECT * FROM answers WHERE uid=5 or all answers for a certain question.

Using this database structure is more versatile than cramming every bit of information into one big table.

I can give you more detailed information or code if you need it.

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