Here's my code to store image in a database. I need to retrieve image from the database. I need help pls.
<?php require_once('Connections/Waffi.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_Waffi, $Waffi);
$query_Recordset1 = "SELECT * FROM registration ORDER BY ID ASC";
$Recordset1 = mysql_query($query_Recordset1, $Waffi) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$error = array();
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
// Check name
if (empty($_POST['title'])) {
$error['tit'] = 'Please enter a Title';
}
if (empty($_POST['surname'])) {
$error['name'] = 'Please enter Your surname';
}
if (empty($_POST['lname'])) {
$error['nam'] = 'Please enter Your Firstname';
}
if (empty($_POST['email'])) {
$error['mail'] = 'Please enter Email Address';
}
if (empty($_POST['nation'])) {
$error['Nat'] = 'Please enter Your Nationality';
}
if (empty($_POST['haq'])) {
$error['HQua'] = 'Please enter Your highest qualification obtained';
}
if (empty($_POST['YoQ'])) {
$error['YrQ'] = 'Please enter the year you obtained the qualification';
}
if (empty($_POST['FoS'])) {
$error['StudF'] = 'Please enter your field of study';
}
if (empty($_POST['loginname'])) {
$error['LOGIN'] = 'Please enter a Login Name';
}
if (empty($_POST['pass'])) {
$error['PSWD'] = 'Please enter a Password';
}
if (empty($_POST['pass1'])) {
$error['PSWDS'] = 'Please enter confirm password';
}
if (empty($_POST['secQ']) || empty($_POST['secA'])) {
$error['QA'] = 'Please select a secret question and enter an answer';
}
$pwdOK = true;
$_POST['pass'] = trim($_POST['pass']);
if (strlen($_POST['pass']) < 6) {
$error['pwd_length'] = 'Your password must be at least 6 characters';
$pwdOK = false;
}
if ($_POST['pass'] != trim($_POST['pass1'])) {
$error['pwd'] = 'Your passwords don\'t match';
$pwdOK = false;
}
if ($pwdOK) {
$_POST['pass'] = sha1($_POST['pass']);
}
$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
if (!preg_match($checkEmail, trim($_POST['email']))) {
$error['email2'] = 'Please enter a valid email address';
}
if(isset($_FILES['MyPix'])) {

$allowedExtensions = array("png","jpg","jpeg","gif");
$Desc='';//$_POST['Desc'];

if (!in_array(end(explode(".",
strtolower($_FILES['MyPix']['name']))),
$allowedExtensions)) {
$error['chk'] = 'sorry image format not surported/you did not upload am image';
}
$theFileSize =intval( $_FILES['MyPix']['size']);
if($theFileSize>75000)
{
$error['size'] = 'sorry passport size most not be above 75kb';
}
//echo $theFileSize;
}
else
{
$error['size2'] = 'Upload your image';
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if (!$error) {
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$target_path ="upload/";
$name = basename( $_FILES['MyPix']['name']);
$ext =explode('.',$name);
$ext = $ext[count($ext)-1];
$SafeFile = $name;
$SafeFile = str_replace("#", "No.", $SafeFile);
$SafeFile = str_replace("$", "Dollar", $SafeFile);
$SafeFile = str_replace("%", "Percent", $SafeFile);
$SafeFile = str_replace("^", "", $SafeFile);
$SafeFile = str_replace("&", "and", $SafeFile);
$SafeFile = str_replace("*", "", $SafeFile);
$SafeFile = str_replace("?", "", $SafeFile);
$theFileSize =intval( $_FILES['MyPix']['size']);
$dbLink = new mysqli('localhost', 'root', 'Myworld001', 'registration');
$iname = $dbLink->real_escape_string($_FILES['MyPix']['name']);
$imime = $dbLink->real_escape_string($_FILES['MyPix']['type']);
$insertSQL = sprintf("INSERT INTO registration (ID, Title, Surname, LastName, MiddleName, Sex, Email, Marital_Status, DoB, Phone, Address, Nationality, HighestAcademicQualification, StudyField, BriefAboutYourself, Hobbies, Loginname, Password, RePassword, SecurityQuestion, SecretAnswer, imgID, imgField) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, '$ext', '{$SafeFile}')",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['title'], "text"),
GetSQLValueString($_POST['surname'], "text"),
GetSQLValueString($_POST['lname'], "text"),
GetSQLValueString($_POST['mname'], "text"),
GetSQLValueString($_POST['radio'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['mstatus'], "text"),
GetSQLValueString($_POST['dob'], "text"),
GetSQLValueString($_POST['fone'], "text"),
GetSQLValueString($_POST['addy'], "text"),
GetSQLValueString($_POST['nation'], "text"),
GetSQLValueString($_POST['haq'], "text"),
GetSQLValueString($_POST['FoS'], "text"),
GetSQLValueString($_POST['brief'], "text"),
GetSQLValueString($_POST['hobby'], "text"),
GetSQLValueString($_POST['loginname'], "text"),
GetSQLValueString($_POST['pass'], "text"),
GetSQLValueString($_POST['pass1'], "text"),
GetSQLValueString($_POST['secQ'], "text"),
GetSQLValueString($_POST['secA'], "text"));

mysql_select_db($database_Waffi, $Waffi);
$Result1 = mysql_query($insertSQL, $Waffi) or die(mysql_error());
$target_path = $target_path.$row['loginname'].$SafeFile;
// $target_path1 = $target_path1.$row['loginname'].$SafeFile1;
if(move_uploaded_file($_FILES['MyPix']['tmp_name'], $target_path)) {
echo 'Success! Your image was successfully added!';
}
else{
echo "There was an error uploading the image, please try again!";
}
$insertGoTo = "registersuccess.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
$_POST = array();
}
}

?>