注意:未定义的索引:第12行的C:\ xampp \ htdocs \ home_teacher_upload.php中的video_code

时间:2017-01-13 20:53:04

标签: php mysql database pdo

嗨,所以我一直遇到这个错误,我想知道如何解决它!这非常重要,因为这个项目是为了学位。

此表单主要用于上传youtube嵌入代码并在另一个页面中调用它们! 我正在使用strip_tags,但似乎它不起作用,因为它表示未定义的变量$ video_title和$ video_code.So这是我第一次打开它时页面的样子:http://imgur.com/a/hTsW7 这是当我点击提交按钮时: http://imgur.com/a/ToVV8 我知道它有重复但我已经尝试了很多这里找到的选项,但似乎没有工作!我试过isset(); ,我试过$ _POST []但没有更好的结果。我也试图改变整个注册和登录表格,但仍然没有! 这些错误是什么以及为什么 我是PHP的新手,很抱歉这个错误的解释!

这是home_teacher_upload.php:

<?php

require_once("session.php");
require_once("class.user.php");

$teacher = new USER();

if(isset($_POST['btn-submit']))
{
   $video_title = strip_tags($_GET['video_title']);
   $video_code = strip_tags($_GET['video_code']);

  if ($video_title == "") {
    $error[]="Duhet të vendosni titullin fillimisht!";
  }
    echo "<h3>Videoja u ngarkua me sukses!</h3>";
  }
   try {
        $stmt = $teacher->runQuery("SELECT video_title, video_code FROM videos WHERE video_title=:video_title OR video_code=:video_code");
      $stmt->execute(array(':video_title'=>$video_title, ':video_code'=>$video_code));
      $row=$stmt->fetch(PDO::FETCH_ASSOC);

      if($row['video_title']==$video_title) {
        echo "<h3>Ky titull është jo valid!</h3>";
      }
      else if($row['video_code']==$video_code) {
        $error[] = "Kjo video aktualisht është e ngarkuar";
      }
      else
      {
        $teacher->submit_video($video_title,$video_code);  
               }
      } 

      catch (PDOException $e) {

          echo $e->getMessage();   

      }





?>
<link href="img/favicon.png" rel="shortcut icon" />
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Cookie">
<link rel="stylesheet" href="css/user.css">
<link rel="stylesheet" href="bootstrap/fonts/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Fjalla+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patua+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Bree+Serif" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Anton" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Hammersmith+One" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="jquery-1.11.3-jquery.min.js"></script>
<link rel="stylesheet" href="style.css" type="text/css"  />
<link rel="stylesheet" type="text/css" href="css/user.css">
<title>Ngarkoni video!</title>
</head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<body>

<nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" style="
          font-family: Bree Serif;">IB-Learning </a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">

            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
              <span class="glyphicon glyphicon-user"></span>&nbsp;&nbsp;<span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="profile.php"><span class="glyphicon glyphicon-user"></span>&nbsp;Profili</a></li>
                <li><a href="logout.php?logout=true"><span class="glyphicon glyphicon-log-out"></span>&nbsp;Dilni</a></li>
              </ul>
            </li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>


    <div class="clearfix"></div>

<div class="container-fluid" style="margin-top:80px;">


</div>
<div class="wanna" style="text-align: center;">
<h3 style="font-family: Hammersmith One;">Ngarkoni një video</h3></div>
<div class="form" style="text-align: center;     margin-left: 431px;
    padding-top: 13px;">
<form class="submit_video" method="POST" style="text-align: center;">

  <div class="form-group" style="font-family: Bree Serif; width: 28%;">
            <input type="text" class="form-control" name="video_title" required placeholder="Titulli i videos" style="text-align: center;"/>
        </div>

  <div class="form-group" style="font-family: Bree Serif;margin-left: -37px;">
            <textarea name="video_code" class="form-control" style="width: 240px; margin: 0px 337px 0px 0px; height: 165px; text-align: center;" form="code" placeholder="Embed code i videos" required="true"></textarea>
        </div>

