I want to upload image file and signature file in different folder - php

I create a html page which have two file that i want to store these 2 different file i.e., Image and Signature in two different folder i.e., profile and signature folder. but Signature file stored in profile folder and Image file cant stored any of them. Please help me out.
here is my code
<?php
require 'config.php';
if(isset($_POST['submit'])){
$imagename = $_FILES["Image"]["name"];
$imagetemp = $_FILES["Image"]["tmp_name"];
$imagefolder = "Upload/profile/".$imagename;
move_uploaded_file($imagetemp, $imagefolder);
echo "<img src='$imagefolder' height='100px' width='100px'";
$signname = $_FILES["Signature"]["name"];
$signtemp = $_FILES["Signature"]["tmp_name"];
$signfolder = "signature/".$signname;
move_uploaded_file($signtemp, $signfolder);
echo "<img src='$signfolder' height='100px' width='100px'";
$Name = $_POST['Name'];
$gender = $_POST['gender'];
$phone = $_POST['phone'];
$location = $_POST['location'];
$Qualification = $_POST['Qualification'];
$Speciality = $_POST['Speciality'];
$Experience = $_POST['Experience'];
$License_No = $_POST['License_No'];
$Email = $_POST['Email'];
$Password = $_POST['Password'];
//$Image = $_POST['Image'];
//$Signature = $_POST['Signature'];
$sql = ("INSERT INTO doctor (Doctor_Name, gender, Phone, Location, Qualification, Speciality, Experience, License_No, Email, Password) value('$Name', '$gender', '$phone', '$location', '$Qualification', '$Speciality', '$Experience', '$License_No', '$Email', '$Password')");
$insertquery= mysqli_query($con, $sql);
if($insertquery){
echo "data inserted";
}
else{
echo "ERROR: $sql <br> $con->error";
}
$con->close();
}

Related

MySQL database could not be updated with PHP program

addmember.php
<?php
require_once("dbtools.inc.php");
$account = $_POST["account"];
$password = $_POST["password"];
$name = $_POST["name"];
$sex = $_POST["sex"];
$year = $_POST["year"];
$month = $_POST["month"];
$day = $_POST["day"];
$telephone = $_POST["telephone"];
$address = $_POST["address"];
$email = $_POST["email"];
$comment = $_POST["comment"];
$link = create_connection();
$sql = "SELECT * FROM users Where account = '$account'";
$result = execute_sql($link, "member", $sql);
if (mysqli_num_rows($result) != 0)
{
mysqli_free_result($result);
echo "<script type='text/javascript'>";
echo "alert('Account already in use! Please choose another username');";
echo "history.back();";
echo "</script>";
}
else
{
mysqli_free_result($result);
$sql = "INSERT INTO users (account, password, name, sex,
year, month, day, telephone, address,
email, comment) VALUES ('$account', '$password',
'$name', '$sex', $year, $month, $day, '$telephone',
'$address', '$email', '$comment')";
$result = execute_sql($link, "member", $sql);
echo "User added successfully!";
}
mysqli_close($link);
?>
join.html
<form action="addmember.php" method="POST" name="myForm">
(Different types of input)
<input type="submit" value="Add">
My aim is to add a member data into the database after the user clicked the Add button on the form in join.html. However the page could run echo "User added successfully!"; this line but the problem is the database could not get updated even though I already called execute_sql command. May I ask what is missing in order to be connected with the database?

How to insert input, text area and image upload into database

