2012-06-29 158 views
0

我有一箇舊的mysql代碼,它成功地將一個圖像文件的名稱插入到數據庫中。但是,由於舊的mysql被斬首,我試圖在mysqli中做同樣的事情(由於PHP版本的原因,不能使用PDO)。問題是我無法將圖像文件名插入到mysqli中的數據庫中。我究竟做錯了什麼?數據沒有插入mysqli

下面是mysqli的代碼:

<?php 

    session_start(); 


    $username="xxx"; 
    $password="xxx"; 
    $database="mobile_app"; 

     $mysqli = new mysqli("localhost", $username, $password, $database); 

     /* check connection */ 
     if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     die(); 
     } 

    $result = 0; 

if(getimagesize($_FILES['fileImage']['tmp_name'])){ 

    if(is_file("ImageFiles/".$_FILES['fileImage']['name'])) { 
     $parts = explode(".",$_FILES['fileImage']['name']); 
     $ext = array_pop($parts); 
     $base = implode(".",$parts); 
     $n = 2; 

     while(is_file("ImageFiles/".$base."_".$n.".".$ext)) $n++; 
     $_FILES['fileImage']['name'] = $base."_".$n.".".$ext; 

     move_uploaded_file($_FILES["fileImage"]["tmp_name"], 
     "ImageFiles/" . $_FILES["fileImage"]["name"]); 
     $result = 1; 

     $imagesql = "INSERT INTO Image (ImageFile) 
     VALUES (?)"; 

       if (!$insert = $mysqli->prepare($imagesql)) { 
      // Handle errors with prepare operation here 
     } 

     $insert->bind_param("s",'ImageFiles/' . $_FILES['fileImage']['name']); 

    $insert->execute(); 

      if ($insert->errno) { 
       // Handle query error here 
      } 

      $insert->close(); 


    } 
     else 
      { 
      move_uploaded_file($_FILES["fileImage"]["tmp_name"], 
      "ImageFiles/" . $_FILES["fileImage"]["name"]); 
      $result = 1; 

     $imagesql = "INSERT INTO Image (ImageFile) 
     VALUES (?)"; 

       if (!$insert = $mysqli->prepare($imagesql)) { 
      // Handle errors with prepare operation here 
     } 

     $insert->bind_param("s",'ImageFiles/' . $_FILES['fileImage']['name']); 

    $insert->execute(); 

      if ($insert->errno) { 
       // Handle query error here 
      } 

      $insert->close(); 


      } 
    } 
    ?> 

下面是舊版本的MySQL代碼中插入所做的工作:

<?php 

    session_start(); 


    $username="xxx"; 
    $password="xxx"; 
    $database="mobile_app"; 

    mysql_connect('localhost',$username,$password); 

    mysql_select_db($database) or die("Unable to select database"); 

    $result = 0; 

if(getimagesize($_FILES['fileImage']['tmp_name'])){ 

    if(is_file("ImageFiles/".$_FILES['fileImage']['name'])) { 
     $parts = explode(".",$_FILES['fileImage']['name']); 
     $ext = array_pop($parts); 
     $base = implode(".",$parts); 
     $n = 2; 

     while(is_file("ImageFiles/".$base."_".$n.".".$ext)) $n++; 
     $_FILES['fileImage']['name'] = $base."_".$n.".".$ext; 

     move_uploaded_file($_FILES["fileImage"]["tmp_name"], 
     "ImageFiles/" . $_FILES["fileImage"]["name"]); 
     $result = 1; 

     $imagesql = "INSERT INTO Image (ImageFile) 
     VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')"; 

     mysql_query($imagesql); 

    } 
     else 
      { 
      move_uploaded_file($_FILES["fileImage"]["tmp_name"], 
      "ImageFiles/" . $_FILES["fileImage"]["name"]); 
      $result = 1; 

      $imagesql = "INSERT INTO Image (ImageFile) 
      VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')"; 

    mysql_query($imagesql); 

      } 

} 
      mysql_close(); 

    ?> 

     <script language="javascript" type="text/javascript"> 
     window.top.stopImageUpload(<?php echo $result ? 'true' : 'false'; ?>, '<?php echo $_FILES['fileImage']['name'] ?>'); 
     </script> 
+0

您是否在某處出現錯誤? – Jocelyn

+0

一個好的開始將是用實際的錯誤處理替換「處理錯誤」類型的註釋。或者至少顯示錯誤。 – smoove

+0

給我5分鐘檢查錯誤 – user1490145

回答

1

試試這個,值分配給一個變量,然後再結合它,不建立在bind_param內的數據:

<?php 
    $imagesql = "INSERT 
        INTO Image (ImageFile) 
        VALUES (?)"; 

    $insert = $mysqli->prepare($imagesql); 

    //Dont pass data directly to bind_param store it in a variable 
    $insert->bind_param("s",$img); 

    //Assign the variable 
    $img = 'ImageFiles/'.$_FILES['fileImage']['name']; 

    $insert->execute(); 
    ?> 

你也應該ch eck如果文件已上傳,並且在執行任何數據庫之前發生了上傳錯誤:

<?php 
//If POST 
if($_SERVER['REQUEST_METHOD']=='POST'){ 
    //Check no errors on upload 
    if($_FILES['fileImage']['error']==0){ 

     if(isset($_FILES['fileImage']['tmp_name'])){ 
      $imgSize = getimagesize($_FILES['fileImage']['tmp_name']); 
      ... 
      ... 
     } 

    }else{ 
     //get error code and display error 
    } 

}else{ 
    //No POST 
} 
?> 
+0

大,已經整理吧: )謝謝 – user1490145

+0

np,很高興它的工作;) –