douyanguo7964 2019-05-19 11:14
浏览 76
已采纳

Php和Sql,未定义的未定义索引[重复]

I was trying to complete and exercise but i got this 2 errors:

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

usually i know how to solve the undefined index error, but this time I have already tried to change the syntax and check the tables/querys but to me everything seems correct.

$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'];

Tables 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 |
+-------------+------+---------------------+

recensione:

+---------------+---------+-------------+--------------+
| Id_recensione | Titolo  | valutazione | Cod_sessione |
+---------------+---------+-------------+--------------+
|             1 | Avenger |           3 |         NULL |
|             2 | Titanic |           4 |            4 |
+---------------+---------+-------------+--------------+
</div>
  • 写回答

1条回答 默认 最新

  • doujuanju3076 2019-05-19 11:17
    关注

    You need proper column alias in query and use the column alias as index

    eg max_id and 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'];
    

    anyway you should not use PHP var in SQL query .. you are at risk for sqlinjection .. for avoid this you should take a look at your db driver for prepared statement and binding param

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助