php - 无法从多页表单输入数据

标签 php mysql database-design

在这里,我想从多页表单中向数据库中插入数据。

我将数据存储在第 1 页(createfinal.php)和第 2 页(personalfinal.php)的 $_SESSION 中,并尝试直接存储第 3 页(socialfinal.php)的数据。 但我收到名为消息的数据库错误: undefined variable :name_of_the_variable。

您还可以向我推荐其他方法,使我的代码尽可能小。

这是简短的代码::

Controller 用户:

<?php

class Users extends CI_Controller
{

    public function viewinsert1()
    {

        $this->load->view('createfinal');
    }

    public function viewinsert2()
    {

        $this->load->view('personalfinal');
    }

    public function viewinsert3()
    {

        $this->load->view('socialfinal');
    }

    public function viewupdate()
    {

        $this->load->view('view_update');
    }

    public function viewdelete()
    {

        $this->load->view('view_delete');
    }

    public function show()
    {
        // $this->load->model('user_model');
        $data['results'] = $this->user_model->get_users();


        $this->load->view('user_view', $data);

        // foreach ($result as $object) 
        // {
        //  echo $object->id . "</br>"; 
        // }
    }

    // public function insert()
    // {

    //  $username = "Peter";
    //  $password = "12345";
    //  $this->user_model->create_users([
    //      'username' => $username,
    //      'password' => $password
    //      ]);
    // }

    public function insert()
    {
        // echo $this->input->post('password') . "</br>";
        // echo $_POST['username'];


        $_SESSION['email'] = $email;
        $_SESSION['pass'] = $pass;
        $_SESSION['cpass'] = $cpass;
        $_SESSION['fname'] = $fname;
        $_SESSION['lname'] = $lname;
        $_SESSION['phone'] = $phone;
        $_SESSION['address'] = $address;

        $data = array(
                    'email' => $email,
                    'firstname' => $fname,
                    'lastname' => $lname,
                    'phone' => $phone,
                    'address' => $address,
                    'twitter' => $this->input->post('twitter'),
                    'facebook' => $this->input->post('facebook'),
                    'googleplus' => $this->input->post('gplus'),
                    'password' => $pass,
                    'confirm' => $cpass, 
                    );

        // echo var_dump($data);


        $result = $this->user_model->create_users($data);

        $this->load->view('createfinal');

        echo "<h1>The data has been inserted</h1>";
    }


    public function update()
    {

        if (isset($_POST['update'])) 
        {

            $id = $this->input->post('id');
            $data = array(
                    'username' => $this->input->post('username'),
                    'password' => $this->input->post('password')
                          );

            $this->user_model->update_users($data, $id);

            echo "<h1> Data is updated successfully:) </h1>";
            $this->load->view('view_update');
        }
        else
        {

            echo "Oops! There is something wrong!";
        }

    }

    public function delete()
    {

        if (isset($_POST['delete'])) 
        {

            $id = $this->input->post('id');
            $this->user_model->delete_users($id);

            echo "<h1>The data has been deleted.</h1>";

        }
        else
        {

            echo "<h3>No Such data exist!</h3>";
        }

    }
}
?>

模型用户模型:

<?php

class User_Model extends CI_Model
{

    public function get_users()
    {
        // $this->db->where([
        //  'id' => $user_id,
        //  'username' => $username
        //  ]);

        //$this->db->where('id', $user_id);

        $query = $this->db->get('users');
        return $query->result(); 

        // $query = $this->db->query("SELECT * FROM users");

        //return $query->num_rows(); //returns the number of rows


    }

    public function create_users($data)
    {

        return $this->db->insert('users', $data);

    }

    public function update_users($data, $id)
    {

        $this->db->where(['id'=>$id]);
        $this->db->update('users', $data);
    }

    public function delete_users($id)
    {
        $this->db->where(['id' => $id]);
        $this->db->delete('users');
    }
}
?>

下面是三个页面的三个 View : 第 1 页(createfinal.php):

<?php

session_start();
?>
<!DOCTYPE html>
<html>

<head>
    <title>Login Form</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style.css">
</head>
<body>
<!-- multistep form -->
<form id="msform" action="viewinsert2" method="post">
    <!-- progressbar -->
    <ul id="progressbar">
        <li class="active">Account Setup</li>
        <li>Social Profiles</li>
        <li>Personal Details</li>
    </ul>
    <!-- fieldsets -->
    <fieldset>
        <h2 class="fs-title">Create your account</h2>
        <h3 class="fs-subtitle">This is step 1</h3>
        <input type="text" name="email" placeholder="Email" />
        <input type="password" name="pass" placeholder="Password" />
        <input type="password" name="cpass" placeholder="Confirm Password" />
        <input type="submit" name="next" class="next action-button" value="Next" />
    </fieldset>


