PHP, MySQL insert error - php

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=="")

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 hide an option in a dropdown menu PHP?

I am learning PHP, and would like to know if there is any way to hide an option depending on what I get,
basiccaly this is my code it's simple.
<?php
require 'database.php';
session_start();
if(!isset($_POST['update'])){
$id=$_GET['id'];
$state=$_GET['state'];
$sql="SELECT * from states";
$show=$conn->prepare($sql);
$show->execute();
$result = $show->fetchAll(PDO::FETCH_ASSOC);
}else{
$id=$_POST['id'];
$state=$_POST['state'];
$sql="UPDATE pqrs SET fk_state=:fk_state WHERE idPqrs=:id";
$update=$conn->prepare($sql);
$update->bindParam(':id',$_POST['id']);
$update->bindParam(':fk_state',$_POST['state']);
$update->execute();
}
?>
<!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">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Document</title>
</head>
<body>
<body>
<div class="ml-5"><?php require 'partials/header.php' ?></div>
<div class="container col-5">
<h1 class="text-center mb-5">Extreme Techonologics</h1>
<h2 class="text-center">Ingrese los campos a cambiar</h2>
<form action="pqr_update.php" method="POST">
<div class="form-group">
<input type="hidden" value="<?php echo $id?>" class="form-control" name="id">
</div>
<div class="form-group my-5">
<label for="pqr_type">Seleccione el estado del PQR:</label>
<select class="form-control" id="state" name="state">
<?php foreach($result as $state):?>
<option value="<?php echo $state['idState'];?>"><?php echo $state['state'];?></option>
<?php endforeach ?>
</select>
</div>
<button type="submit" class="btn btn-primary" name="update">Enviar</button>
</form>
</div>
Cerrar Sesión
</body>
</html>
</html>
Basically I would like to know if when $ _GET = ['state'] = 'Nuevo', when I press the button (name = 'update') what I receive from the POST, it only shows me an option (En Ejecución), in theory there are 3 states , Nuevo, En Ejecución and Cerrado. When the status is ¿Nuevo' I don't want it to show me the option "EN Ejecucuion" and when the option received is "EN Ejecucuion" I do not want it to show me the option 'Nuevo'.
Based on the above requirement, try this:
<?php
require 'database.php';
session_start();
if(!isset($_POST['update'])){
$id=$_GET['id'];
$state=$_GET['state'];
$sql="SELECT * from states";
$show=$conn->prepare($sql);
$show->execute();
$result = $show->fetchAll(PDO::FETCH_ASSOC);
}else{
$id=$_POST['id'];
$state=$_POST['state'];
$sql="UPDATE pqrs SET fk_state=:fk_state WHERE idPqrs=:id";
$update=$conn->prepare($sql);
$update->bindParam(':id',$_POST['id']);
$update->bindParam(':fk_state',$_POST['state']);
$update->execute();
}
?>
<!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">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Document</title>
</head>
<body>
<body>
<div class="ml-5"><?php require 'partials/header.php' ?></div>
<div class="container col-5">
<h1 class="text-center mb-5">Extreme Techonologics</h1>
<h2 class="text-center">Ingrese los campos a cambiar</h2>
<form action="pqr_update.php" method="POST">
<div class="form-group">
<input type="hidden" value="<?php echo $id?>" class="form-control" name="id">
</div>
<div class="form-group my-5">
<label for="pqr_type">Seleccione el estado del PQR:</label>
<select class="form-control" id="state" name="state">
<?php
foreach($result as $state):
if (
($_REQUEST['state'] == '¿Nuevo\'' && $state['state'] != 'EN Ejecucuion')
&&
($_REQUEST['state'] == 'EN Ejecucuion' && $state['state'] != '¿Nuevo\'')
):
?>
<option value="<?php echo $state['idState'];?>"><?php echo $state['state'];?></option>
<?php endif; endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary" name="update">Enviar</button>
</form>
</div>
Cerrar Sesión
</body>
</html>
</html>

Image/File upload doesn't work with materializecss framework

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.

How Do I Echo All The Contents Of An Html File?

I am trying to echo a whole html code. I am using foundation 5 and if I try to make a form variable inside the php code block, there is an error stating that the classes used in foundation 5 are not functionable. As you can see this is not a finished code so I am just looking for answers right now.
Here is my code:
<?php
session_start();
?>
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>AskmanProducts</title>
<link rel="stylesheet" href="css/foundation.css" />
<script src="js/vendor/modernizr.js"></script>
<script src="js/signinvaldator.js"></script>
</head>
<body>
<div class="row" style="margin-top:10%">
<div align="center"><h2>Log In To Access This Website</h2></div>
<br />
<div class="medium-6 medium-centered large-centered large-6 columns">
<form data-abide>
<div class="name-field">
<label for="username">Username</label>
<input id="username" type="text" required="" name="user"></input>
<small class="error" data-error-message="">A username is required.</small>
</div>
<label for="password">Password</label>
<input id="password" type="password" required="" name="password"></input>
<small class="error">A Password is required.</small>
<br />
<br />
<button type="submit" name="loginbtn">Log In</button>
Sign Up
Forgot Password?
<br />
</form>
</div>
</div>
<?php
if ($_POST['loginbtn']) {
$user = $_POST['user'];
$password = $_POST['password'];
}
else
echo
?>
<script src="js/vendor/jquery.js"></script>
<script src="js/foundation.min.js"></script>
<script>
$(document).foundation();
</script>
</body>
</html>
Rename your html file as php. Example: index.html -> index.php

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>

Categories