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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。