php - 注意第 3 行的 : Undefined index: id in C:\xampp\htdocs\libsys\edit. php

标签 php mysql

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

<?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];

}
?>

最佳答案

尚不清楚,但我相信您应该将 $id = $_POST['id'] 移至 if 语句内

代码应该是:

<?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];
}
?>
<小时/>

额外信息

如果我认为这个问题是关于从图书列表中获取图书 ID,然后在数据库中运行查询以返回结果,那么更好的代码将是这样的:

<?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 
}
?>

另请注意,您可以使用列名称代替 $sql[0]、$sql[1]、$sql[3]

因此,如果您的第一个表列的名称为 book_id 那么它可以是

$book_id = $sql['book_id']; 而不是 $book_id = $sql[0];。当您在代码中工作时,这更容易处理,因此您不必返回数据库来检查列的索引。此外,当您更新代码或与他人共享代码时,它也会对您有很大帮助。

关于php - 注意第 3 行的 : Undefined index: id in C:\xampp\htdocs\libsys\edit. php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26198381/

相关文章:

javascript - 根据我数据库中的 id 将数据从 html 对象传递到另一个对象

php - 使用继承访问表的关系

php - 文件开头的意外空格

mysql - SQL导入错误

mysql - 实现嵌套group_concat

php - 如何从一对多关系中使用 SQL 获取平均税率

php - 实时获取多个 exec() 进程的进度

php - $form->createView() 在 Symfony2 中无错误地挂起(失控的内存使用)

c# - 使用 MySqlTransaction 回滚

javascript - 使用 Mysql UNIX_TIMESTAMP() 到 JAVASCRIPT Date.UTC() 获得不同的结果