<div class="button_form-group">
              <button type="submit" class="btn btn-primary" style="margin-right: 415px;font-family: Bree Serif;" name="btn-submit">
                  Submit
                </button>
            </div>
</form>
</div>



<?php include('footer.php');?>
<script src="bootstrap/js/bootstrap.min.js"></script>
<style type="text/css">
  h3{
    font-family: Bree Serif;
    text-align: center;
    padding-left: 20px;
  }
</style>
</body>
</html>

这是session.php:

<?php

    session_start();

    require_once 'class.user.php';
    $session = new USER();

    // if user session is not active(not loggedin) this page will help 'home.php and profile.php' to redirect to login page
    // put this file within secured pages that users (users can't access without login)

    if(!$session->is_loggedin())
    {
        // session no set redirects to login page
        $session->redirect('login.php');
    }

这是class.user.php:

<?php

require_once('dbconfig.php');

class USER
{   

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }


    public function register($uname,$umail,$upass)
    {
        try
        {
            $new_password = password_hash($upass, PASSWORD_DEFAULT);

            $stmt = $this->conn->prepare("INSERT INTO tik_students(user_name,user_email,user_pass) 
                                                       VALUES(:uname, :umail, :upass)");

            $stmt->bindparam(":uname", $uname);
            $stmt->bindparam(":umail", $umail);
            $stmt->bindparam(":upass", $new_password);

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }
    public function register_teacher($t_uname,$t_umail,$t_upass)
    {
        try
        {
            $new_password = password_hash($upass, PASSWORD_DEFAULT);

            $stmt = $this->conn->prepare("INSERT INTO tik_teachers(user_name,user_email,user_pass)
                                                       VALUES(:uname, :umail, :upass)");

            $stmt->bindparam(":uname", $t_uname);
            $stmt->bindparam(":umail", $t_umail);
            $stmt->bindparam(":upass", $t_new_password);                              

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }

    public function doLogin($uname,$umail,$upass)
    {
        try
        {
            $stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM tik_students WHERE user_name=:uname OR user_email=:umail ");
            $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() == 1)
            {
                if(password_verify($upass, $userRow['user_pass']))
                {
                    $_SESSION['user_session'] = $userRow['user_id'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    public function doLogin_teacher($t_uname,$t_umail,$t_upass)
    {
        try
        {
            $stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM tik_teachers WHERE user_name=:uname OR user_email=:umail ");
            $stmt->execute(array(':uname'=>$t_uname, ':umail'=>$t_umail));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() == 1)
            {
                if(password_verify($t_upass, $userRow['user_pass']))
                {
                    $_SESSION['user_session'] = $userRow['user_id'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

        public function submit_video($video_title,$video_code)
    {
        try
        {

            $stmt = $this->conn->prepare("INSERT INTO videos(video_title, video_code) 
                                                       VALUES(:video_title, :video_code)");

            $stmt->bindparam(":video_title", $video_title);
            $stmt->bindparam(":video_code", $video_code);       

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }


    public function is_t_loggedin()
    {
        if(isset($_SESSION['user_session']))
        {
            return true;
        }
    }


    public function is_loggedin()
    {
        if(isset($_SESSION['user_session']))
        {
            return true;
        }
    }
    public function redirect($url)
    {
        header("Location: $url");
    }

    public function doLogout()
    {
        session_destroy();
        unset($_SESSION['user_session']);
        return true;
    }
}

?>

帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

  

注意:未定义的索引/未定义的偏移量

     

当您(或PHP)尝试访问未定义的索引时,会出现此通知   一个数组。

您需要先查看以下内容

if(isset($_GET['video_code'])){

}

所以在你的情况下

$video_code = isset($_GET['video_code']) ? strip_tags($_GET['video_code']): ' ';

在访问索引之前检查索引是否存在。

为此你可以使用

isset()

array_key_exists()

//isset()
$video_code = isset($_GET['video_code']) ? strip_tags($_GET['video_code']): '';

// OR

//array_key_exists()
$video_code = array_key_exists('video_code',$_GET) ? strip_tags($_GET['video_code']): '';