首页 > 解决方案 > 如何解决注意:PHP 中未定义的索引?

问题描述

我试图通过阅读其他帖子来解决这个问题,但是已经过了几天,我仍然被困在这里。我正在尝试发布两个输入,然后将它们存储在我的数据库中。我正在使用使用 PHPMYADMIN 的 000webhost。这是错误:注意:未定义索引:第 11 行 /storage/ssd5/749/14997749/public_html/includes/enviar.inc.php 中的 nombre

注意:未定义索引:第 12 行 /storage/ssd5/749/14997749/public_html/includes/enviar.inc.php 中的意见

这是我的代码

索引.php:

<!DOCTYPE HTML>
<html>
    <head>
        <title>Title goes here</title>
    </head>
    <body>
        <br><br>
        <h1>Welcome</h1>
        <div>
            <h3>text goes here </h3>
        </div>
        <br><br>
        <form action="includes/enviar.inc.php" method="POST">
            <div>
                <label for="nombre">Nombre:</label>
                <input type="text" name="nombre" required>
            </div>
            
            <div>
                <label for="opinion">Opinión:</label>
                <input type="text" name="opinion"></textarea>
            </div>
            
            <button type="submit" name="submit" value="signin">Enviar</button>
        </form>
    </body>
</html>

enviar.inc.php 文件:

<?php

//error_reporting(E_ALL ^ E_NOTICE);

session_start();
require_once '../classes/DataBaseHandler.php';

$dbh = new DataBaseHandler();
$dbh->ConnectToDataBase();

$nombre = $dbh->EscapeString($_POST['nombre']);
$opinion = $dbh->EscapeString( $_POST['opinion']);

$sql = "INSERT INTO opiniones (id, nombre, opinion) VALUES (NULL, '$nombre', '$opinion');";
$dbh->Query($sql);

exit;

数据库处理程序.php:

<?php

class DataBaseHandler {
    
    private $conn = null;
    private $host="localhost";
    private $username="myusername";
    private $password="mypassword";
    private $dbname="mydbname";
    
    public function ConnectToDataBase() {
        if($this->conn == null){
            $this->conn = mysqli_connect($this->host, $this->username, $this->password, $this->dbname);
        }
        else {
            return $this->conn;
        }
    }
    
    public function Query($query) {
        return mysqli_query($this->conn, $query);
    }
    
    public function EscapeString($string) {
        return mysqli_real_escape_string($this->conn, $string);
    }
}

我无法将任何内容上传到数据库,但是如果我将 enviar.inc.php 中的 '$nombre' 和 '$opinion' 替换为 'test1' 和 'test2',它可以完美运行(并删除第 11 行和 12)

问题是什么?

标签: php

解决方案


在您的 HTML 中,您的“意见”字段不正确。你打开一个<input>但关闭</textarea>


推荐阅读