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

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.

Related

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

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();
}

insert record using mysqli with one page

I'm wondering what's the problem with this code?
What I want to happen is to insert record when I click the Submit Button. But it seems I'm having a problem with the isset function.
Database Name: dbase
Table Name: tblmessage
Fields:
message_id - INT - auto increment
message - TEXT
Update:
I can't still add / insert record in my database.
Thank you in advance!
<html>
<head></head>
<body>
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
Message: <input type = "text" name = "message">
</br></br>
<input type = "submit" name = "submit">
</form>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['message'])) {
$conn = mysqli_connect("localhost", "root", "","dbase");
$message = $_POST['message'];
$sql = ""INSERT INTO tblmessage (message_id, message) VALUES (NULL, '$message')";
$insert = mysqli_query($conn,$sql);
if ($insert) {
echo "Message successfully added!";
}
else {
echo "Error" . mysqli_error($conn);
}
}
}
mysqli_close($conn);
?>
</body>
</html>
Working Code just copy and paste it
<html>
<head></head>
<body>
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
Message: <input type = "text" name = "message">
</br></br>
<input type = "submit" name = "submit">
</form>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['message'])) {
$conn = mysqli_connect("localhost", "root", "", "dbase");
$message = $_POST['message'];
$sql = "INSERT INTO tblmessage (message_id, message) VALUES (NULL, '" . $message . "')";
$insert = mysqli_query($conn, $sql);
mysqli_close($conn);
if ($insert) {
echo "Message successfully added!";
} else {
echo "Error" . mysqli_error($conn);
}
}
}
?>
</body>
</html>
You're trying to implode a String. Read about implode.
Change:
$sql = "INSERT INTO tblmessage (message) VALUES (NULL, ".implode(',',$message).")";
$insert = mysqli_query($conn,$sql);
To:
$sql = "INSERT INTO tblmessage (message_id, message) VALUES (NULL, '$message')";
$insert = mysqli_query($conn,$sql);
You don´t have any SQL-Statement in your Code.
If you want to insert the Message from your form you need to change $sql.
$sql = INSERT into dbase(your_database_field) Values ($message);
$sql-statement=mysqli_query($conn, $sql);
You should sanitize your input before sending your data to the database.

Can't make the user update there information

Here is the code, I search in the net for the UPDATE statement, and this is what I learned, but even if it doesn't make an error, it doesnt change the information in database.
update.inc.php:
<?php
session_start();
include '../data_base.php';
include 'header.php';
$first = $_POST['first'];
$last = $_POST['last'];
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$number = $_POST['number'];
$age = $_POST['age'];
$email = $_POST['email'];
$sql = "UPDATE user
SET first = $first, last = $last, uid = $uid, pwd = $pwd, number = $number, age = $age, email = $email
WHERE id = $id";
$result = mysqli_query($conn,$sql);
header("Location: ../index.php");
?>
update.php:
<?php
include 'header.php';
?>
<?php
if (isset($_SESSION['id'])) {
echo "You Can Edit Your Files!";
} else {
echo "You Need To Create An Account First!";
}
?>
<br><br><br>
<?php
if (isset($_SESSION['id'])) {
echo "<form action='includes/update.inc.php' method='POST'>
<input type='text' name='first' placeholder='FirstName'><br>
<input type='text' name='last' placeholder='LastName'><br>
<input type='text' name='uid' placeholder='UserName'><br>
<input type='password' name='pwd' placeholder='Password'><br>
<input type='text' name='number' placeholder='Telemóvel'><br>
<input type='text' name='age' placeholder='Idade'><br>
<input type='text' name='email' placeholder='Email'><br>
<button type='submit'>EDIT</button>
</form>";
}
?>
Need Help.
First enable error in your file and change your query with
$sql = "UPDATE user
SET first = '$first', last = '$last', uid = '$uid', pwd = '$pwd', number = '$number', age = '$age', email = '$email'
WHERE id = $id";
enable errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Your code is open to sql injection so I would like to prefer to make use of prepared statement with mysqli(preferred is PDO one)
$sql = "UPDATE user
SET first = ?,
last = ?,
uid = ?,
pwd = ?,
number = ?,
age = ?,
email = ?
WHERE id = ?";
$stmt = $mysqli->prepare($sql);
// here s represnts string and i represents integer to the corresponding variable
// example $firstname is string, i $uid is integer
$stmt->bind_param("ssisiisi",$firstname,$lastname,$uid,$pwd,$number,$age,$email,$id);
$first = $_POST['first'];
$last = $_POST['last'];
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$number = $_POST['number'];
$age = $_POST['age'];
$email = $_POST['email'];
// in above your $id is missing
$id = $_POST["id"];
// now execute the statement now your database changes
$stmt->execute();
echo "Record updated successfylly";
header("Location: ../index.php");

how to Save image in sever folder

This is my php code for image upload.with some text.This script is used in an android app code.
Image id ,Image url and text save in database correctly but image not saved in server folder. Why is happen?
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_FILES['image'];
$fullname = $_POST['fullname'];
require_once('dbConnect.php');
$sql ="SELECT id FROM uploadfinding ORDER BY id ASC";
$res = mysqli_query($conn,$sql);
$id = 0;
while($row = mysqli_fetch_array($res)){
$id = $row['id'];
}
$path = "uploads/$id.png";
$actualpath = "My_url/$path";
// query for db
$sql = "INSERT INTO uploadfinding (image,fullname) VALUES ('$actualpath','$fullname')";
if(mysqli_query($conn,$sql)){
file_put_contents($actualpath,base64_decode($image));
echo "Successfully Uploaded";
}
mysqli_close($conn);
}else{
echo "Error";
}
?>
This one is the working code for this question
<?php
header("content-type:application/json");
require_once('dbConnect.php');
if($_SERVER['REQUEST_METHOD']=='POST'){
if(isset($_FILES['image'])){
$errors= array();
$fullname = $_POST['fullname'];
$location = $_POST['location'];
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$date=date('Y-m-dH:i:s');
$path="folder_name/".$date.$file_name;
$actualpath = "http:myUrl/uploadfinding/$path";
if(empty($errors)==true){
move_uploaded_file($file_tmp,$path);
$sql = "INSERT INTO uploadfinding (image,fullname,location,) VALUES ('$actualpath','$fullname','$location',)";
if(mysqli_query($conn,$sql)){
echo "message";
}
}else{
print_r($errors);
}
}
}
?>
Make sure you have added 'enctype' => 'multipart/form-data', for example:
<form method="post" name="abc" enctype="multipart/form-data">

OO Profile picture upload through PHP

I have developed a registration form using object-oriented techniques, it works fine except when I try and incorportate a file upload so that the user can have a picture saved in the database.
In my database I am using the BLOB format and using file_get_contents to retrieve the file uploaded.
I previously had $profilepic = $_POST['photo']; which is why $_POST['photo'] is still in the second if statement.
I'm a little confused what format to use as I've not done a lot of object oriented.
Thanks in advance
<center>
<?php
session_start();
include 'registrationform.php';
include 'connection.php';
if (isset($_POST['regsubmit']))
{
$firstname = $_POST['firstname'];
$firstname = ucfirst($firstname);
$lastname = $_POST['lastname'];
$lastname = ucfirst($lastname);
$user = $_POST['username'];
$user = ucfirst($user);
$pass = $_POST['password'];
$spass = $_POST['secondpassword'];
$profilepic = file_get_contents($_FILES['photo']['tmp_name']);
if($_POST['firstname'] && $_POST['lastname'] && $_POST['photo'] && $_POST['username'] && $_POST['password'] && $_POST['secondpassword'])
{
if ($spass == $pass)
{
$query = "INSERT INTO users (firstname, lastname, photo, username, password) VALUES(?, ?, ?, ?, ?)";
$statement = $connection->prepare($query);
$statement->bind_param('ssbss', $firstname, $lastname, $profilepic, $user, $pass);
if($statement->execute()){
print 'Success!';
}else
{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();
}
else
{
print 'The passwords do not match!';
}
}
else
{
print 'Enter all fields please';
}
}
?>
</center>
You can move uploaded image in folder using move_uploaded_file() and store image name in database.
You can get image name as following:
$image_name = $_FILES["photo"]["name"];
For showing image you can retrieve image name.
Then in html you can show image like following:<img src="YOUR_IMAGE_FOLDER_PATH/".$IMAGE_NAME>

Categories