即時通訊使用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.';
}
?>
你是**開放** SQL注入攻擊,和**如果你還沒有被黑客攻擊**。使用準備/參數化查詢完全避免此問題。 – Brad
只是一個瘋狂的猜測,但你是否試過這個:'href =「get_file.php?id = {$ row ['id']}&$ table」>'。 (注意雙引號,而不是單個) –
@JPLew他不能使用雙引號,因爲他已經使用了echo,但是這裏的問題是他沒有定義變量名,所以正確的方法是'href = 'get_file.php?id = {$ row ['id']}&table = $ table'> Download',除此之外,他在這段代碼中有很多威脅,他肯定會被黑客攻擊,但我們來修復他的問題 – MaveRick