编辑-我尝试将数据存储在会话值中,并将其作为参数传递给模型以进行处理,但这也不起作用。
好吧,在您拒绝投票之前,请听我说。我有一个HTML表单来获取用户评论。用户登录其Google帐户后可以发表评论。我为此使用了Google 0Auth 2.0 API。因此,当您单击表单上的提交时,您将被重定向到Google身份验证页面,您可以在其中选择您要使用的帐户。
但是,如果您单击“添加另一个帐户”并输入凭据,则我通过原始User Review Form
(title of review
和review content
)获得的数据是“未设置”的。我知道这一点是因为对数据库的最终查询将插入从Google登录信息以及Review Form
输出"review_title" value can't be null
插入的数据。我知道这很令人困惑,但是请相信我,这是我能解释的最好的。
Controller.php
defined('BASEPATH') OR exit('No direct script access allowed');
class Googlelogin extends CI_Controller
{
public function __construct()
{
parent::__construct();
require_once APPPATH . 'third_party/src/Google_Client.php';
require_once APPPATH . 'third_party/src/contrib/Google_Oauth2Service.php';
}
public function index()
{
// $this->load->view('pages/review');
}
public function login()
{
$clientId = 'dddd.googleusercontent.com'; //Google client ID
$clientSecret = 'dddd-ddd'; //Google client secret
$redirectURL = base_url() . 'googlelogin/login';
//https://curl.haxx.se/docs/caextract.html
//Call Google API
$gClient = new Google_Client();
$gClient->setApplicationName('Login');
$gClient->setClientId($clientId);
$gClient->setClientSecret($clientSecret);
$gClient->setRedirectUri($redirectURL);
$google_oauthV2 = new Google_Oauth2Service($gClient);
if (isset($_GET['code'])) {
$gClient->authenticate($_GET['code']);
$_SESSION['token'] = $gClient->getAccessToken();
header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken()) {
$userProfile = $google_oauthV2->userinfo->get();
// print_r($userProfile);
$name = $userProfile['name'];
$email = $userProfile['email'];
$picture = $userProfile['picture'];
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'title');
$this->form_validation->set_rules('message', 'message');
$title = $this->input->post('title');
$message = $this->input->post('message');
// MY SESSION SOLUTION
$this->session->set_userdata($array);
$session_title = $this->session->userdata('title');
$session_message = $this->session->userdata('message');
print_r($this->input->post());
if ($this->form_validation->run() === FALSE) {
echo "error in your form ";
$this->session->set_flashdata('review_error', 'Please fill all the fields!');
$this->load->view('pages/review');
// redirect(base_url().'review/');
} else {
echo $title, $message;
$this->session->set_flashdata('review_success', 'Thank you for you ');
$this->review_model->set_review($name, $email, $picture, $session_title , $session_message);
redirect(base_url() . 'review/');
}
// echo "just dies";
die;
} else {
$url = $gClient->createAuthUrl();
header("Location: $url");
exit;
}
}
}
我在login
方法中遇到错误。在FormValidation
条件内。
Model.php
class Review_model extends CI_Model
{
public function set_review($google_name, $google_email, $google_image, $title, $message)
{
$data = array('review_title' => $title,
'review_content' => $message,
'email' => $google_email,
'name' => $google_name,
'image_url' => $google_image);
return $this->db->insert('reviews', $data);
}
public function get_review(){
$query = $this->db->get('reviews');
return $query->result_array();
}
}
模型给出的实际输出为"review_title" cannot be NULL
。
如果有人感兴趣,这里是表格。
Form.php
<form action="<?php echo base_url(); ?>review/submit/" method="post">
<input type="hidden" name='<?php echo $this->security->get_csrf_token_name(); ?>'
value='<?php echo $this->security->get_csrf_hash(); ?>'>
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<!-- <input type="text" name="title">-->
<?php echo form_input(['name' => 'title', 'class' => 'form-control', 'placeholder' => 'Enter your review title']); ?>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Review</label>
<!-- <textarea rows="10" cols="100" name="message"></textarea>-->
<?php echo form_textarea(['name' => 'message', 'class' => 'form-control', 'placeholder' => 'Enter your review title', 'rows' => '10', 'cols' => '100']); ?>
</div>
<?php echo form_submit(['name' => 'submit', 'value' => 'submit']) ?>
</form>