0

我有一个旧的 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>
4

1 回答 1

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();
    ?>

此外,在执行任何 db 操作之前,您还应该检查文件是否已上传,以及上传时是否发生了任何错误:

<?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
}
?>
于 2012-06-30T00:02:15.723 回答