2013-08-22 14 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