Thursday 25 April 2013

store image in mysql xampp from html form - php

fileupload.html
<html>
<head>
</head>
<body>
<form method="post" action="addMember.php" enctype="multipart/form-data">
<table>
<tr>
<td>Band Member or Affiliates Name:</td>
<td> <input type="text" name="nameMember"/></td>
</tr>
<tr>
<td> Band Position:</td>
<td> <input type="text" name="bandMember"/></td>
</tr>
<tr>
<td> Photo:</td>
<td><input type="hidden" name="size" value="350000">
<input type="file" name="photo"></td>
</tr>
<tr>
<td>Other Member Information:</td>
<td><textarea rows="9" cols="40" name="aboutMember">
</textarea></td>
</tr>
<tr>
<td> Other Bands:</td>
<td><input type="text" name="otherBands" size=30 /></td>
</tr>
<tr>
<td><input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/></td>
<td><input TYPE="submit" name="cancel" title="Cancel the data" value="Cancel"/></td>
</tr>    
</table>
</form>
</body>
</html>
addMember.php
<?php
//This is the directory where images will be saved
$target = "C:/xampp/htdocs/phpPractice/images/";

$target = $target . basename( $_FILES['photo']['name']);

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

// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("Occumen") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO dbvalues (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;

//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['photo']['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.";
}
?>

No comments: