2014-10-05 37 views
0

我知道這是一個新手的問​​題,但這個錯誤一直纏着我有一段時間了,請幫助TY注意:未定義指數:ID在C: XAMPP htdocs中 LIBSYS edit.php上線3

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

} 
?> 
+0

您的'$ _POST'數組沒有索引爲'id'的元素。通常你應該首先檢查if(isset($ _ POST ['id']))$ id = intval($ _ POST ['id']); else $ id = false;'和後面的'if($ id)..'並閱讀SQL注入 – Cheery 2014-10-05 00:13:45

回答

1

目前尚不清楚,但我相信你應該移動$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]; 
} 
?> 

EXTRA信息

如果我認爲這個問題是有關從書單讓書籍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];。當你在你的代碼中工作時,這很容易處理,所以你不必回頭查看你的數據庫來檢查列的索引。當你更新你的代碼或與他人分享時,它也會幫助你很多。

+1

爲什麼?如果在$ if語句之前不會分配$ id。 – 2014-10-05 00:15:26

+0

@ZeeTee,在他的代碼中,他定義了'$ newName = $ id;'它在查詢中的任何地方都沒有使用。我相信他有一個書籍清單表格,從這個表格中他可以得到一個在他的查詢中使用的書籍ID並獲得結果。 – SULTAN 2014-10-05 00:20:35

+0

實際上,你在SULTAN上的位置就是我想要做的。 – 2014-10-05 00:23:26

1

您的SQL語句不檢查表單中的數字ID值,而是檢查字符串$id。需要改變:

'SELECT * FROM booklist WHERE id = '. $id 
+0

甚至沒有「字符串」,因爲它沒有被引用。這只是無效的SQL。這不是提到的錯誤的原因,但它是值得注意的。 – cHao 2014-10-05 00:15:15

+0

與TS有什麼不同? – Cheery 2014-10-05 00:16:29

+0

@Cheery:這個問題有一個單引號的字符串。 '$ id'不會被內插。 – cHao 2014-10-05 00:17:31

相關問題