Hello guys. Lately I have problems in uploading my image on my website because I don't know how to upload my image and store it in phpmyadmin database and I don't know how to display the image. I've been stuck for this problems for a month. So luckily I managed to solved this using Path Method which is create path inside of your PHPmyadmin database. You guys should be grateful for me to store this code for you guys to refer so you guys don't feel the way that I feel sticks for a months.
So click this video on my youtube channel to watch my explaination.
Still not understand???
Here is the coding for you to refer.
insert.php
<?php
include "connect.php";
session_start();
if(isset($_SESSION["username"])){
$username = $_SESSION ["username"];
}else{
header('Location: loginAdmin.php');
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>BANGSA JOHOR BAHAGIA</title>
<link rel="stylesheet" href="css/layoutstyle.css" type="text/css">
<!-- internal css -->
<style media="screen">
.logo {
width: 180px;
height: 80px;
margin-left: 20px;
}
</style>
<style>body {
background-image: url("images/p2.jpg");
}</style>
<script>
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user=getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:","");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
}
</script>
</head>
<body onload="checkCookie()">
<!-- main box of content -->
<div class="main">
<!-- box of header -->
<div class="header">
<?php include "headerA.php"; ?>
</div>
<!-- box of content -->
<div class="Mcontent">
<br><br><br><br>
<table align="center" border="0" width="50%" cellpadding="0" cellspacing="0">
<td><hr></td>
<td width="30%" align="center"><h2><font color="whitesmoke">Register New Class</font></h2></td>
<td><hr></td>
<style>table{
background-image: url("images/p2.jpg");
}</style>
</table>
<!-- Register Books Content -->
<form action="insert.php" method="post" enctype="multipart/form-data">
<table align="center" border="0" width="40%" cellpadding="5" cellspacing="0">
<tr>
<td align="center" width="30%">Gambar Kelas</td>
<td><input type="file" name="pic" id="pic"></td>
</tr>
<tr>
<td align="center" width="30%"> ID Kelas</td>
<td><input class="Minput" type="text" name="id" autofocus required></td>
</tr>
<tr>
<td align="center" width="30%">Nama Kelas</td>
<td><input class="Minput" type="text" name="name" required></td>
</textarea></td>
</tr>
<tr>
<td align="center" width="30%">Sinopsis Kelas</td>
<td><textarea name="synopsis" rows="4" cols="100" class="Minput" id="message" required=""></textarea>
</tr>
<tr>
<td align="center" width="30%">Harga</td>
<td><input class="Minput" type="number" step=".01" name="price" required></td>
</tr>
<tr>
<td align="center" width="30%">Hari Kelas</td>
<td><select name="day" required>
<option value="Isnin"> Isnin</option>
<option value="Selasa"> Selasa </option>
<option value="Rabu"> Rabu </option>
<option value="Khamis"> Khamis </option>
<option value="Jumaat"> Jumaat</option>
<option value="Sabtu"> Sabtu</option>
<option value="Ahad">Ahad</option>
</select></td>
</tr>
<tr>
<td><input class="Lbtn" type="submit" name="submit" value="submit"></td>
<td><input type="reset" class="reset" id="button" onclick="resett()" value="Set Semula"></td>
</tr
</table>
</form>
</div>
<!-- box of footer -->
<br>
<?php
if(ISSET($_POST['submit'])){
$id = $_POST['id'];
$name = $_POST['name'];
$synopsis = $_POST['synopsis'];
$price = $_POST['price'];
$day = $_POST['day'];
$target_dir = "image/";
if (is_uploaded_file($_FILES['pic']['tmp_name'])){
$target_file = $target_dir . basename($_FILES["pic"]["name"]);
$filename = basename($_FILES["pic"]["name"]);
}
else{
$target_file = $target_dir . "no-image.png";
$filename = "none";
}
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$sql = "INSERT INTO product (prod_id, prod_name, prod_syn, prod_price, prod_day, filename, filepath)
VALUES ('$id', '$name', '$synopsis', '$price', '$day','$filename', '$target_file')";
if (is_uploaded_file($_FILES['pic']['tmp_name'])) {
$check = getimagesize($_FILES["pic"]["tmp_name"]);
if ($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
}
else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["pic"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
echo "Sorry, your file was not uploaded.";
else {
if (move_uploaded_file($_FILES["pic"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["pic"]["name"]). " has been uploaded.";
}
else
echo "Sorry, there was an error uploading your file.";
}
}
$insert = mysqli_query($conn, $sql);
if($insert){
echo '<script language="javascript">';
echo 'alert ("Insert Success");';
echo 'window.location.href="insert.php";' ;
echo '</script>';
}
else{
echo '<script language="javascript">';
echo 'alert ("Insert Fail");';
echo 'window.location.href="insert.php";' ;
echo '</script>';
}
mysqli_close($conn);
}
?>
<?php
include "footer.php";
?>
</body>
</html>
viewproductuser.php
<?php
include "connect.php";
session_start();
if(isset($_SESSION["username"])){
$username = $_SESSION ["username"];
}else{
header('Location: login.php');
}
?>
<style>table td{
text-align:center;
background-color: #FFFFFF;
color: black
} </style>
<?php
include "header.php";
include "connect.php";
?>
<div id="main">
<form action="index.php" align=left>
<button type="submit"> Balik </button>
</form>
<h1>Servis yang Disediakan</h1>
<?php
$sql = ("SELECT * FROM product");
$result = mysqli_query($conn, $sql);
echo "<table border=2>";
echo "<tr><td></td><td></td</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr><td><b>PRODUCT PHOTO</b></td>";
echo "<td>";
?>
<?php
echo "<br>";
echo "</td>";
echo "<tr><td><b>ID No</b></td>";
echo "<td>".$row["prod_id"]."</td>";
echo "<tr><td><img src='" . $row["filepath"] . "'></td>";
echo "<tr><td><b> Nama</b></td>";
echo "<td>".$row["prod_name"]."</td>";
echo "<tr><td><b> Sinopsis Kelas</b></td>";
echo "<td>".$row["prod_syn"]."</td>";
echo "<tr><td><b> Harga</b></td>";
echo "<td>".$row["prod_price"]."</td>";
echo "<tr><td><b> Sesi Berlansung</b></td>";
echo "<td>".$row["prod_day"]."</td>";
echo "<tr><td><b></b></td>";
echo "<td><a href=\"registerService1.php?id=".$row["prod_id"]."\">Daftar</a>";
}
echo "</table>";
?>
</div>
<style>body {
background-image: url("images/p2.jpg");
}</style>
<?php
include "footer.php";
?>
0 Comments