</body>
</html>

第 2 页(personalfinal.php):

<?php

session_start();

$email = $_POST['email'];
$pass = $_POST['pass'];
$cpass = $_POST['cpass'];

$_SESSION['email'] = $email;
$_SESSION['pass'] = $pass;
$_SESSION['cpass'] = $cpass;
?>
<!DOCTYPE html>
<html>
<head>
    <title>Login Form</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style.css">
</head>
<body>
<!-- multistep form -->
<form id="msform" method="post" action="viewinsert3">
    <!-- progressbar -->
    <ul id="progressbar">
        <li>Account Setup</li>
        <li class="active">Social Profiles</li>
        <li>Personal Details</li>
    </ul>
    <!-- fieldsets -->
    <fieldset>
        <h2 class="fs-title">Personal Details</h2>
        <h3 class="fs-subtitle">We will never sell it</h3>
        <input type="text" name="fname" placeholder="First Name" />
        <input type="text" name="lname" placeholder="Last Name" />
        <input type="text" name="phone" placeholder="Phone" />
        <textarea name="address" placeholder="Address"></textarea>
        <input type="submit" name="previous" class="previous action-button" value="Previous" />
        <input type="submit" name="submit" class="submit action-button" value="Submit" />
    </fieldset>
</form>

</body>
</html>

第 3 页(socialfinal.php):

<?php

session_start();

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$phone = $_POST['phone'];
$address = $_POST['address'];


$_SESSION['fname'] = $fname;
$_SESSION['lname'] = $lname;
$_SESSION['phone'] = $phone;
$_SESSION['address'] = $address;
?>
<!DOCTYPE html>
<html>
<head>
    <title>Login Form</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style.css">
</head>
<body>
<!-- multistep form -->
<form id="msform" method="post" action="insert">
    <!-- progressbar -->
    <ul id="progressbar">
        <li>Account Setup</li>
        <li>Social Profiles</li>
        <li class="active">Personal Details</li>
    </ul>
    <!-- fieldsets -->
    <fieldset>
        <h2 class="fs-title">Social Profiles</h2>
        <h3 class="fs-subtitle">Your presence on the social network</h3>
        <input type="text" name="twitter" placeholder="Twitter" />
        <input type="text" name="facebook" placeholder="Facebook" />
        <input type="text" name="gplus" placeholder="Google Plus" />
        <input type="submit" name="previous" class="previous action-button" value="Previous" />
        <input type="submit" name="next" class="next action-button" value="Next" />
    </fieldset>
</form>

</body>
</html>

错误图片: [1]: http://i.stack.imgur.com/izsjY.png

最佳答案

在您的用户 Controller 中尝试调整插入方法,如下图所示。

public function insert()
{
    // echo $this->input->post('password') . "</br>";
    // echo $_POST['username'];

    #Set the provided data into variables
    $email = $this->input->post('email');
    $pass  = $this->input->post('pass');
    $cpass = $this->input->post('cpass');
    $fname = $this->input->post('fname');
    $lname = $this->input->post('lname');
    $phone = $this->input->post('phone');
    $address = $this->input->post('address');

    $_SESSION['email'] = $email;
    $_SESSION['pass'] = $pass;
    $_SESSION['cpass'] = $cpass;
    $_SESSION['fname'] = $fname;
    $_SESSION['lname'] = $lname;
    $_SESSION['phone'] = $phone;
    $_SESSION['address'] = $address;

    $data = array(
                'email' => $email,
                'firstname' => $fname,
                'lastname' => $lname,
                'phone' => $phone,
                'address' => $address,
                'twitter' => $this->input->post('twitter'),
                'facebook' => $this->input->post('facebook'),
                'googleplus' => $this->input->post('gplus'),
                'password' => $pass,
                'confirm' => $cpass, 
                );

    // echo var_dump($data);


    $result = $this->user_model->create_users($data);

    $this->load->view('createfinal');

    echo "<h1>The data has been inserted</h1>";
}

关于php - 无法从多页表单输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35186388/

相关文章:

php - 网页的单个实例

php - 通过 php 和 html 表单上传文件的代码错误

PHP递归导航

数据库模式 : What's the standard way for manually sorting a table?

php - 找不到 Laravel 4 工作台类

mysql - (er_parse_error)#1064

MySQL 5.6/数据导出不起作用(转储数据)

php - 默认内容放在哪里?代码还是数据库?

design-patterns - 在通知系统中每个通知插入多少行?

php - 通过 PUT 将文件发送到 codeigniter REST_Controller