Hye, currently I follow some tutorial in the video, but I don't know how my data doesn't upload pic into a database.
This is happening in my database when I put some data in a form and my pic still doesn't upload
This is code in PHP
<?php
$sqlservername = "127.0.0.1";
$sqlusername = "root";
$sqlpassword = "";
$sqldbname ="test";
$conn = new mysqli($sqlservername, $sqlusername, $sqlpassword, $sqldbname);
if($conn->connect_error){
die("Connection fail");
}
session_start();
if(isset($_POST['li_submit'])){
$_SESSION['li_username'] = $_POST['username'];
$li_username = $_SESSION['li_username'];
}
if(isset($_POST['submit'])){
$id = $username = $password = $location = $description = "";
if(isset($_POST['username']))
{
$id = uniqid($prefix='u_');
$username = $_POST['username'];
}
if(isset($_POST['password'])){
$password = $_POST['password'];}
if(isset($_POST['location'])){
$location = $_POST['location'];
}
if(isset($_POST['description']))
{
$description = $_POST['description'];
}
if(isset($_FILES['profile_pic'])){
$target_dir = "IMG_UPLOAD/";
$target_file = basename($_FILES['profile_pic']["name"]);
$file_type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$target_path = $target_dir . uniqid($prefix='img.'). "." .$file_type;
if(!move_uploaded_file($_FILE['profile_pic']['tmp_name'], $target_path)){
$target_path = "";
}
echo $target_path;
}
$sql_insert = $conn->prepare("insert into user_info(id, username, password,
location, description, image_path) values (?,?,?,?,?,?)");
$sql_insert->bind_param("ssssss", $id, $username, $password, $location,
$description, $target_path);
$sql_insert->execute();
$sql_insert->close();
}
$conn->close();
?>
and this code in HTML incase you want to see it
<body>
<h1>Profile</h1>
<form method='post' enctype=''multipart/form-data'>
<input type='name' name='username' placeholder='Username'/>
<input type='password' name='password' placeholder='Password'/>
<input type='test' name='location'placeholder='Location'/>
<textarea name='description'>Enter about yourself</textarea>
<input type='file' name='profile_pic'/>
<input type='submit' name='submit' value='save'/>
</form>
</body>
i expect when i press save button, all my data that i had put will save a to a database including picture based on screenshot. Please help me, im still newbie in PHP
you have another typo in the following line. It will be $_FILES not $_FILE.
if(!move_uploaded_file($_FILE['profile_pic']['tmp_name'], $target_path)){
for inserting data into the database, use the following code:
$sql_insert = mysqli_query($conn, "insert into user_info (username, password, location, description, image_path) values ('$username', '$password', '$location', '$description', '$target_path')");
Set id as primary key and auto increment.
Also make sure you have created a folder named "IMG_UPLOAD" in the directory containing your php file.

Uploaded File is not moving in specified folder in php

I have a php file named abstract_submission and to process the form in that file I created submitted_abstract php file. but move_uploaded file function does not working. for files I created a folder named submissions. Does anyone have a solution to this?
I tried shifting insert query up and down in if statement but it doesn't work.
<?php
$con = mysqli_connect("localhost",MYSQL_USER,MYSQL_PASSWORD,"svavpo_phicon");
ini_set('display_errors', 1);
ini_set('display_startup-errors', 1);
error_reporting(E_ALL);
$edit_state = false;
if(isset($_POST['sybmitabs']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$category = $_POST['category'];
$subm_category = $_POST['subm_category'];
$main_category = $_POST['main_category'];
$title = $_POST['title'];
$author = $_POST['author'];
$body = $_POST['body'];
$reference = $_POST['reference'];
$file = $_FILES['file']['name'];
$target = "submissions/".basename($_FILES['file']['name']);
$sql = "INSERT INTO `sub_abstract`(`name`, `email`, `category`, `subm_category`, `main_category`, `title`, `author`, `body`, `reference`, `file`) VALUES ('$name','$email','$category','$subm_category','$main_category','$title','$author','$body','$reference','$file')";
$result=mysqli_query($con,$sql);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "errro in updating db";
}
else
{
echo ("<script LANGUAGE='JavaScript'>
window.alert('Abstract Submitted Succesfully !');
window.location.href='../abstract_submission.php';
</script>");
}
}
?>

if statement resetting form data variable

If user uploads wrong file then if statement resetting $name to blank. What is the problem in following code?
if(isset($_POST['submit'])){
$conn= new mysqli('localhost','root','','dilip');
if(!$conn){
die("Not connect".mysqli_error);
}
$stm = $conn->prepare("Insert into comment (Name, email, Message, Image) values (?, ?, ?, ?)");
$stm->bind_param('ssss',$name,$email,$message,$image);
$name = $_POST['name'];
$message = $_POST['readerInput'];
$email = $_POST['email'];
$image='upload/comment/default.jpg';
$time = date("s");
//if(isset($_FILES['image'])){
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$tmp_image = $_FILES['image']['size'];
$finfo = new finfo(FILEINFO_MIME_TYPE);
$file = $finfo->file($_FILES['image']['tmp_name']); //This line checks MIME Type of uploaded image
if($file!=='image/jpeg' && $file !=='image/gif' || $tmp_image > 1024*1024*2){ //1024*1024*2 = 2MB
echo "Upload your Profile Image in jpg/gif format and lower than 2mb. Otherwise continue without Image.";
}
else{
$photo = move_uploaded_file($_FILES['image']['tmp_name'],'upload/comment/'.$name.$time.'.jpg');
$image = 'upload/comment/'.$name.$time.'.jpg';
echo '<script> alert("Your file is accepted.")</script>';
$stm->execute();
$message = 'Your Comment';
$name = 'Your Name';
$email = 'Your eMail';
}
}else{
$stm->execute();
$message = 'Your Comment';
$name = 'Your Name';
$email = 'Your eMail';
}
}
Please try with below snippet. You are preparing insert statement before variable initialization and image upload code. I have moved code below file upload code.
if(isset($_POST['submit'])){
$flag = 0;
$conn= new mysqli('localhost','root','','dilip');
if(!$conn){
die("Not connect".mysqli_error);
}
$name = $_POST['name'];
$message = $_POST['readerInput'];
$email = $_POST['email'];
$image='upload/comment/default.jpg';
$time = date("s");
//if(isset($_FILES['image'])){
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$tmp_image = $_FILES['image']['size'];
$finfo = new finfo(FILEINFO_MIME_TYPE);
$file = $finfo->file($_FILES['image']['tmp_name']); //This line checks MIME Type of uploaded image
if($file!=='image/jpeg' && $file !=='image/gif' || $tmp_image > 1024*1024*2){ //1024*1024*2 = 2MB
echo "Upload your Profile Image in jpg/gif format and lower than 2mb. Otherwise continue without Image.";
$flag = 1;
}else{
$photo = move_uploaded_file($_FILES['image']['tmp_name'],'upload/comment/'.$name.$time.'.jpg');
$image = 'upload/comment/'.$name.$time.'.jpg';
echo '<script> alert("Your file is accepted.")</script>';
}
}
$stm = $conn->prepare("Insert into comment (Name, email, Message, Image) values (?, ?, ?, ?)");
$stm->bind_param($name,$email,$message,$image);
if($flag==0){
$stm->execute();
}
}

