Image/File upload doesn't work with materializecss framework - php

I want to create a post creator which works with a database, but I can't upload an image or a file. My web-page is using the materialize framework. This code is mostly shrunk down to the important parts.
<?php
require_once 'include/Database.php';
require_once 'include/Sessions.php';
require_once 'include/Functions.php';
if (isset($_POST['addPostButton']) || isset($_FILES['image'])) {
$imageName = $_POST['image'];
$imageDirectory = 'uploads/' . basename($_FILES['image']['name']);
if (!move_uploaded_file($_FILES['image']['tmp_name'], $imageDirectory)) {
$_SESSION['ErrorMessage'] = 'File is invalid!';
} else {
global $ConnectingDB;
$stmt = $ConnectingDB->prepare("INSERT INTO posts(image) VALUES('$imageName')");
if ($stmt->execute()) {
$_SESSION['SuccessMessage'] = 'Post created';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Dashboard</title>
<link type="text/css" rel="stylesheet" href="css/materialize.css">
<link type="text/css" rel="stylesheet" href="css/styles.css">
</head>
<body>
<form action="add-post.php" method="post">
<div class="row">
<div class="file-field input-field">
<div class="btn green">
<span>File</span>
<input type="file" name="image">
</div>
<div class="file-path-wrapper">
<input type="text" class="file-path" placeholder="Choose an image">
</div>
</div>
</div>
</form>
</body>
</html>

From
<form action="add-post.php" method="post">
To
<form action="add-post.php" method="post" enctype="multipart/form-data">
Here need to add a form attribute enctype to send files.

Related

I am try to add a admin panel to upload videos on my movie streaming website

So I have created a streaming website and I want to create an admin panel so that admin can add a preview, a thumbnail, the category and the main video to the database. I tried in this way but this isn't working so any suggestions where I might be going wrong?
The error I am getting is "Notice : Trying to get property 'error' of non-object in adminhandler.php on line 33"
admin.php
<?php
include("config.php");`enter code here`
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ShortFilm</title>
<link rel="stylesheet" href="./css/global.css">
<link rel="stylesheet" href="./css/admin.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" />
</head>
<body>
<!-- Content Starts -->
<div class="admin-box">
<form action="adminHandler.php" method="POST" class="admin-box-form" enctype="multipart/form-data">
<h3 class="admin-box-title">Add Movie</h3>
<div class="form-group">
<label htmlFor="name">Movie Name</label>
<input type="text" name="name" id="name" placeholder="Enter movie name" value="" required />
</div>
<div class="form-group">
<label htmlFor="category_id">Movie Genre</label>
<input type="number" name="category_id" id="category_id" placeholder="Enter movie genre" value="" required />
</div>
<div class="form-group">
<label htmlFor="thumbnail">Movie Cover</label>
<input type="file" name="thumbnail" id="thumbnail" required/>
</div>
<div class="form-group">
<label htmlFor="preview">Movie Preview</label>
<input type="file" name="preview" id="preview" required/>
</div>
<!-- <div class="form-group">
<label htmlFor="filePath">Movie File</label>
<input type="file" name="filePath" id="filePath" required/>
</div> -->
<button type="submit" class="btn btn-primary" name="admin_form" value="Submit">
Submit
</button>
</form>
</div>
</body>
</html>
adminhandler.php
<?php
// Change the upload limits in php.ini
// ini_set('upload_max_filesize', '50M');
// ini_set('post_max_size', '50M');
// ini_set('max_input_time', 300);
// ini_set('max_execution_time', 300);
if (isset($_POST['admin_form'])) {
include 'config.php';
// File Targets
$targetCover = "../entities/thumbnail/" . basename($_FILES['thumbnail']['name']);
$targetMovie = "../entities/previews/" . basename($_FILES['preview']['name']);
// Store in DB
$name = strtolower($_POST['name']);
$category= strtolower($_POST['categoryId']);
$thumbnail = "entities/thumbnail/" . basename($_FILES['thumbnail']['name']);
$filePath = "entities/previews/" . basename($_FILES['preview']['name']);
$sql = "INSERT INTO entities(name, thumbnail, preview, categoryId) VALUES('$name','$thumbnail', '$filePath','$category')";
$result = $con -> query($sql);
if ($result && move_uploaded_file($_FILES['thumbnail']['name'], $targetCover) && move_uploaded_file($_FILES['preview']['name'], $targetMovie)) {
echo "<script type='text/javascript'>alert('Movie Added Successfully! Press OK to Redirect to Admin Page.')</script>";
echo '<script type="text/javascript">window.location.href="admin.php"</script>';
}
else {
echo "<script type='text/javascript'>alert(`Error Uploading Movie! Press Try Again.\n{$con -> error}`)</script>";
echo '<script type="text/javascript">window.location.href="admin.php"</script>';
}
}
?>
Database Tables
enter code here

How to use span tags in php error statements

The echo statement is not printing when the answer does not equal to 8.So if the answer is other than 8 it should print in red "Please try again " underneath the textbox.All it shows is nothing when I click the register button.
<?php
session_start();
$nameErr = "";
$answer = $_POST['lastname'];
if ($_POST['reg']){
if ($answer != '8') {
echo "<span style='color:red'>Please enter Correct Answer</span>";
}
}
?>
<!Doctype html>
<head>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"
type="text/css">
<link href="bootstrap/css/styles.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="Register.css">
<script src='https://www.google.com/recaptcha/api.js'></script>
<title> Register</title>
</head>
<html>
<body>
<form method="post" action="">
What is 5 + 3 =<br>
<input type="text" name="lastname">
<div class="row">
<div class="form-group col-xs-5 col-xs-offset-3.5">
<input class="btn btn-primary btn-lg btn-block" value="Register"
type="submit" name="reg">
You need to put the echo in the <body> of the document. Also, <html> needs to be at the top.
<?php
session_start();
$nameErr = "";
?>
<!Doctype html>
<html>
<head>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"
type="text/css">
<link href="bootstrap/css/styles.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="Register.css">
<script src='https://www.google.com/recaptcha/api.js'></script>
<title> Register</title>
</head>
<body>
<form method="post" action="">
What is 5 + 3 =<br>
<input type="text" name="lastname">
<?php
if (isset($_POST['reg'])){
$answer = $_POST['lastname'];
if ($answer != '8') {
echo "<span style='color:red'>Please enter Correct Answer</span>";
}
}
?>
<div class="row">
<div class="form-group col-xs-5 col-xs-offset-3.5">
<input class="btn btn-primary btn-lg btn-block" value="Register" type="submit" name="reg">
</div>
</div>
</form>
</body>
</html>
You print the error span above the actual HTML code. Just move the if statement down. Like that:
...
<div class="row">
<?php
if ($answer != '8') {
echo "<span style='color:red'>Please enter Correct Answer</span>";
}
?>
</div>
<div class="form-group col-xs-5 col-xs-offset-3.5">
...

PHP - Form Elements Resetting

I have made a simple form with a checkbox that echos out true or false depending on if the checkbox is set or not, however every time i click submit once the checkbox control has been ticked it seems to reset.
How can i modify my code to store the value of any form controls so if i tick the box it remains ticked after i have clicked submit?
<?php
// Setup Variables
$Result = '';
if (isset($_POST["submit"])) {
//Use Captials?
if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
$Result = "true";
} else {
$Result = "false";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Contact Form</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="index.php">
Checkbox
<input type="checkbox" name="formCheckbox" value="Yes"/>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</form>
<div class="col-sm-10 col-sm-offset-2">
<?php echo $Result; ?>
</div>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Add the checked property &
Change the way the check box is displayed:
<?php
// Setup Variables
$Result = '';
if (isset($_POST["submit"])) {
//Use Captials?
if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
$Result = "true";
} else {
$Result = "false";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Contact Form</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="index.php">
Checkbox
<?php
if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
echo '<input type="checkbox" name="formCheckbox" value="Yes" checked/>';
}
else
echo '<input type="checkbox" name="formCheckbox" value="Yes"/>';
?>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</form>
<div class="col-sm-10 col-sm-offset-2">
<?php echo $Result; ?>
</div>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

PHP Session variable not carrying over

I am working with PHP session variables, but the problem is that the session variables are not carrying over. I checked using Firebug, and found that the session variables are not being created at all.
print_r($_SESSION); returns an empty array after the page returns to index.php, but displays it correctly before the refresh.
logincheck.php:
<?php
session_start();
error_reporting(-1);
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
{
$username=$_SESSION['username'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Smart Shopping Cart System</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<style type="text/css">
.show-grid{
background:#E0E0E0;
}</style>
</head>
<body>
<?php
include "connection.php"
?>
// ** Some Content**
</body>
</html>
<?php
}
else
{
?>
<html>
<head>
<title>Log In | SmartCart</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-brand">
SmartCart
</div>
</div>
</div>
<div class="container">
<br />
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<form role="form" method="post" name="login" action="test.php">
<div class="form-group">
<label for="username">Operator Username</label>
<input type="username" class="form-control" name="username" placeholder="Enter your username">
</div>
<div class="form-group">
<label for="pass">Password</label>
<input type="password" class="form-control" name="pass" placeholder="Password">
</div>
<button type="submit" class="btn btn-default" name="sub">Submit</button>
</form>
</div>
<div class="col-md-3">
</div>
</div>
</div>
</body></html>
<?php
}
?>
test.php
<?php
session_start();
error_reporting(-1);
?>
<html><head><title>Redirecting...</title>
</head>
<body>
Redirecting....
<?php
include "connection.php";
if(isset($_POST['sub']))
{
$u_name=$_POST['username'];
$password=mysql_escape_string($_POST['pass']);
$password=mysql_escape_string($password);
$w="select * from operators where operator_username = '$u_name' AND operator_pass = '$password'";
$b=mysql_query($w) or die(mysql_error()."in query $w");
$num_rows = mysql_num_rows($b);
if($num_rows > 0)
{
$_SESSION['logged_in']=true;
$_SESSION['username']=$_POST['username'];
print_r($_SESSION);
echo '<meta http-equiv="refresh" content="0; url=index.php">';
}
else
{
echo "<script>
alert('Your username or password is incorrect. Please try again'); window.location = 'index.php'; </script>";
}
}
?>
</body></html>
Please help.
Spot the difference:
logincheck:
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
^^^^^^^^^
test:
$_SESSION["loggedin"]=true;
^^^^^^^^
One of these things is not like the other...
I think the under score is missing in on test.php page
in $_SESSION['loggedin']=true;
change this to $_SESSION['logged_in']=true;
As you are using this in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
there is no error with you session part the error is may be in you db access part check your database connection. you can check you session by commenting the query part, it is working fine.

PHP, MySQL insert error

I have form for adding articles. I have 6 things (title,date,author,keywors,content,image) what I want to save to my database in localhost. But if I click on submit button in my database appear only 3 of them (title, date, image) but other nothing.
Any ideas how to solve it?
Here code for insert_post.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>APK Market</title>
<link rel="stylesheet" type="text/css" href="../style/style.css">
<link href="../bootstrap/css/bootstrap.css" rel="stylesheet" media="screen">
<script src="../bootstrap/js/bootstrap.min.js"></script>
<script src="../bootstrap/js/bootstrap.js"></script>
</head>
<body>
<?php
include("../includes/connect.php");
if(isset($_POST['submit']))
{
$post_date = date('y-m-d');
$post_title = $_POST['title'];
$post_author = $_POST['author'];
$post_keywords = $_POST['keywords'];
$post_image = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
$post_content = $_POST['content'];
if($post_title=="" or $post_author="" or $post_keywords="" or $post_content="")
{
echo"<script>alert('any field is empty')</script>";
exit();
}
else
move_uploaded_file($image_tmp,"../uploaded_images/$post_image");
$insert_query = "insert into posts (post_title,post_date,post_author,post_image,post_keywords,post_content) values ('$post_title','$post_date','$post_author','$post_image','$post_keywords','$post_content')" ;
}
?>
<form method="post" action="insert_post.php" enctype="multipart/form-data">
<div class="new_post">
<div class="headtitle">Insert new post</div>
<div class="new_title">
<p>New title</p>
<input type="text" name="title">
</div>
<div class="new_author">
<p>Author</p>
<input type="text" name="author">
</div>
<div class="new_keywords">
<p>Keywords</p>
<input type="text" name="keywords">
</div>
<div class="new_image">
<p>Image</p>
<input type="file" name="image">
</div>
<div class="new_content">
<textarea name="content" cols="20" rows="8"></textarea>
</div>
<div class="submit">
<input type="submit" name="submit" value="OK">
</div>
</div>
</form>
<script src="https://code.jquery.com/jquery.js"></script>
<script src="../bootstrap/js/bootstrap.js"></script>
</body>
</html>
<?php
if(mysql_query($insert_query))
{
echo "<center><h1>Post published seccesfuly!</h1></center>";
}
?>
if($post_title=="" or $post_author="" or $post_keywords="" or $post_content="")
main error on HERE
replace it with
if($post_title=="" or $post_author=="" or $post_keywords=="" or $post_content=="")

Categories