Php和Sql,未定义的未定义索引[重复]
Posted
技术标签:
【中文标题】Php和Sql,未定义的未定义索引[重复]【英文标题】:Php and Sql, undefined index when it's not undefined [duplicate] 【发布时间】:2019-05-19 11:14:41 【问题描述】:我试图完成和锻炼,但我得到了这 2 个错误:
Notice: Undefined index: Id_sessione in C:\xampp\htdocs\Challenge\Recensione.php on line 24
Notice: Undefined index: Cod_sessione in C:\xampp\htdocs\Challenge\Recensione.php on line 25
通常我知道如何解决未定义的索引错误,但这次我已经尝试更改语法并检查表/查询,但对我来说一切似乎都是正确的。
$Titolo = $_POST['Titolo'];
$Rate = $_POST['Valutazione'];
$query = "Select Titolo From recensioni where Titolo = '$Titolo' limit 1";
$query2 = "Select MAX(Id_sessione) from sessione";
$query3 = "Select MAX(Cod_sessione) from recensioni";
$result = $conn->query($query);
$result2 = $conn->query($query2);
$result3 = $conn->query($query3);
$row = mysqli_fetch_assoc($result);
$row2 = mysqli_fetch_assoc($result2);
$row3 = mysqli_fetch_assoc($result3);
$controllaTitolo = $row['Titolo'];
$Sessione = $row2['Id_sessione'];
$controllaSessione = $row3['Cod_sessione'];
表格 会议:
+-------------+------+---------------------+
| Id_sessione | Nome | TimeA |
+-------------+------+---------------------+
| 1 | zhao | 2019-05-17 13:04:19 |
| 2 | zhao | 2019-05-18 18:07:24 |
| 3 | zhao | 2019-05-18 18:47:20 |
| 4 | zhao | 2019-05-19 11:49:54 |
+-------------+------+---------------------+
评论:
+---------------+---------+-------------+--------------+
| Id_recensione | Titolo | valutazione | Cod_sessione |
+---------------+---------+-------------+--------------+
| 1 | Avenger | 3 | NULL |
| 2 | Titanic | 4 | 4 |
+---------------+---------+-------------+--------------+
【问题讨论】:
警告:您对SQL Injections 持开放态度,应该真正使用参数化Prepared Statements,而不是像那样手动构建查询。特别是因为您根本没有逃避用户输入! 【参考方案1】:您需要在查询中使用适当的列别名并将列别名用作索引
例如max_id
和max_cod
Titolo = $_POST['Titolo'];
$Rate = $_POST['Valutazione'];
$query = "Select Titolo From recensioni where Titolo = '$Titolo' limit 1";
$query2 = "Select MAX(Id_sessione) max_id from sessione";
$query3 = "Select MAX(Cod_sessione) max_cod from recensioni";
$result = $conn->query($query);
$result2 = $conn->query($query2);
$result3 = $conn->query($query3);
$row = mysqli_fetch_assoc($result);
$row2 = mysqli_fetch_assoc($result2);
$row3 = mysqli_fetch_assoc($result3);
$controllaTitolo = $row['Titolo'];
$Sessione = $row2['max_id'];
$controllaSessione = $row3['max_cod'];
无论如何,你不应该在 SQL 查询中使用 PHP var .. 你有 sqlinjection 的风险 .. 为避免这种情况,你应该查看你的数据库驱动程序以获取准备好的语句和绑定参数
【讨论】:
以上是关于Php和Sql,未定义的未定义索引[重复]的主要内容,如果未能解决你的问题,请参考以下文章