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 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么