无法使用PHP在MS数据库中上传照片

时间:2014-06-23 06:14:25

标签: php html sql-server image sqlsrv

使用PHP和Microsoft Server SQL在数据库中上传照片时遇到问题。这是我的第一次,无法通过互联网找到解决方案,有人可以帮忙吗?

以下是表单的HTML代码:

<!DOCTYPE html>
<html>
  <head>
    <title>e-Scan Archiving - Accounting</title>
    <link href="main1.css" rel="stylesheet" type="text/css">


  </head>

  <body>

    <form method="post" action="addMember.php" enctype="multipart/form-data">
        <p>
                  Please Enter the Members Name.
                </p>
                <p>
                  Name:
                </p>
                <input type="text" name="nameMember"/>

                <p>
                  Please Upload a Photo of yourself.
                </p>
                <p>
                  Photo:
                </p>
                <input type="hidden" name="size" value="6000000">
                <input type="file" name="photo"> 

                <br/>
                <br/>
                <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Name"/>
              </form>
      </body>
</html>

这是我的PHP代码:

<?php

    include_once("NewConnectProcess.php");

    class UploadPicture extends Connection {
        public function doUploadData() {
            //This is the directory where images will be saved
            $target = "your directory";
            $target = $target . basename( $_FILES['photo']['name']);

            //This gets all the other information from the form
            $name=$_POST['nameMember'];
            $pic=($_FILES['photo']['name']);

            //Writes the information to the database
            $sql = "INSERT INTO dbo.pictureInfo (name,photo)
            VALUES ('$name','$pic')" ;

            $stmt = sqlsrv_query($this->conn, $sql);

            //Writes the photo to the server
            if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
                {

                //Tells you if its all ok
                echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
                }
            else 
                {

                //Gives and error if its not
                echo "Sorry, there was a problem uploading your file.";
                }
            if( $stmt === false )
                {
                echo "Statement could not be executed.\n";
                die( print_r( sqlsrv_errors(), true));
                } 
            else 
                {
                echo "Rows affected: ".sqlsrv_rows_affected( $stmt )."\n<br>"; 
                }

            // Free statement and connection resources
            sqlsrv_free_stmt($stmt);
            sqlsrv_close($this->conn);

        }
    }
    if(isset($_POST['upload'])){
      $i = new UploadPicture();
      $i->connectDatabase();
      $i->doUploadData();
    }
?>

这是我的NewConnectProcess.php用于数据库连接,它可以工作,但我认为它可能会有所帮助:

<?php

class Connection {

    public $conn;

    public function connectDatabase() {
        $serverName = "localhost"; 
        $uid = "sa";   
        $pwd = "joseph04";  
        $databaseName = "TestDatabase"; 

        $connectionInfo = array( "UID"=>$uid, "PWD"=>$pwd, "Database"=>$databaseName); 

        // Connect using SQL Server Authentication 
        $this->conn = sqlsrv_connect( $serverName, $connectionInfo);  

        // Test Connection
        if( $this->conn === false )
        {
            echo "Connection could not be established.\n";
            die( print_r( sqlsrv_errors(), true));
        }
    }
}
?>

这是错误: error

1 个答案:

答案 0 :(得分:0)

错误表明了问题。 uploadedfile未定义。

echo "The file ". basename( $_FILES['uploadedfile']['name']). "

尝试将其更改为[&#39;照片&#39;]。

$_FILES['photo']['name']