2015-12-25 145 views
0
<body bgcolor = "pink"> 
<form method = "post" action = "" enctype = "mutlipart/form-data"> 
    <input type = "file" name="up" > 
    <input type = "submit" value = "upload" name="submit"> 
    <input type = "reset" value = "clear"> 
</form> 
<?php 
if(isset($_POST['submit'])) 
{ 
//echo "<pre>"; 
//print_r($_FILES); 
//echo "</pre>"; 
$name=addslashes($_FILES['up']['name']); 
$image=addslashes($_FILES['up']['tmp_name']); 
$image=file_get_contents($image); 
$image=base64_encode($image); 
echo $image; 
$con = mysqli_connect("localhost", "root", ""); 
mysqli_select_db($con, "pic"); 
$res = mysqli_query($con , "INSERT INTO image(name, image) VALUES ('$name', '$image')"); 
if($res) 
{ 
    echo "Image uploaded successfully"; 
} 
else 
{ 
    echo "Failed to save image in Database"; 
} 
$qry = mysqli_query($con, "SELECT * FROM image"); 
echo "<table width = 450 border = 5 bordercolor = red align = center bgcolor = green cellpadding = 12>"; 
echo "<th>Sno</th>"; 
echo "<th>Name</th>"; 
echo "<th>Image</th>"; 
while ($row = mysqli_fetch_row($qry)) { 
    $src = '<img width = 250 height = 125 src = "data:image; base64, '.$row[2].'">'; 
    echo "<tr>"; 
    echo "<th>".$row[0]."</th>"; 
    echo "<th>".$row[1]."</th>"; 
    echo "<th>".$src."</th>"; 
    echo "</tr>"; 
} 
    echo "</table>"; 
     } 
    ?> 
</body> 

這裏我使用圖片上傳並在表格中顯示,我得到了以下錯誤。如何上傳圖片並使用PHP在網頁中顯示?

  • 注意:未定義指數:最多在C:\ WAMP \ WWW \ DMS \ image.php

注:在這裏,我寫PHP和HTML代碼在同一個頁面。

如何來解決這個問題。請幫我

+0

很明顯,你沒有這樣一個索引作爲'陣列中up'。沒有給出這個名字的文件。嘗試'var_dump'文件數組,看看它有什麼。 –

+0

數組(size = 0)爲空。而 var_dump($ _ FILES); – Kiran

+0

不能工作怎麼可能 – Kiran

回答

1

嘗試

<body bgcolor = "pink"> 
<form method="post" action="" enctype="multipart/form-data"> 
    <input type="file" name="up"> 
    <input type="submit" value="Upload" name="submit"> 
    <input type = "reset" value = "clear"> 
</form> 
<?php 
    if(isset($_POST['submit'])) 
    { 
    //echo "<pre>"; 
    //print_r($_FILES); 
    //echo "</pre>"; 
    $name=addslashes($_FILES['up']['name']); 
    $image=addslashes($_FILES['up']['tmp_name']); 
    $image=file_get_contents($image); 
    $image=base64_encode($image); 
    echo $image; 
    $con = mysqli_connect("localhost", "root", ""); 
    mysqli_select_db($con, "pic"); 
    $res = mysqli_query($con , "INSERT INTO image(name, image) VALUES ('$name', '$image')"); 
    if($res) 
    { 
     echo "Image uploaded successfully"; 
    } 
    else 
    { 
     echo "Failed to save image in Database"; 
    } 

    $qry = mysqli_query($con, "SELECT * FROM image"); 
    echo "<table width = 450 border = 5 bordercolor = red align = center bgcolor = green cellpadding = 12>"; 
    echo "<th>Sno</th>"; 
    echo "<th>Name</th>"; 
    echo "<th>Image</th>"; 
    while ($row = mysqli_fetch_row($qry)) 
    { 
     $src = '<img width = 250 height = 125 src = "data:image; base64, '.$row[2].'">'; 
     echo "<tr>"; 
     echo "<th>".$row[0]."</th>"; 
     echo "<th>".$row[1]."</th>"; 
     echo "<th>".$src."</th>"; 
     echo "</tr>"; 
    } 

    echo "</table>"; 
    } 
    exit(); 
?> 
</body> 
+0

謝謝Hassaan我得到了。爲什麼你使用exit(); 。我在最後寫了exit()函數非常感謝。 – Kiran

+0

@Kiran'exit()'不是必須的,你可以刪除它。我只是爲了測試目的而寫它。 – Hassaan

+0

您是否在我的php代碼中發現任何錯誤,然後在我的代碼中更改了哪些內容。 – Kiran

相關問題