php - 如何解决注意: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)
问题是什么?
解决方案
在您的 HTML 中,您的“意见”字段不正确。你打开一个<input>
但关闭</textarea>
推荐阅读
- qt - QtCreator:无法运行编译器“g++”。输出:
- javascript - 在网页运行时如何更改 CodeMirror 设置?
- javascript - 如何用html标签替换字符串
- ios - 如何向 iOS WebApp (PWA) 发送推送通知?
- r - 使用 ggplot2 facet 生成分组相关矩阵热图时无法更改子图背景
- php - 从控制器获取变量以查看
- javascript - Webpack4 如何直接从 HTML 文件加载图像和自定义 javascript?
- javascript - 使用 Discord.js 每 1 分钟发送一次随机消息
- javascript - 从 JSON 字符串动态构建可折叠 UL 的 jQuery 代码
- c# - ListView 的索引在清除它之前是 -1