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

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题