2016-11-23 88 views
0

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

然而,问题是,我可以设置为只有一个用户,你可以在9号线在save.php看到

所以我的问题是,如何设置用户在系统中自动这样如果其他用户登录,数据将被保存在他们的名下。这个问题是因为登录使用yii2框架,这个选择事件被使用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); 
     } 
    } 
+1

这看起来像一些旧的遗留PHP代码被迫使用Yii。你为什么不正确使用Yii 2? – Bizley

+0

@Bizley其实,我试过使用yii2,但它不工作..所以我从我的朋友那里得到帮助,但他不是太高手使用yii2,这就是为什么代码混合yii2和html。但是,html代码仅适用于这两个文件(viewevents.php和save.php) – Fyp16

+0

带'// user = ...'的注释行正是获取已记录的Yii 2用户的用户名的方式,但是我真的怀疑你是否可以像这样在没有任何问题的情况下使用它。除了你的代码是开放给SQL注入的。 Yii 2并不难学。检查[Yii 2指南](http://www.yiiframework.com/doc-2.0/guide-index.html)并正确执行。 – Bizley

回答

0

的代码,我建议你得到一些有关

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

并获得与yii2及其文档有点熟悉并且。

这段代码是纯PHP,在Yii2等框架中,没有像包含文件或连接到数据库那样的东西。

仔细查看来自This Url的Yii2文档,并尝试使用Yii2结构下提供的概念进行编码。确保你熟悉这个概念,并尝试忘记老派原生PHP编程。