Wednesday 19 December 2012

Storing image in database using php/ mysql

First in this tutorial, creating web form and posting data on next page ,as image and  data stored on table. I have mention  3 details below.
1. Creating Web form
2. Storing  Image on database using Page 2
3. Table created where value is stored.
===================================================================


Page 1-----
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="18decres.php" method="post" enctype="multipart/form-data" target="_blank">
<table>
<tr><td>
<h1 align="center"><u><b>LETTER HEAD</b></u></h1><br />
<hr>
<h6 align="right">Photo<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
                <input name="image" type="file" />
                                            </h6><p align="right"><em><small><text  align="right">
  To be attested by head of office</small></em></p><BR>
<h5><b>Subject: </b>Verification of employement for <input type="text" name="a" size="10"/> [employee name] to NIC Certifying Authority.</h5>

<p>This letter is to confirm that <input type="text" name="b" size="10"/>[<i>Applicant Name</i>] is working in <input type="text" name="c" size="10"/>[<i>Organisation name</i>] as <input type="text" name="d" size="10"/>[<i>Designation</i>]. He/She has been working in this organisation since<input type="text" name="e" size="10"/> [<i>Date of joining</i>].His/Her tenure is till <input type="text" name="f" size="10"/>[<i>dact</i>]
<br />
<br />
<br />
<b><u>Additional declaration forxxxxxxxxxxxs:</u></b><br />
<input type="text" name="g" size="10"/>[<i>DSC Applicant Name</i>] is working in<input type="text" name="h" size="10"/> [<i>Organisation xxxx</i>]. His/Her contract is valid from <input type="text" name="i" size="10"/>to <input type="text" name="j" size="10"/>.
<table>
<tr><td align="right">Yours Sincerely,<br />
[<i>xxxxxxxxe (xxxxxxxx)</i>]<br />
<input type="text" name="k" size="10"/> [<i>xxxxxzzzzzzzxxxxxmp</i>]
</td>
</tr></table>
<em><small>
<input type="submit" name="submit" value="Save Changes"  />
</small></em></td></tr></table>
</form>
</body>
</html>
===================================================================

Page 2..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
 <?php
     $a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$d = $_POST['d'];
$e = $_POST['e'];
$f = $_POST['f'];
$g = $_POST['g'];
$h = $_POST['h'];
$i = $_POST['i'];
$j = $_POST['j'];
$k = $_POST['k'];    
        ?>
 <form action="" method="post">
<table>
<tr><td>
<h1 align="center"><u><b>LETTER HEAD</b></u></h1><br />
<hr>
<h6 align="right">Photo<?php error_reporting (E_ALL ^ E_NOTICE); ?>
 

</h6><p align="right"><em><small><text  align="right">
  To be attested by head of office</small></em></p><BR>
<h5><b>Subject: </b>Verification of employement for <b><?php echo $a;?> to NIC Certifying Authority.</h5>

