codeigniter data is not insert into database - php

hello Everyone i am new in codeigniter i was try to insert data in database but not work properly.but not insert data in database.I was activate helper and auto load is i was write.
my controller
public function save() {
if($this->input->post('submit')) {
$this->Checkout_model->process();
}
}
my model
function process() {
$name = $this->input->post('name');
$phone = $this->input->post('phone');
$email = $this->input->post('email');
$address = $this->input->post('address');
$data=array (
'name' => $name,
'phone' => $phone,
'email' => $email,
'address' => $address
);
$this->db->insert('customers',$data);
}

Use this code In Controller
public function save() {
if($this->input->post('submit')) {
$name = $this->input->post('name');
$phone = $this->input->post('phone');
$email = $this->input->post('email');
$address = $this->input->post('address');
$data=array (
'name' => $name,
'phone' => $phone,
'email' => $email,
'address' => $address
);
$this->Checkout_model->process($data);
}
}
In Model
function process($data) {
$this->db->insert('customers',$data);
}

Related

PHP UPDATE IN CODEIGNETER 3

I want to change the database, but my coding deletes all its contents.
I use codeigneter 3
my controller:
public function update_() {
$id = $this->input->post('id');
$nama = $this->input->post('nama');
$alamat = $this->input->post('alamat');
$number = $this->input->post('number');
$rekening = $this->input->post('rekening');
$email = $this->input->post('email');
$data = array(
'nama' => $nama,
'alamat' => $alamat,
'number' => $number,
'rekening' => $rekening,
'email' => $email,
);
$where = array (
'id' => $id
);
$this->Modellaptop->update_client($where, $data, 'tb_invoice ');
$this->session->set_flashdata('message',
'<div class="alert alert-success" role="alert">Update success </div>'
);
redirect('Admin/invoice');
}
my model:
public function update_client($where,$data,$table)
{
$this->db->where($where);
$this->db->update($table, $data);
}
maybe you can try like this
in controller
public function update_() {
$id = $this->input->post('id');
$nama = $this->input->post('nama');
$alamat = $this->input->post('alamat');
$number = $this->input->post('number');
$rekening = $this->input->post('rekening');
$email = $this->input->post('email');
$data = [
'nama' => $nama,
'alamat' => $alamat,
'number' => $number,
'rekening' => $rekening,
'email' => $email
];
$where = array (
'id' => $id
);
$this->Modellaptop->update_client($where, $data);
$this->session->set_flashdata('message',
'<div class="alert alert-success" role="alert">Update success </div>'
);
redirect('Admin/invoice');
}
in model
public function update_client($where,$data = [])
{
$this->db->where($where);
$this->db->update('tb_invoice(your table name)', $data);
//dont forget to return
return $this->db->affected_rows();
}

How to use a request field to save new a variable with that value?

So, I have a function that creates me alerts based on changes made on a profile.And I need to have customizable alerts for every field, like name, city, email etc.Now, I need to make this working and I also need to show me an alert like ... User x changed his name to y, or User z changed his email to w.
Here is my controller
public function generalinfo(Request $request)
{
if ( $user = Sentinel::getUser() ) {
if ( $user->inRole('individuals') ) {
$data = $this->data;
$id = $user->id;
$user = User::findOrfail($id);
$generalinfo = User::firstOrNew(array('email' => $user->email ));
$dob = date("Y-m-d", strtotime($request->get('dob')));
$generalinfo->name = $request['name'];
$generalinfo->city = $request['city'];
$generalinfo->address = $request['address'];
$generalinfo->email = $request['email'];
$generalinfo->website1 = $request['website1'];
$generalinfo->website2 = $request['website2'];
$generalinfo->website3 = $request['website3'];
$generalinfo->save();
$feed = \App\Feed::create([
'user_id' => $id,
'feed_url' => $request['rss'],
'active' => 1
]);
$generalinfo = User::find($id);
$generalinfo->name = 'NewName';
// other $user changes
foreach ($generalinfo->getDirty() as $key => $attribute) {
$log = \App\Log::create([
'user_id' => $id,
'log' => " {$key} changed to {$user->user}.",
'date' => time(),
'type' => 'profile_updates',
'formatted_date' => date_format(date_create(date('Y-m-d')),"F dS, Y"),
'action_user_id' => $id,
'action_name' => $user->name,
'action_username' => $user->username,
'profile_picture' => $user->profile_picture,
]);
}
$generalinfo->save();
}
}
}
This is my problem : $generalinfo->name = 'NewName'; .I need to get the new value from database from $user->name. The problem is, I tried to use this : $generalinfo->name = $request['name']; but with no effect.

