Friday, September 12, 2014

Simple Adding and Inserting image with PHP

These are the codes in our Administration area, credits to sourcecodester.com.

-> please make sure you have a connection with the database:
<?php
error_reporting(0);
session_start();
include("dbconnect.php");
$action = $_REQUEST['action']; 
?>
1. Let's start with ADDING along with INSERTING IMAGE form:
elseif($action=="YOURACTION"){
echo "<CENTER><form action='admin.php' method='post' enctype='multipart/form-data'>";
echo "<table>";
echo "<tr><td colspan=2 align=center>  Add Profile </td></tr>";
echo "<tr><td>  Firstname: </td><td><input type=text name=denfname size=30></td></tr>";
echo "<tr><td>  Lastname: </td><td><input type=text name=denlname size=30></td></tr>";
echo "<tr><td>  Address: </td><td><input type=text name=denaddress size=30></td></tr>";
echo "<tr><td> Gender:<td><select name=dengen><option name=dengen> Male";
echo "<option name=dengen> Female"; echo "</select></td>";
echo "<tr><td> Add picture: </td><td><input type=file name=logo size=20></td></tr>";
echo "<tr><td align=center colspan=2><input type=submit name=submit value='add dentist'></td></tr>";
echo "<input type=hidden name=action value=YOUR_ACTION>";
echo "</form>";
echo "</table>";
}
*then your query for YOUR_ACTION
elseif($action=="YOUR_ACTION"){
if ($_FILES['logo']['name'] != "") { 
$imageInfo = getimagesize($_FILES['logo']['tmp_name']); // get image size
$width = $imageInfo[0]; // image width
$height = $imageInfo[1];  // image height

if($height > 2000 || $width > 2000){
echo "<script> alert('Image is to big! Try to resize the picture!') </script>";
exit;
} // check size 

$newimg1 = date("YmdHis").".jpg";  // set name for new image
$newimg2 = date("YmdHis").".pdf"; // set name for pdf if is necesary

if(stristr($_SERVER['OS'],"win")){
$path = "prodimg/";
} // determin path of image folder
elseif(stristr($_SERVER['OS'],"linux")){
$path = str_replace("admin.php","",$_SERVER['SCRIPT_FILENAME']) . "prodimg/";
else {
$path = "prodimg/";
}

move_uploaded_file ( $_FILES['logo']['tmp_name'], $path . $newimg1  );
}  // copy image in image folder
else{
$newimg1="pc.gif"; 
} // if is not posible than set image name as pc.gif

$denfname = $_POST['denfname'];
$denlname = $_POST['denlname'];
$denaddress = $_POST['denaddress'];
$dengen = $_POST['dengen'];
$res2 = mysql_query("insert into dentists(denfname,denlname,denaddress,dengen,denpicture) 
values('$denfname','$denlname','$denaddress','$dengen','$newimg1')");

echo "<script>location.replace('admin.php?action=showalldentists')</script>";
}


 




 


No comments:

Post a Comment

Powered By Blogger

Translate