Tuesday, March 6, 2018

Upload Picture in the Database Using PHP and MySQLi

In this article I would like to share with you a code that I wrote that enables you to upload a picture in your database using PHP and MySQLi. The code is very easy to understand and use. I hope you will find my work useful.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.












Sample Program Output


Program Listing


users.sql

-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 06, 2018 at 12:57 PM
-- Server version: 10.1.30-MariaDB
-- PHP Version: 7.2.2

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `login`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(200) NOT NULL,
  `password` varchar(200) NOT NULL,
  `lastname` varchar(200) NOT NULL,
  `firstname` varchar(200) NOT NULL,
  `pix` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `username`, `password`, `lastname`, `firstname`, `pix`) VALUES
(1, 'jake', 'jake', 'POMPERADA', 'JAKE', '/login/images/jake.jpg'),
(2, '123', '123', 'POMPERADA', 'JACOB SAMUEL', '/login/images/jacob.jpg'),
(3, 'iya', 'iya', 'POMPERADA', 'JULIANNA RAE', '/login/images/iya.jpg'),
(4, 'allie', 'allie', 'POMPERADA', 'MA. JUNALLIE', '/login/images/allie.jpg'),
(5, 'bill', 'bill', 'GATES', 'WILLIAM', '/login/images/gates.jpg'),
(6, 'peter', 'peter', 'NORTON', 'PETER', '/login/images/norton.jpg'),
(15, 'alan', 'alan', 'TURING', 'ALAN', '/login/images/turing.jpg'),
(16, 'neil', 'neil', 'ARMSTRONG', 'NEIL ALDEN', '/login/images/armstrong.jpg'),
(18, 'kobe', 'kobe', 'BRYANT', 'KOBE', '/login/images/kobe.jpg'),
(21, 'larry', 'lary', 'BIRD', 'LARRY', '/login/images/bird.jpg'),
(22, 'linus', 'linus', 'LINUS', 'TORWALDS', '/login/images/linux.jpg'),
(23, 'james', 'james', 'GOSLING', 'JAMES', '/login/images/gosling.jpg');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=24;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

connect_to_database.php

<?php  
$localhost = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "login"; 
 
// create connection 
$connect = new mysqli($localhost, $username, $password, $dbname); 
 
// check connection 
if($connect->connect_error) {
    die("connection failed : " . $connect->connect_error);
} else {
    // echo "Successfully Connected";
}
  
?>


upload.php

<?php
error_reporting(0);
include 'connect_to_database.php'; 

$username = $_POST['username'];
$password = $_POST['password'];
$lastname = $_POST['lastname'];
$firstname =$_POST['firstname'];

$last = strtoupper($lastname);
$first = strtoupper($firstname);
  
  $msg = "";


  if (isset($_POST['upload']))  {

$image2 = $_FILES['image']['name'];
  $image = "/login/images/".$_FILES['image']['name'];


  // image file directory
  $target = "/xampp/htdocs/login/images/".basename($image2);

  $sql = "INSERT INTO users (username,password,lastname,firstname,pix) VALUES ('$username','$password','$last','$first','$image')";
  // execute query
  mysqli_query($connect, $sql);

  if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
 
header('Location: success.php');
  }else{
  $msg = "Failed to upload record and image.";
  }
  }
 
?>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
<style type="text/css">
   #content{
    width: 50%;
    margin: 20px auto;
    border: 1px solid #cbcbcb;
   }
   form{
    width: 50%;
    margin: 20px auto;
   }
   form div{
    margin-top: 5px;
   }
   #img_div{
    width: 80%;
    padding: 5px;
    margin: 15px auto;
    border: 1px solid #cbcbcb;
   }
   #img_div:after{
    content: "";
    display: block;
    clear: both;
   }
   img{
    float: left;
    margin: 5px;
    width: 300px;
    height: 140px;
   }
   
body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
    }
input, button, select, option, textarea {
    font-size: 100%;
}

</style>
</head>
<body>
<div id="content">
    <form method="POST" action="upload.php" enctype="multipart/form-data">
  <input type="hidden" name="size" value="1000000">
<table cellspacing="0" cellpadding="0">
            <tr>
                <th>Username &nbsp;&nbsp;</th>
                <td><input type="text" name="username" placeholder="Name" required/> &nbsp;</td>
            </tr>   
            <tr>
                <th>Password</th>
                <td><input type="text" name="password" placeholder="Password" /></td>
            </tr>
<tr>
                <th>Lastname</th>
                <td><input type="text" name="lastname" placeholder="Lastname" /></td>
            </tr>
<tr>
                <th>Firstname</th>
                <td><input type="text" name="firstname" placeholder="Firstname" /></td>
            </tr>
</table>
  <div>
    <input type="file" name="image">
  </div>
    <div>
  <button type="submit" name="upload">POST</button>
  </div>
  </form>
  
  <?php
    echo $msg;
 ?>
</div>
</body>
</html>

success.php

<?php

$msg = "<h1> Record and Image uploaded successfully. </h1>";

echo $msg;

?>





No comments:

Post a Comment