php - call the username that select item for current user login
Get the solution ↓↓↓I have a system that user can login, and can select the event and will save into the database.
However, the problem is that i can set to only one user, as you can see in line 9 in the save.php
So my question is, how to set the user in the system to be automatic so that if other user login, the data will be save under their name. the problem is because the login is using yii2 framework and this select event is using html and the model of the event is using gii,
Here are my code:
The code for viewevents.php
<?php
echo '<link href="../../css/style.css" rel="stylesheet"/>';
$event = get_events();
function get_events(){
include ("../config/connect.php");
$sql = "SELECT * FROM `events` order by event_id desc;";
if ($result = $dayahlatiff->query($sql)) { $in = 0;
while ($row = $result->fetch_assoc()) {
$new_res[$in] = $row;
$in++;
}
$result->free();
}
return isset($new_res)?$new_res:array();
}
$this->title = 'View Events';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $in = 1;
if (!empty($event)) {
include 'events.php';
}
?>
The code for save.php
<?php
session_start();
if(isset( $_SESSION["id_"])){
// $hh = $_SESSION["id_"];
// die($hh);
}
require("../../../vendor/yiisoft/yii2/Yii.php");
$user = "dayahlatiff";
//$user = Yii::$app->user->identity->username;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
if(isset($_POST["save"])){
include ("../../config/connect.php");
$where = $_POST["where"];
$list = $_POST["check_list"];
if(empty($list)){
header("Location: ".$where);
}else {
$done = true;
for($s = 0; $s<sizeof($list); $s++){
$event = $list[$s];
$sql = "INSERT INTO `selectevents` (`user_id`, `username`, `event_title`) VALUES (NULL, '$user', '$event');";
if($dayahlatiff->query($sql) === FALSE){
$event = false;
}
}
header("Location: ".$where);
}
}
Answer
Solution:
I suggest you getting some information about
- Object Oriented Programming
- MVC structure
- Namespacing
- Active Record
And getting a little bit familier with yii2 and its documentation.
This code is pure PHP, in frameworks such as Yii2, there is no such things as including files or connecting to databases like that.
Take a close look at Yii2 documentation from This Url and try to code using the concepts available under Yii2 structure. Make sure you get familier with this concepts and try to forget old school native PHP programming.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.