简体   繁体   English

调用选择当前用户登录项的用户名

[英]call the username that select item for current user login

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 但是,问题是我只能设置为一个用户,如您在save.php的第9行中所见

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, 问题是因为登录使用的是yii2框架,而此select事件使用的是html,而事件的模型使用的是gii,

Here are my code: 这是我的代码:

The code for viewevents.php 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 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);
       }
  }

I suggest you getting some information about 我建议您获得一些有关

  • Object Oriented Programming 面向对象编程
  • MVC structure MVC结构
  • Namespacing 命名空间
  • Active Record 活动记录

And getting a little bit familier with yii2 and its documentation. 并熟悉yii2及其文档。

This code is pure PHP, in frameworks such as Yii2, there is no such things as including files or connecting to databases like that. 这段代码是纯PHP的,在诸如Yii2之类的框架中,没有诸如包含文件或连接到数据库之类的东西。

Take a close look at Yii2 documentation from This Url and try to code using the concepts available under Yii2 structure. 请仔细阅读This Url中的Yii2文档,并尝试使用Yii2结构下可用的概念进行编码。 Make sure you get familier with this concepts and try to forget old school native PHP programming. 确保您熟悉此概念,并尝试忘记老式的本地PHP编程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM