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 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