数据没有插入mysqli

时间:2012-06-29 23:42:22

标签: php mysqli

我有一个旧的mysql代码,它成功地将图像文件的名称插入到数据库中。但是由于旧的mysql被斩首,我试图在mysqli中做同样的事情(由于我的PHP版本,不能使用PDO)。问题是我无法将图像文件名插入到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>

1 个答案:

答案 0 :(得分: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();
    ?>

此外,您应该检查文件是否已上传,以及在执行任何数据库内容之前是否发生了上传错误:

<?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
}
?>