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

注意:未定义的索引:第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];

}
?>

图片转代码服务由CSDN问答提供 功能建议

我知道这是一个新手问题,但是这个错误一直困扰着我一段时间请帮助ty

 &lt;?PHP 
include“configdb.php”; 
 $ id = $ _POST ['id']; 
 
if(isset($ id))
 
 \  n $ newName = $ id; 
 $ sql ='SELECT * FROM booklist WHERE id = $ id'; 
 $ res = mysqli_query($ connect,$ sql)或die(“无法更新”.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]; 
 
} 
?n&gt; \  n   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论
  • dsgdg46465 2014-10-05 00:12

    Your SQL Statement is not checking a numeric ID value from your form, instead it's checking the string $id. Need to change that:

    'SELECT * FROM booklist WHERE id = '. $id
    
    打赏 评论