I have a problem uploading a photo in the database using PHP and Microsoft Server SQL. It's my first time and cant find the solution over the internet, can someone please help?
Here's my HTML code for the form:
<!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>
And here's my PHP code:
<?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.
";
die( print_r( sqlsrv_errors(), true));
}
else
{
echo "Rows affected: ".sqlsrv_rows_affected( $stmt )."
<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();
}
?>
Here's my NewConnectProcess.php which is used for database connection, it works but I think it may help:
<?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.
";
die( print_r( sqlsrv_errors(), true));
}
}
}
?>
Here's the error:
The error have indicated the problem. uploadedfile
was not defined.
echo "The file ". basename( $_FILES['uploadedfile']['name']). "
try changing it to ['photo'].
$_FILES['photo']['name']