2016-11-27 18 views
0

私はこのPHPコードを使用して画像をデータベースにアップロードしています。それに という問題があり、それは何であるか分かりません。データベーステーブル名は です。 id,name VARCHAR(),photo LONGBLOBである。PHPを使用してMySQLデータベースに画像をアップロードする際の問題

<?php 
ini_set('display_errors', '1'); 
$servername = ""; 
$username = ""; 
$password = ""; 
//$host = ""; 

// Create connection 
$conn = new mysqli($servername, $username, $password); 

// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
echo "Connected successfully"; 
?> 
<html> 
<body> 
    <form method="post" enctype="multipart/form-data"> 
     <input type="file" name="image"/> 
     </br> 
     </br> 
     </br> 
     <input type="submit" name="go" value="Upload"/> 
    </form> 
    <?php 
     if(isset($_POST['go'])){ 
      if(getimagesize($_FILES['image']['tmp_name']) == FALSE){ 
       echo "Select a photo please"; 
      }else { 
       $image = addslashes($_FILES['image']['tmp_name']); 
       $name = addslashes($_FILES['image']['name']); 
       $image = file_get_contents($image); 
       $image = base64_encode($image); 
       save_image($image , $name); 
      } 
     } 
     function save_image($image , $name){ 
      $servername = "localhost"; 
      $username = "cl60-shooters"; 
      $password = "dbsjcNs-b"; 
      $conn = new mysqli($servername, $username, $password); 
      $qry = "insert into images (photo , name) VALUES ('$image','$name')"; 
      $result = mysqli_query($conn,$qry); 

      if($result){ 
       echo "Successfull upload"; 
      }else{ 
       echo "try Again"; 
       print_r($result); 
      } 



     } 

     ?> 
</body> 
</html> 

添付のスクリーンショットのように、結果は次のとおりです。 Result

答えて

0

あなたの関数は、データベースを言及する無視 - あなたは次のように、パラメータの1つとしてそれを提供する必要があります。

function save_image($image , $name){ 
    $servername = "localhost"; 
    $username = "cl60-shooters"; 
    $password = "dbsjcNs-b"; 
    $database='xxxxxxxx';/* enter correct db name */ 


    $conn = new mysqli($servername, $username, $password, $database); 
    $qry = "insert into images (`photo`, `name`) VALUES ('$image','$name')"; 
    $result = mysqli_query($conn,$qry); 

    if($result){ 
     echo "Successfull upload"; 
    }else{ 
     echo "try Again"; 
     print_r($result); 
    } 
} 

FYIは、あなたのコードはSQLインジェクションに対して脆弱だと言っています。

+0

ありがとう、今はうまくいきますし、SQLインジェクションについてはテストのために心配しないでください ありがとうございました:) –

0

あなたはmysqliコンストラクタでデータベース名を使用していません。次のようになります。

$servername = "localhost"; 
$username = "cl60-shooters"; 
$password = "dbsjcNs-b"; 
$database = "database_name_here"; 
$conn = new mysqli($servername, $username, $password, $database); 

これでうまくいくはずです。

+1

ありがとう、それは今うまくいきます:) –

+0

あなたは大歓迎です。 :) – Perumal

関連する問題