在这里,我想从多页表单中插入数据库中的数据。
我将数据存储在第1页的$ _SESSION(createfinal.php)和第2页(personalfinal.php)中,并尝试直接存储第3页的数据(socialfinal.php)。 但是我收到了名为Message的数据库错误: 未定义的变量:name_of_the_variable。
你也可以建议我使用其他方法让我的代码尽可能小。
这是简短的代码::
控制器用户:
JlCompress::compressDir(outputFile, inputDir, true);
模型user_model:
<?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>";
}
}
}
?>
以下是三页的三个视图: 第1页(createfinal.php):
<?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');
}
}
?>
第2页(personalfinal.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>
第3页(socialfinal.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>
错误图片: [1]:http://i.stack.imgur.com/izsjY.png
答案 0 :(得分:0)
在您的用户控制器中尝试调整插入方法,如下所示。
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>";
}
答案 1 :(得分:0)
我发现了错误。实际上我试图在我的旧数据库表中插入数据,其中不包含带有电子邮件的列名。因此,我得到了错误。非常感谢你的宝贵帮助。 :)