2013-08-22 27 views
-1

即時通訊使用href將參數從一個頁面傳遞給另一個頁面,但卻出現錯誤。 注意:未定義的索引:$ table在/var/www/html/download/get_file.php上線3 錯誤!查詢失敗:使用href在PHP中使用href時出錯

您的SQL語法錯誤;檢查對應於您的MySQL服務器版本的手冊,以便在第3行'WHERE id = 1'附近使用正確的語法。

我想在href中將表名傳遞給其他腳本來執行它,所以對於我有很多桌子。

希望這可以解釋我想要的。

href='get_file.php?id={$row['id']}&$table'>Download 

這裏是我的代碼

<?php 

if(isset($_POST["dropdown"])) 

{ 
$table = $_POST['dropdown']; 
// Connect to the database 
$dbLink = new mysqli('localhost', 'sqldata', 'sqldata', 'balhaf'); 
if(mysqli_connect_errno()) { 
die("MySQL connection failed: ". mysqli_connect_error()); 
} 
// Query for a list of all existing files 
$sql = "SELECT id, name, mime, size, created FROM $table"; 
$result = $dbLink->query($sql); 
// Check if it was successfull 
if($result) { 
// Make sure there are some files in there 
if($result->num_rows == 0) { 
    echo '<p>There are no files in the database</p>'; 
} 
else { 
    // Print the top of a table 
    echo '<table border="1" align="center"> 
      <H2 align="center"> Report Table</H> 
      <tr> 
       <td><b>Name</b></td> 
       <td><b>Mime</b></td> 
       <td><b>Size (bytes)</b></td> 
       <td><b>Created</b></td> 
       <td><b>Download</b></td> 

      </tr>'; 

    // Print each file 
    while($row = $result->fetch_assoc()) { 
     echo " 
      <tr> 
       <td>{$row['name']}</td> 
       <td>{$row['mime']}</td> 
       <td>{$row['size']}</td> 
       <td>{$row['created']}</td> 
       <td><a style='text-decoration:none;' href='get_file.php?id= {$row['id']}&$table'>Download</a></td> 
      </tr>"; 
    } 

    // Close table 
    echo '</table>'; 
    } 

    // Free the result 
    $result->free(); 
    } 
    else 
    { 
    echo 'Error! SQL query failed:'; 
    echo "<pre>{$dbLink->error}</pre>"; 
    } 
    // Close the mysql connection 
    $dbLink->close(); 

    } 
    ?> 

我的第二個代碼 get_file.php

<?php 

$table =$_GET['$table']; 

// Make sure an ID was passed 
if(isset($_GET['id'])) { 
// Get the ID 
$id = intval($_GET['id']); 

// Make sure the ID is in fact a valid ID 
if($id <= 0) { 
    die('The ID is invalid!'); 
} 
else { 
    // Connect to the database 
    $dbLink = new mysqli('localhost', 'sqldata', 'sqldata', 'accounts'); 
    if(mysqli_connect_errno()) { 
     die("MySQL connection failed: ". mysqli_connect_error()); 
    } 

    // Fetch the file information 
    $query = " 
     SELECT mime, name, size, data 
     FROM $table 
     WHERE id = {$id}"; 
    $result = $dbLink->query($query); 

    if($result) { 
     // Make sure the result is valid 
     if($result->num_rows == 1) { 
     // Get the row 
      $row = mysqli_fetch_assoc($result); 

      // Print headers 
      header("Content-Type: ". $row['mime']); 
      header("Content-Length: ". $row['size']); 
      header("Content-Disposition: attachment; filename=". $row['name']); 

      // Print data 
      echo $row['data']; 
     } 
     else { 
      echo 'Error! No image exists with that ID.'; 
     } 

     // Free the mysqli resources 
     @mysqli_free_result($result); 
     } 
     else { 
     echo "Error! Query failed: <pre>{$dbLink->error}</pre>"; 
     } 
     @mysqli_close($dbLink); 
     } 
     } 
     else { 
     echo 'Error! No ID was passed.'; 
     } 
     ?> 
+2

你是**開放** SQL注入攻擊,和**如果你還沒有被黑客攻擊**。使用準備/參數化查詢完全避免此問題。 – Brad

+1

只是一個瘋狂的猜測,但你是否試過這個:'href =「get_file.php?id = {$ row ['id']}&$ table」>'。 (注意雙引號,而不是單個) –

+1

@JPLew他不能使用雙引號,因爲他已經使用了echo,但是這裏的問題是他沒有定義變量名,所以正確的方法是'href = 'get_file.php?id = {$ row ['id']}&table = $ table'> Download',除此之外,他在這段代碼中有很多威脅,他肯定會被黑客攻擊,但我們來修復他的問題 – MaveRick

回答

1

使用的HREF雙引號,因爲與變量單引號內字面上分析,就好像有沒有變數。或者像使用$ row ['id']一樣使用{}。

在旁邊注意,巨大的風險!

1

更新您的鏈接href='get_file.php?id={$row['id']}&table=$table' - 這樣你實際上在查詢字符串中有一個正確的名稱/值對。

然後在第二個文件中,使用$table =$_GET['table']而不是$table =$_GET['$table']

正如其他人所指出的那樣,您的代碼有很多安全問題。你需要重新思考你的方法來顯着地構建查詢。

+0

這是工作感謝你。 – hadi

0

問題是在這裏..

$table =$_GET['$table']; 

應該

$table =$_GET['table']; 

此您的驗證碼:href='get_file.php?id= {$row['id']}&$table'>

應改爲此代碼:href='get_file.php?id={$row['id']}&table={$table}'>

和你的問題解決了..

注意:mysql_ *現在已折舊。所以更好地使用mysqli_ *或PDO