<p>This letter is to confirm that <b><?php echo $b;?></b> is working in <b> <?php echo $c;?></b> as <b><?php echo $d;?></b>.He/She has been working in this organisation since <b><?php echo $e;?></b>. His/Her tenure is till <b><?php echo $f;?></b>
<br />
<br />
<br />
<b><u>Additional declaration for contactual employees:</u></b><br />
<b><?php echo $g;?></b> is working in <b><?php echo $h;?></b>. His/Her contract is valid from <b><?php echo $i;?></b> to <b><?php echo $j;?></b>.
<table>
<tr><td align="right">Yours Sincerely,<br />
[<i>Head of Office/Office-In-Charge (for contract employee)</i>]<br />
<b><?php echo $k;?></b>
</td>
</td></tr>
</td></tr></table>
</form>
<?php
                                                                if (isset($_POST['submit']) && $_FILES['image']['size'] > 0) {
                                                                    $fileName = $_FILES['image']['name'];
                                                                    $tmpName = $_FILES['image']['tmp_name'];
                                                                    $fileSize = $_FILES['image']['size'];
                                                                    $fileType = $_FILES['image']['type'];

                                                                    $fp = fopen($tmpName, 'r');
                                                                    $content = fread($fp, filesize($tmpName));
                                                                    $content = addslashes($content);
                                                                    fclose($fp);

                                                                    if (!get_magic_quotes_gpc()) {
                                                                        $fileName = addslashes($fileName);
                                                                    }
                                                                    $objConnect = mysql_connect("localhost", "root", "") or die(mysql_error());
                                                                    $objDB = mysql_select_db("database name");

//$query = "INSERT INTO upload (name, size, type, content ) ".
//"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
//mysql_query($query) or die('Error, query failed');
                                                                 
                                                                    //echo "<br>File $fileName uploaded<br>";
                                                                    //$certi = serialize($_POST['cbcretificate']);
                                                                    //  $objConnect = mysql_connect("localhost","root","123456") or die(mysql_error());
//$objDB = mysql_select_db("phpmyadmin");
//$cbcretificate = $_POST['cbcretificate'];
                                                                    //$cbcretificate=$_POST["cbcretificate"];
//while (list ($key,$val) = @each ($cbcretificate))
                                                                    //{echo "$val,";
                                                                    //}
//$selected_colors = "";
//foreach ($cbcretificate as $cbcretificate) {
//$selected_cert .= $cbcretificate . ", ";
//}
//$selected_cert = substr($selected_cert, 0, -2);
//implode(',', $_POST['cbcretificate']);
//$cbcretificate=serialize($_POST['cbcretificate']); //takes the data from a post operation...
//$media_array = $_POST['cbcretificate'];
//foreach ($media_array as $one_media) {
//$source .= $one_media.", ";
//}
//$media = substr($source, 0, -2);$q1 = "select * from firstpagetable4 where ApplicantID IN (select max(ApplicantID) from firstpagetable4)  ";
                                                                    $q2 = "select * from sc_scr_print_new1 where ApplicantID IN (select max(ApplicantID) from sc_scr_print_new1)  ";

                                                                    $res1 = mysql_query($q2);
                                                                    $output_str2 = ' ';
                                                                    while ($row = mysql_fetch_assoc($res1)) {

                                                                        $output_str2 .= ($row['ApplicantID'] + 1); //the site link is for example
                                                                        // $output_str .= '<a href="pdf.php' . $row["max(ApplicantID)+1"] . '">  Click here to generate PDF</a>' . $tab;
//echo" <h1>'<a  href="pdf.php?postid=' . $row["max(ApplicantID)+1"] . '">Click here to generate PDF</a>'.$tab;
                                                                    }



                                                                    $strSQL = "INSERT INTO letter ";



                   $strSQL .="(ApplicantID,name,size,type,content,a,b,c,d,e,f,g,h,i,j,k) ";
        $strSQL .="VALUES ";
$strSQL .="('','" . $fileName . "','" . $fileSize . "','" .      $fileType . "','" . $content . "','" . $_POST["a"] . "','" . $_POST["b"] . "' ";
$strSQL .=",'" . $_POST["c"] . "','" . $_POST["d"] . "','" . $_POST["e"] . "' ";
$strSQL .=",'" . $_POST["f"] . "'";
$strSQL .=",'" . $_POST["g"] . "','" . $_POST["h"] . "','" . $_POST["i"] . "','" . $_POST["j"] . "','" . $_POST["k"] . "')";
$objQuery = mysql_query($strSQL);

 printf(" Your Applicant ID is  %d\n", mysql_insert_id());


//$lastid=mysql_insert_id();
                                                                    if ($objQuery) {
                                                                        echo ".";
                                                                    } else {
                                                                        echo "Error Save [" . $strSQL . "]";
                                                                    }
                                                                    $inserted_id = mysql_insert_id();
//$strsql1="Select Image  from Applicantform22";
//$result_last_id=mysql_query($strsql1);

                                                                    mysql_close($objConnect);
                                                                }
                                                                ?>

</body>
</html>


=============================================================

Table 


CREATE TABLE IF NOT EXISTS `letter` (
  `ApplicantID` mediumint(9) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `size` int(11) DEFAULT NULL,
  `type` varchar(20) DEFAULT NULL,
  `content` mediumblob,
  `a` varchar(30) NOT NULL,
  `b` varchar(30) NOT NULL,
  `c` varchar(30) NOT NULL,
  `d` varchar(30) NOT NULL,
  `e` varchar(30) NOT NULL,
  `f` varchar(30) NOT NULL,
  `g` varchar(30) NOT NULL,
  `h` varchar(30) NOT NULL,
  `i` varchar(30) NOT NULL,
  `j` varchar(30) NOT NULL,
  `k` varchar(30) NOT NULL,
  PRIMARY KEY (`ApplicantID`)
) ;