Create folder name: images inside htdocs before you can create upload photo.
First, all images file will be uploaded to the images folder.
Second, path info. will be save on the database mysql.
code to paste:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="savepicture1.php" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td>Lastname</td>
<td><input name="lastname" type="text"> </td>
</tr>
<tr>
<td>Firsntame</td>
<td><input name="firstname" type="text"> </td>
</tr>
<tr>
<td>Age</td>
<td><input name="age" type="text"> </td>
</tr>
<tr>
<td>Picture</td>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile" >
</td>
<td><input name="upload" type="submit" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
savepicture1.php
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sample", $con);
$lastname=$_REQUEST['lastname'];
$result = mysql_query("SELECT * FROM table1 where lastname='$lastname'");
$count=mysql_num_rows($result);
if($count==1)
{
echo "<br><br><br><p align='center'><b>Record was already exist</b></p>";
}
else
{
echo "Upload: " . $_FILES["userfile"]["name"] . "<br />";
echo "Type: " . $_FILES["userfile"]["type"] . "<br />";
echo "Size: " . ($_FILES["userfile"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["userfile"]["tmp_name"] . "<br />";
move_uploaded_file($_FILES["userfile"]["tmp_name"],
"images/" . $_FILES["userfile"]["name"]);
echo "Stored in: " . "images/" . $_FILES["userfile"]["name"];
$path = "images/" . $_FILES["userfile"]["name"];
$sql="INSERT INTO table1 (lastname, firstname, age, photo)
VALUES
('$_POST[lastname]','$_POST[firstname]','$_POST[age]','$path')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}
mysql_close($con);
?>
No comments:
Post a Comment