Problems with sessions and mysql

i'm creating a site for a client and i get an error message saying "undefined index". I'm trying to upload data to a database from 3 multi form pages and they are handled by cv.php.
The form details are stored on page 2
<?php
session_start();
if(isset($_SESSION['FirstName'])){
$_SESSION['FirstName'] = $_POST['FirstName'];}
if(isset($_SESSION['LastName'])){
$_SESSION['LastName'] = $_POST['LastName'];}
if(isset($_SESSION['dob'])){
$_SESSION['dob'] = $_POST['dob'];}
if(isset($_SESSION['Age'])){
$_SESSION['Age'] = $_POST['Age'];}
if(isset($_SESSION['AddressLine1'])){
$_SESSION['AddressLine1'] = $_POST['AddressLine1'];}
if(isset($_SESSION['AddressLine2'])){
$_SESSION['AddressLine2'] = $_POST['AddressLine2'];}
if(isset($_SESSION['City'])){
$_SESSION['City'] = $_POST['City'];}
if(isset($_SESSION['County'])){
$_SESSION['County'] = $_POST['County'];}
if(isset($_SESSION['PostCode'])){
$_SESSION['PostCode'] = $_POST['PostCode'];}
if(isset($_SESSION['Country'])){
$_SESSION['Country'] = $_POST['Country'];}
if(isset($_SESSION['Telephone'])){
$_SESSION['Telephone'] = $_POST['Telephone'];}
if(isset($_SESSION['Mobile'])){
$_SESSION['Mobile'] = $_POST['Mobile'];}
if(isset($_SESSION['Email'])){
$_SESSION['Email'] = $_POST['Email'];}
?>
Page 3
<?php
session_start();
if(isset($_SESSION['Skills'])) {
$_SESSION['Skills'] = $_POST['Skills'];}
if(isset($_SESSION['ReasonApp'])){
$_SESSION['ReasonApp'] = $_POST['ReasonApp'];}
if(isset($_SESSION['WorkName'])){
$_SESSION['WorkName'] = $_POST['WorkName'];}
if(isset($_SESSION['WorkDesc'])){
$_SESSION['WorkDesc'] = $_POST['WorkDesc'];}
if(isset($_SESSION['W_AddressLine1'])){
$_SESSION['W_AddressLine1'] = $_POST['W_AddressLine1'];}
if(isset($_SESSION['W_AddressLine2'])){
$_SESSION['W_AddressLine2'] = $_POST['W_AddressLine2'];}
if(isset($_SESSION['W_City'])){
$_SESSION['W_City'] = $_POST['W_City'];}
if(isset($_SESSION['W_Telephone'])){
$_SESSION['W_Telephone'] = $_POST['W_Telephone'];}
?>
And my CV.php
<?
session_start();
ini_set('display_errors',1);
error_reporting(E_ALL);
//include connection profile
require_once("Sql/con.php");
include("config.php");
//declare variables with sessions
$FirstName = $_SESSION['FirstName'];
$LastName = $_SESSION['LastName'];
$dob = $_SESSION['dob'];
$Age = $_SESSION['Age'];
$AddressLine1 = $_SESSION['AddressLine1'];
$AddressLine2 = $_SESSION['AddressLine2'];
$PostCode = $_SESSION['PostCode'];
$City = $_SESSION['City'];
$County = $_SESSION['County'];
$Country = $_SESSION['Country'];
$Mobile = $_SESSION['Mobile'];
$Telephone = $_SESSION['Telephone'];
$Email = $_SESSION['Email'];
$Skills = $_SESSION['Skills'];
$ReasonApp = $_SESSION['ReasonApp'];
$SchoolName = $_SESSION['SchoolName'];
$Course = $_SESSION['Course'];
$Certificate = $_SESSION['Certificate'];
$DateFrom = $_SESSION['DateFrom'];
$DateTo = $_SESSION['DateTo'];
$CollName = $_SESSION['CollName'];
$CollQualification = $_SESSION['CollQualification'];
$CollYear = $_SESSION['CollYear'];
$WorkName = $_SESSION['WorkName'];
$WorkDesc = $_SESSION['WorkDesc'];
$W_AddressLine1 = $_SESSION['W_AddressLine1'];
$W_AddressLine2 = $_SESSION['W_AddressLine2'];
$W_PostCode = $_SESSION['PostCode'];
$W_City = $_SESSION['City'];
$W_Telephone = $_SESSION['Telephone'];
//database connection
$dblink = mysqli_connect($mysql_host,$mysql_user,$mysql_pw,$mysql_db) OR DIE ("Unable to
connect to database! Please try again later.");
//inserting information into tables
$order = "INSERT INTO CV_personal
(FirstName,LastName,dob,Age,AddressLine1,AddressLine2,PostCode,City,County,Country,Mobile,Telephone,Email,Skills,ReasonApp,SchoolName,Course,Certificate,DateFrom,DateTo,CollName,CollQualification,CollYear,WorkName,WorkDesc,W_AddressLine1,W_AddressLine2,W_City,W_Telephone)
VALUES
('$FirstName',
'$LastName',
'$dob',
'$Age',
'$AddressLine1',
'$AddressLine2',
'$PostCode',
'$City',
'$County',
'$Country',
'$Mobile',
'$Telephone',
'$Email',
'$Skills',
'$ReasonApp',
'$SchoolName',
'$Course',
'$Certificate',
'$DateFrom',
'$DateTo',
'$CollName',
'$$CollQualification',
'$ColYear',
'$WorkName',
'$WorkDesc',
'$W_AddressLine1',
'$W_AddressLine2',
'$W_PostCode',
'$W_City',
'$W_Telephone',)";
//declare in the order variable
$result = mysqli_query($dblink, $order); //order executes
?>
On my final page do i need to had my form into the session because i declared a variable for them on Cv.php ?
Thank you
In your first two blocks of code, you should be checking if the $_POST[...] is set, not the $_SESSION[...] because that it what you are assigning, so it won't cause an error.
On CV.php you should check whether the $_SESSION[...] exists before assigning it to a variable or else it WILL cause an error.
Tip:
If you are going to name your variables exactly the name of all your keys in the $_SESSION array. You can just substitute you many lines with this single line:
extract($_SESSION);
More on extract: http://www.php.net/extract

Categories