繁体   English   中英

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

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

我有一个用户可以登录的系统,可以选择事件并将其保存到数据库中。

但是,问题是我只能设置为一个用户,如您在save.php的第9行中所见

所以我的问题是,如何将系统中的用户设置为自动,以便在其他用户登录时,数据将以其名称保存。 问题是因为登录使用的是yii2框架,而此select事件使用的是html,而事件的模型使用的是gii,

这是我的代码:

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

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

我建议您获得一些有关

  • 面向对象编程
  • MVC结构
  • 命名空间
  • 活动记录

并熟悉yii2及其文档。

这段代码是纯PHP的,在诸如Yii2之类的框架中,没有诸如包含文件或连接到数据库之类的东西。

请仔细阅读This Url中的Yii2文档,并尝试使用Yii2结构下可用的概念进行编码。 确保您熟悉此概念,并尝试忘记老式的本地PHP编程。

暂无
暂无

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

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