Fetching Form Data Values using OOP - php

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;
}

Related

In php unit getting error Call to a member function prepare() on null

I am learning phpunit and trying to do test on my project using phpunit. I keep on getting this error (app\tests\UserTest::testUser Error: Call to a member function prepare() on null") whenever i test the below Usermodel function(reg_user). Also please find the testcode below as well.
public function reg_user($data)
{
// $this->firstName = trim($data['firstname']);
$this->set_first_name(trim($data['firstname']));
$this->lastName = trim($data['lastname']);
$this->email = trim($data['email']);
$this->userName = trim($data['username']);
$this->password = trim($data['password']);
$hash = $this->encrypt($this->password);
$this->roleId = trim($data['roleid']);
$security = new Security();
$token = $security->emailToken();
if ($this->check_user_exist($this->userName)) {
$this->error[] = "username already exist";
} elseif ($this->check_user_exist($this->userName) < 1) {
$sql = "INSERT INTO User ( firstname, lastname, email, username, passwords, roleid, token, emailverify ) VALUES ( :firstname, :lastname, :email, :username, :passwords, :roleid, :token, :emailverify )";
$query = $this->conn->pdo->prepare($sql);
$result = $query->execute(array(':firstname' => $this->get_first_name(), ':lastname' => $this->lastName, ':email' => $this->email, ':username' => $this->userName, ':passwords' => $hash, ':roleid' => $this->roleId, ':token' => $token, ':emailverify' => 0));
// send email
$this->send_email($data['email'], $token, $data['firstname']);
if ($result) {
$this->success[] = "registerd sussecfull";
return $result;
}
}
}
below is my test function to test the above function
class UserTest extends TestCase
{
protected $database;
protected $conn;
protected $handler;
public function testUser()
{
$user = new User();
$security = new Security();
$data = [
'firstname' => 'Jane',
'lastname' => 'Doe',
'email' => 'janedoe#gmail.com',
'username' => 'wwedew',
'password' => password_hash('password', PASSWORD_BCRYPT, ['cost' => 10]),
'roleid' => 2,
'token' => $security->emailToken(),
'emailverify' => 0
];
$create = $user->reg_user($data);
$this->assertTrue($create);
}
}
any advice would be usefull what i am doing wrong. I have configured the phpunit.xml file by following the documentation.

"No provider registered " error occure using geocode packages in laravel

https://github.com/geocoder-php/GeocoderLaravel/blob/master/config/geocoder.php I cant get pass this point. 1. this is my Job
public $model;
public function __construct($model)
{
$this->model = $model;
}
public function handle()
{
$result = new Geocoder();
$result->geocode($this->model->getAddressString());
// $result = Geocoder::geocode($this->model->getAddressString());
$this->model->setCoordinates($result->getLatitude(), $result->getLongitude());
}
}
I followed the instructions in the repo readme to install geocoder into my project.
2. this is my controller
On trying to get Langitude and latitude from address , i get the no provider registered error..
protected function create(array $data)
{
$user = User::create([
'fname' => $data['fname'],
'lname' => $data['lname'],
'email' => $data['email'],
'zipcode' => $data['zipcode'],
'address_latitude' => $data['address_latitude'],
'address_longitude' =>$data['address_longitude'],
'password' => Hash::make($data['password']),
'gender' => $data['gender'],
'dob' => $data['dob'],
'health' => $data['health'],
]);
//$this-> dispatch(new GeocodePartnerAddress($user));
$this->dispatch(new GeocodeAddress($user));
}
On trying to get Langitude and latitude from address , i get the no provider registered error..
I followed the instructions in the repo readme to install geocoder into my project.
I get "No provider registered." this error if you done this problem please give me solution where i am wrong.
public function handle()
{
$users = User::whereNull('lat')->whereNull('lng')->whereNull('city')->whereNull('state')->whereNull('address')->get();
foreach ($users as $user)
{
$response = Geocode::make()->address($user->zipcode);
if ($response){
$lat = $response->latitude();
$lng = $response->longitude();
$city = $response->raw()->address_components[1]->long_name;
$state = $response->raw()->address_components[2]->long_name;
$address = $response->formattedAddress();
echo $response->locationType();
DB::table('users')->where('id', $user->id)->update(['lat' => $lat, 'lng' => $lng, 'city' => $city, 'state' => $state, 'address'=> $address]);
}
}exit;
}

Laravel insert into new form, with existing users

I've created a new form for existing users.
And i'm trying to insert the new values into the database, but the values wont save.
public function editUser(Request $request, $id, $check)
{
$curID = $id;
$userData = $request->input();
$curUser = User::Where('id', $curID)->first();
$address = Address::where('id', $userData['address_id']);
------Not working----------------
if(empty($curUser->$address)){
$curUser->insert($address)([
'address_id' => $address->id,
'region_id' => $userData['region'],
'country_id' => $userData['country'],
'city_id' => $userData['city'],
'street_name' => $userData['street_name'],
'house_number' => $userData['house_number'],
'postcode' => $userData['postcode']
]);
-------Not working----------------
} else{
$address->update([
'region_id' => $userData['region'],
'country_id' => $userData['country'],
'city_id' => $userData['city'],
'street_name' => $userData['street_name'],
'house_number' => $userData['house_number'],
'postcode' => $userData['postcode'],
]);
return redirect('client')->with($this->messageBag, "User edited!");
}
public function editUser(Request $request, $id, $check)
{
$user = User::where('id',$id)->first();
$address = Address::where('id',$request->input('address_id'))->first();
if(empty($address)){
$address=new Address();
}
$address=$this->saveData($address,$request()->all());
$user->update(['address_id'=>$address->id]);
return redirect('client')->with($this->messageBag, "User edited!");
}
protected function saveData(Address $address,array $userData){
$address->region_id= $userData['region'];
$address->country_id= $userData['country'];
$address->city_id= $userData['city'];
$address->street_name= $userData['street_name'];
$address->house_number= $userData['house_number'];
$address->postcode= $userData['postcode'];
$address->save();
return $address;
}

codeigniter data is not insert into database

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);
}

retrive primary_key of just inserted data in codeigniter

I just added the data in mysql with CI query builder. Now I need to retrive the primary key(Track Code) of those data(row). I used insert and select query simultanously but it didn't worked.
Model:
public function complainReg($cName,$vName,$Email,$Contact,$date,$Complain,$ip)
{
$data = array(
'cName' => $cName,
'vName' => $vName,
'Email' => $Email,
'Contact' => $Contact,
'Date' => $date,
'Complain' => $Complain,
'ip' => $ip
);
$sql= $this->db->set($data)->get_compiled_insert('tbl_complain');
$q=$this->db->query($sql);
return $q;
}
Controller :
public function index()
{
if (isset($_POST['btnRegister']))
{
$cName=$this->input->post('cName');
$vName=$this->input->post('vName');
$Email=$this->input->post('email');
$Contact=$this->input->post('phone');
$date=$this->input->post('Date');
$Complain=$this->input->post('complain');
$ip=file_get_contents("http://ipecho.net/plain");
$this->HamroSamajModel->complainReg($cName,$vName,$Email,$Contact,$date,$Complain,$ip);
$this->session->set_flashdata("message","Your complain has been registered sucessfully");
}
$this->load->view('Complain/index');
}
If you need last Insert ID just try this
public function complainReg($cName,$vName,$Email,$Contact,$date,$Complain,$ip)
{
$data = array(
'cName' => $cName,
'vName' => $vName,
'Email' => $Email,
'Contact' => $Contact,
'Date' => $date,
'Complain' => $Complain,
'ip' => $ip
);
$this->db->insert('tbl_complain', $data);
$lastID = $this->db->insert_id();
return $lastID;
}

Categories