Wordpress insert new user not adding in backend

When signing up on my dev site, a bug is here that i cant seem to figure out. I am allowed to pass the validation when signing up. But when i use the submit button to create the account, it seems to not want to log any information into the database. I get the logged in message but that's it, i can just recreate the
same over ad over again.
add_action( 'wp_ajax_nopriv_ajaxregister', 'ajax_register' );
add_action( 'wp_ajax_ajaxregister', 'ajax_register' );
function ajax_register() {
$title = $_POST['titles'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['user_email'];
$user_pass = $_POST['user_pass'];
$confirm_pass = $_POST['confirm_pass'];
$agree_tc = $_POST['agree_tc'];
$nationality = $_POST['nationality'];
$username = $_POST['user_email'];//strtolower($firstname) . '-' . strtolower($lastname);
$phone = '+' . $_POST['country_code'] . $_POST['phone'];
//$address = $_POST['address'];
if ($user_pass != $confirm_pass || !is_email($email) || $agree_tc != 'true') {
echo json_encode(array('loggedin' => false, 'message' => __('There are some errors')));
} else {
$user = array(
'user_login' => $username,
'user_pass' => $user_pass,
'first_name' => $firstname,
'last_name' => $lastname,
'user_email' => $email,
'role' => 'client'
);
$user_id = wp_insert_user($user);
if (is_wp_error($user_id)) {
$error_string = $user_id->get_error_message();
echo json_encode(array('loggedin' => false, 'message' => $error_string));
} else {
update_user_meta($user_id, 'user_num', $phone);
update_user_meta($user_id, 'user_nat', $nationality);
update_user_meta($user_id, 'display_titles',$title);
//update_user_meta($user_id, 'user_address', $address);
$creds = array(
'user_login' => $username,
'user_password' => $user_pass
);
$user_signon = wp_signon($creds, false);
echo json_encode(array('loggedin' => true, 'message' => __('Login successful, redirecting...')));
}
}
die();
}
EDIT .
The markup was perfectly fine, however after going through my repo of changes. The display titles was not getting parsed.
function ajax_register() {
$title = $_POST['titles'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['user_email'];
$confirm_email = $_POST['confirm_email'];
$user_pass = $_POST['user_pass'];
$confirm_pass = $_POST['confirm_pass'];
$agree_tc = $_POST['agree_tc'];
$nationality = $_POST['nationality'];
$username = $_POST['user_email'];
$phone = '+' . $_POST['country_code'] . $_POST['phone'];
if ($user_pass != $confirm_pass || !is_email($email) || !is_numeric($_POST['phone']) || $agree_tc != 'true') {
echo json_encode(array('loggedin' => false, 'message' => __('There are some errors')));
} else {
$user = array(
'user_login' => $username,
'user_pass' => $user_pass,
'first_name' => $firstname,
'last_name' => $lastname,
'user_email' => $email,
'display_titles'=>$title, // THIS WAS MISSING
'role' => 'client'
);
$user_id = wp_insert_user($user);
if (is_wp_error($user_id)) {
$error_string = $user_id->get_error_message();
echo json_encode(array('loggedin' => false, 'message' => $error_string));
} else {
update_user_meta($user_id, 'user_num', $phone);
update_user_meta($user_id, 'user_nat', $nationality);
update_user_meta($user_id, 'display_titles',$title);
//update_user_meta($user_id, 'user_address', $address);
$creds = array(
'user_login' => $username,
'user_password' => $user_pass
);
$user_signon = wp_signon($creds, false);
echo json_encode(array('loggedin' => true, 'message' => __('Login successful, redirecting...')));
}
}
die();
}

Fetching Form Data Values using OOP

I am trying to create a Script to fetch the values of form data through OOP, I am not able to return and print the values. Here's my code.
I want to create dynamic Validations too, Using some dynamic PHP code and OOP using arrays.
this is my signup.php file
<?php
include('inc/form-data.class.php');
$signup_data = new FetchUserData;
if(isset($_POST['signup']))
{
$signup_data->FetchSignup('fname_prefix', 'fname', 'uname', 'key',
'ckey', 'gender', 'pnumber', 'country',
'state', 'city', 'zpcode', 'website',
'agreement');
}
?>
This is inc/form-data.class.php
<?php
class FetchUserData
{
private $fname_prefix,
$fname,
$uname,
$key_pass,
$ckey,
$gender,
$pnumber,
$country,
$state,
$city,
$zpcode,
$website,
$agreement,
$values;
public function SignupValuesReturn()
{
return $this->value;
}
public function FetchSignup($fname_prefix, $fname, $uname, $key, $ckey,
$gender, $pnumber, $country, $state, $city,
$zpcode, $website, $agreement)
{
$this->fname_prefix = mysql_real_escape_string($_POST["$fname_prefix"]);
$this->fname = mysql_real_escape_string($_POST["$fname"]);
$this->uname = mysql_real_escape_string($_POST["$uname"]);
$this->key_pass = mysql_real_escape_string($_POST["$key"]);
$this->ckey = $_POST["$ckey"];
$this->gender = mysql_real_escape_string($_POST["$gender"]);
$this->pnumber = mysql_real_escape_string($_POST["$pnumber"]);
$this->country = mysql_real_escape_string($_POST["$country"]);
$this->state = mysql_real_escape_string($_POST["$state"]);
$this->city = mysql_real_escape_string($_POST["$city"]);
$this->zpcode = mysql_real_escape_string($_POST["$zpcode"]);
$this->website = mysql_real_escape_string($_POST["$website"]);
$this->agreement = mysql_real_escape_string($_POST["$agreement"]);
$this->values = array(
'fname_prefix' => $this->fname_prefix,
'fname' => $this->fname,
'uname' => $this->uname,
'key_pass' => $this->key_pass,
'ckey' => $this->ckey,
'gender' => $this->gender,
'pnumber' => $this->pnumber,
'country' => $this->country,
'state' => $this->state,
'city' => $this->city,
'zpcode' => $this->zpcode,
'website' => $this->website,
'agreement' => $this->agreement
);
return $this->values;
}
}
?>
You had an "s" missing from $this->values:
public function SignupValuesReturn()
{
return $this->values;
}

Insert data Restful API Phil Sturgeon

Can anyone show me how to insert data to mysql by using Phil Sturgeon Restful sever?
It's OK when I get data using GET method, but when I tried insert using POST method to insert data it allways echo like this:
{"status":false,"error":"Unknown method."}
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/REST_Controller.php';
class Api extends REST_Controller
{
public function __construct()
{
parent::__construct();
$this->load->Model("user_model");
}
function register_post() {
$username = $this->post('username');
$password = $this->post('password');
$firstname = $this->post('firstname');
$lastname = $this->post('lastname');
$mobile = $this->post('mobile');
$sex = $this->post('sex');
$avatar = $this->post('avatar');
$active = $this->post('active');
$device_token = $this->post('device_token');
$fbconnected = $this->post('fbconnected');
$token = $this->post('token');
$register = array(
'username' => $username,
'password' => MD5('".$password."'),
'firstname' => $firstname,
'lastname' => $lastname,
'mobile' => $mobile,
'sex' => $sex,
'avatar' => $avatar,
'active' => $active,
'device_token' => $device_token,
'fbconnected' => $fbconnected,
'token' => $token
);
$this->user_model->register($register);
//$this->response($register, 200); // 200 being the HTTP response code
}
}
?>

Categories