dqx13503925528 2014-10-05 00:08
浏览 237
已采纳

注意:未定义的索引:第3行的C:\ xampp \ htdocs \ libsys \ edit.php中的id

I know this is a newbie question but this error keeps bugging me for a while now please help ty

<?PHP
include "configdb.php";
$id = $_POST['id'];

if (isset($id))
{
    $newName = $id;
    $sql = 'SELECT * FROM booklist WHERE id = $id';
    $res = mysqli_query($connect, $sql) or die("Could not update".mysql_error());
    $row = mysqli_fetch_row($res);
    $id = $row[0];
    $title = $row[1];
    $author = $row[2];
    $ISBN = $row[3];
    $category = $row[4];
    $image_upload = $row[5];
    $image_upload2 = $row[6];

}
?>
  • 写回答

2条回答 默认 最新

  • dongyan5815 2014-10-05 00:12
    关注

    It is not clear but I believe you should move the $id = $_POST['id'] inside your if statement

    and the code should be:

    <?PHP
    include "configdb.php";
    
    
    if (isset($_POST['id']))
    {
        $id = $_POST['id'];
        $sql = "SELECT * FROM `booklist` WHERE `id` = '$id'";
        $res = mysqli_query($connect, $sql) or die("Could not update".mysql_error());
        $row = mysqli_fetch_row($res);
        $id = $row[0];
        $title = $row[1];
        $author = $row[2];
        $ISBN = $row[3];
        $category = $row[4];
        $image_upload = $row[5];
        $image_upload2 = $row[6];
    }
    ?>
    

    EXTRA INFO

    If what I believe this question is about getting a book id from a book list, then run a query in db to return the results then a better code will be something like this:

    <?PHP
    include "configdb.php";
    
    
    if (isset($_POST['id']))
    {
      $id = $_POST['id'];
      $sql = mysqli_query($connect, "SELECT * FROM booklist WHERE id = $id") or die("Could not update".mysql_error());
      $rows = mysqli_num_rows($sql); // get the number of returning rows (0 means no query match found, > 0 means a query match found)
      if($rows > 0)
      {
        while($i = mysqli_fetch_assoc($sql))
        {
          $book_id        = $sql[0];
          $book_title     = $sql[1];
          $author         = $sql[2];
          $ISBN           = $sql[3];
          $category       = $sql[4];
          $image_upload   = $sql[5];
          $image_upload2  = $sql[6];
    
          // the rest of your code and what you want to do with the result....
        }
      }
      eles // no results found message
      {
      echo 'Sorry, no results found.';
      } // end of else 
    }
    ?>
    

    Also please note that you can use the column name instead of $sql[0], $sql[1], $sql[3]

    so if your first table columns has a name of book_id then it can be

    $book_id = $sql['book_id']; instead of $book_id = $sql[0];. This much easier to deal with when you work in your code so you don't have to go back and fourth to your db to check indexes of columns. also, it will help you a lot when you update your code or share it with others.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划