图片未更新且未检索

时间:2018-10-01 05:28:27

标签: php html

我想在数据库中存储图像。我尝试使用插入查询,但是通过更新(使用UPDATE查询)无法正常工作。而且检索查询不起作用。我想在三个位置检索图像。

这是我的代码...其他所有内容均已成功更新和检索,但未存储和检索图像..请帮助我解决问题。 我正在使用长Blob数据类型来存储图像。

更新是在PersonalDetails模式下完成的。这是我的模式及其php代码。

StudentProfile.php(需要在卡片和模式中都进行检索)

<?php

// Initialize the session
session_start();

// Include Connection file
require_once 'Connection.php';

//define variables
include('Variables.php');

//define variables for errors
$first_name_err=$middle_name_err=$last_name_err=$admitted_date_err=$academic_year_err=$course_err=$dob_err=$nic_err=$gender_err=$nationality_err=$religion_err=$race_err="";


    //Create select query for retrieving data 
    $sql = "SELECT personal_details.*,contact_details.*,academic_details.*,extra_curricular_activities.* FROM personal_details JOIN contact_details ON personal_details.Student_ID = contact_details.Student_ID JOIN academic_details ON personal_details.Student_ID = academic_details.Student_ID JOIN extra_curricular_activities ON personal_details.Student_ID = extra_curricular_activities.Student_ID WHERE personal_details.Student_ID =  '{$_SESSION['student_id']}'";
   
   //Check query
    $result = $connection->query($sql);


    if ($result->num_rows > 0) {

    // output data of each row
        while($row = $result->fetch_assoc()) {

            //Include Row File
            include('Row.php');
        }
    } else {
        echo "No results found";
    }

//Close connection
$connection->close();


?>

//card
//Image need to retrieve here
<div class="col-3">
            			<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($profile_picture).'" height="200" width="200" />';?>
            		</div>
            		<div class="col-9">
 
//modal

<!--Model body-->
            <form action="PersonalDetails.php" method="post" enctype="multipart/form-data">
            <div class="modal-body mx-3">

            	<div class="row">

                    <div class="md-form col-6">
                        <?php echo '<img src="data:image/jpeg;base64,'.base64_encode($profile_picture).'" height="200" width="200" class="img-thumnail" />';?><br><br>
                        <div class="custom-file">
                            <input type="file" name="profile_picture" class="custom-file-input" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
                            <label class="custom-file-label" for="inputGroupFile01"></label>
                        </div>
                    </div>

PersonalDetails.php

<?php
// Initialize the session
session_start();

// Include Connection file
require_once 'Connection.php';

//define variables
include('Variables.php');

//define variables for errors
$first_name_err=$middle_name_err=$last_name_err=$admitted_date_err=$academic_year_err=$course_err=$dob_err=$nic_err=$gender_err=$nationality_err=$religion_err=$race_err="";

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


if(isset($_POST["save"])){

	// Processing form data when form is submitted
    if($_SERVER["REQUEST_METHOD"] == "POST"){

        // Validate input fields
        if(empty(trim($_POST['first_name'])) || empty(trim($_POST['middle_name']))  || empty(trim($_POST['last_name'])) || empty(trim($_POST['admitted_date'])) || empty(trim($_POST['academic_year'])) || empty(trim($_POST['course'])) || empty(trim($_POST['dob'])) || empty(trim($_POST['nic']))  || empty(trim($_POST['gender']))  || empty(trim($_POST['nationality'])) || empty(trim($_POST['religion'])) || empty(trim($_POST['race'])) ){

            $_SESSION['empty'] = "Mandatory fields are missing.Please fill the empty fields."; 
            header('location: StudentProfile.php');    
        } 

        $first_name = test_input($_POST['first_name']);
        $middle_name = test_input($_POST['middle_name']);
        $last_name = test_input($_POST['last_name']);

        // check if Name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$first_name) || !preg_match("/^[a-zA-Z ]*$/",$middle_name) || !preg_match("/^[a-zA-Z ]*$/",$last_name)) {

          $_SESSION['charachter']= "Only letters and white space allowed"; 
          header('location: StudentProfile.php');
        }
        
        // Check input errors before inserting in database
        if(empty($_SESSION['empty']) && empty($_SESSION['charachter']) ){

            // Prepare a select statement
            $sql = "SELECT * FROM personal_details WHERE Student_ID = '{$_SESSION['student_id']}'";
            
            $result = $connection->query($sql);
        
            // Check if student id exists, if yes then create update query
            if($result->num_rows > 0){ 

                $file = addslashes(file_get_contents($_FILES["profile_picture"]["tmp_name"]));
                $sql="UPDATE personal_details 
                SET Profile_Picture='$file',
                    First_Name='$first_name',
                    Middle_Name='$middle_name',
                    Last_Name='$last_name',
                    Admitted_Date='$admitted_date',
                    Academic_Year='$academic_year',
                    Course='$course',
                    DOB='$dob',
                    NIC='$nic',
                    Gender='$gender',
                    Nationality='$nationality',
                    Religion='$religion',
                    Race='$race' 

                WHERE Student_ID='{$_SESSION['student_id']}'";	

                if ($connection->query($sql) === TRUE) {
    				$_SESSION['success']  = "Updated Successfully!!";
                    header('location: StudentProfile.php');
    			} else {
    				$_SESSION['error']  = "Error: " . $sql . "<br>" . $connection->error;
    			}

            } else{

                $file = addslashes(file_get_contents($_FILES["profile_picture"]["tmp_name"]));
                $allowed = array("jpeg","jpg","png");
                // Create a insert query if student id doesn't exist
                $sql= "INSERT INTO personal_details(Student_ID,Profile_Picture,First_Name,Middle_Name,Last_Name,Admitted_Date,Academic_Year,Course,DOB,NIC,Gender,Nationality,Religion,Race)
    				VALUES ('{$_SESSION['student_id']}','$file','$first_name','$middle_name','$last_name','$admitted_date','$academic_year','$course','$dob','$nic','$gender','$nationality','$religion','$race')"; 
    			 

    			if ($connection->query($sql) === TRUE) {
    			    $_SESSION['success']  = "Submitted Successfully!!";
                    header('location: StudentProfile.php');
    			} else {
    			    $_SESSION['error']  = "Error: " .$sql."<br>". $connection->error;
    			}
            } 
        }

    }  



}

$connection->close();
?>

0 个答案:

没有答案