PHP/MYSQL image delete button - php

Hello im still a beginner but i have a uni project that i have to complete.I have to make a php/mysql image gallery and i have to add a DELETE button on each image, when clicked it needs to delete the image from the page and from the database.I have looked up everywhere but im not sure how exactly to write the code in order to delete the images, i have created the button and i have linked it to delete-image.php.
Any recommendations ?
view-album.php
<?php
include 'connection.php';
if (isset($_GET['album_id'])) {
$album_id = $_GET['album_id'];
$get_album = $mysqli->query("SELECT * FROM gallery_albums WHERE album_id = $album_id");
$album_data = $get_album->fetch_assoc();
} else {
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $album_data['album_name'] ?></title>
</head>
<body>
<?php
$photo_count = $mysqli->query("SELECT * FROM gallery_photos WHERE album_id = $album_id");
?>
Home | <?php echo $album_data['album_name'] ?> (<?php echo $photo_count->num_rows; ?>)<br><br>
<form method="post" action="upload-photo.php?album_id=<?php echo $album_id ?>" enctype="multipart/form-data">
<label>Add photo to this album:</label><br>
<input type="file" name="photo" /> <input type="submit" name="upload-photo" value="Upload" />
</form>
<?php
if (isset($_GET['upload_action'])) {
if ($_GET['upload_action'] == "success") { ?>
<br><br>Photo successfully added to this album!<br><br>
<?php }
}
?>
<?php
$photos = $mysqli->query("SELECT * FROM gallery_photos WHERE album_id = $album_id");
while($photo_data = $photos->fetch_assoc()) { ?>
<img src="<?php echo $photo_data['photo_link'] ?>" width="200px" height="200px" />
<form method="POST" action="delete-image.php">
<button name='delete'>Delete file </button>
</form>
<?php }
?>
</body>
</html>
delete-image.php
<?php
include 'connection.php';
?>

(1.)First you have to create input hidden field with row id with every button Tag.
(2.)Get id from hidden field in delete_image.php by $_POST['']; POST method.
(3.)then delete the image from that id.
<?php
$photos = $mysqli->query("SELECT * FROM gallery_photos WHERE album_id = $album_id");
while($photo_data = $photos->fetch_assoc()) {
$album_id = $photo_data['album_id'];
?>
<img src="<?php echo $photo_data['photo_link']; ?>" width="200px" height="200px" />
<form method="POST" action="delete-image.php">
<input type="hidden" name="delete" value="$album_id"> // hidden input field with id.
<button name='delete'>Delete file </button>
</form>
<?php }
?>
delete-image.php
<?php
include 'connection.php';
$idget = $_POST['delete']; // get here hidden id by POST method.
$photosDelete = $mysqli->query("DELETE FROM gallery_photos WHERE album_id = $idget");
?>
Note:- For More information regarding hidden field see this:-
https://www.w3schools.com/tags/att_input_type_hidden.asp

Related

Delete article is not working when require it

I have problem with article deleting. I have got 2 files(article.php, delete.php). When I require file delete.php in article.php it is not deleting the article, but when i open it itself.. just delete.php, It works. I don't know why its happening. Can someone please help me? thanks. Files below.
This is article.php:
<?php
session_start();
include_once('../connect.php');
include_once('admin.php');
include_once('../includes/article.php');
if(isset($_SESSION["user_id"])){
$query=$pdo->prepare("SELECT * FROM users WHERE id =
".$_SESSION["user_id"]);
$query->execute();
$row=$query->fetch(PDO::FETCH_ASSOC);
if($row['privileges']==1){
?>
<html>
<head>
<title>CMS Tutorial</title>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<br/>
<div class="options">
<img src="../images/add.png" width="20px" height="20px" style="float:left;"><form method="post"><input class="article" type="submit" name="add" value="Pridať článok">
</form>
<img src="../images/delete.png" width="20px" height="20px" style="float:left;"><form method="post"><input class="article" type="submit" name="delete" value="Odstrániť článok"></form>
<img src="../images/edit.png" width="20px" height="20px" style="float:left;"><form method="post"><input class="article" type="submit" name="edit" value="Upraviť článok"></form>
</div>
<?php
if(isset($_POST['add'])){
require_once('add.php');
}else{
if(isset($_POST['delete'])){
require_once('delete.php');
}else{
if(isset($_POST['edit'])){
require_once('edit.php');
}}}?>
</body>
</html>
<?php
} else{
header('Location: ../index.php');
}
} else{
header('Location: ../index.php');
}
?>
This is delete.php:
<?php
session_start();
include_once('../connect.php');
include_once('../includes/article.php');
$article= new Article;
if(isset($_SESSION['user_id'])){
if(isset($_POST['id'])){
$id=$_POST['id'];
$query=$pdo->prepare('DELETE FROM articles WHERE article_id=?');
$query->bindValue(1,$id);
$query->execute();
header('Location: ../index.php');
}
$articles=$article->fetch_all();
?>
<html>
<head>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<div class="container">
<br/>
<h4>Zvoľte článok, ktorý chcete odstrániť:</h4>
<form action="" method="post" class="addarticle">
<select name="id">
<?php foreach($articles as $article){?>
<option value="<?php echo $article['article_id']; ?>"><?php echo
$article['article_title']; ?></option>
<?php } ?>
</select>
<input type="submit" value="Odstrániť článok">
</form>
</div>
</body>
</html>
<?php
} else{
header('Location: ../index.php');
}
?>
You should make sure you are calling session_start exactly once:
if(session_id() == '') {
session_start();
}
and now to your problem. When you click on the delete button, your form is submitted and the only data sent is delete=Odstrániť článok, no id being sent there. You need to change your form like this:
<img src="../images/delete.png" width="20px" height="20px" style="float:left;"><form method="post"><input class="article" type="submit" name="delete" value="Odstrániť článok"><input type="hidden" name="id" value="<?php /*echo id of the article to be removed*/ ?>"></form>
If you do so, upon clicking on the delete button, the id will be sent to the server as well. Your delete is executed now without a problem, but the id you provide to it is faulty.

UPDATE query is not working php

Going to try to keep it short. I have a while loop in grid.php file to fill up a table as such...
<?php while($product = $products->fetch_assoc()) { ?>
<tr>
<td><?php echo $product['cd_id']?></td>
<td><?php echo $product['cd_title']?></td>
<td><?php echo $product['cd_musician_fname']?></td>
<td><?php echo $product['cd_musician_lname']?></td>
<td><?php echo $product['cd_price']?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php } ?>
If I click the first anchor tag takes me to a edit.php file and here is the head code for that file.
<?php include '_includes/db.php';
$cd_id = trim($_GET['id']);
$message = '';
include '_includes/connection.php';
if($db->connect_error){
$message = $db->connect_error;
}else{
$sql = "SELECT * FROM CD WHERE cd_id = $cd_id";
$result = $db->query($sql);
$row = $result->fetch_assoc();
if($db->error){
$message = $db->error;
}
}
?>
Now I will show the html of edit.php
<!-- Product Musician last name-->
<fieldset class="form-group">
<label for="cd_musician_lname">Musician's lirst name</label>
<input type="text" class="form-control" id="cd_musician_lname" name="cd_musician_lname" value="<?php echo $row['cd_musician_lname'];?>">
</fieldset>
<!-- End of Musician last name-->
<!-- Product price-->
<fieldset class="form-group">
<label for="cd_price">Product price</label>
<input type="text" class="form-control" id="cd_price" name="cd_price" value="<?php echo $row['cd_price'];?>">
</fieldset>
<!-- End of Product price-->
<!-- Form submit button-->
Update Record
<a class="btn btn-primary" href="index.php" role="button">Go Back Home</a>
I have the edit.php page working just fine but if I make changes in the fields and click the submit anchor tag I get all the fields of the row empty but the PK. Here is the code for the final edit_confirm.php file
<?php
include '_includes/db.php';
$cd_id = trim($_GET['id']);
$cd_title = $_POST['cd_title'];
$cd_musician_fname = $_POST['cd_musician_fname'];
$cd_musician_lname = $_POST['cd_musician_lname'];
$cd_price = $_POST['cd_price'];
$message = '';
include '_includes/connection.php';
if($db->connect_error){
die("Connection failed: ".$db->connect_error);
} else {
$sql = "UPDATE CD SET cd_title='".$cd_title."', cd_musician_fname='".
$cd_musician_fname."', cd_musician_lname='".
$cd_musician_lname."', cd_price='".$cd_price."' WHERE cd_id = $cd_id ";
$db->query($sql);
var_dump($sql);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include '_includes/main-head.php';?>
</head>
<body>
<?php include '_includes/main-navbar.php';?>
<div class="container">
<hr>
<?php
if($db->query($sql) === TRUE){ ?>
<h1>Record updated successfully.</h1>
<?php echo $cd_title; ?>
<?php echo $record->affected_rows ?>
<p> record was updated in the database.</p></br>
<?php } else { ?>
<p>Error updating the record: </p> <?php $db->error; ?>
<?php }; ?>
<hr>
<a class="btn btn-primary" href="index.php" role="button">Go Back Home</a>
</div>
<?php include '_includes/main-script.php';?>
</body>
</html>
If you notice in the edit_confirm.php I did a var_dump to see what are the values in the variables and it shows empty.
I need help with this.
Thank you in advance.
Man the better way to do this is make it simple to test if the record is updating or not
formsample.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
include("connection.php");
$id = $_GET['id'];
$query= "select * from clients where id = '$id'";
$sql = mysqli_query($connect, $query);
$result = mysqli_fetch_assoc($sql);
?>
<form action="process.php" method="post">
<input type="text" name="name" id="name" value="<?php echo $result['name'] ?>" />
<input type="text" name="email" id="email" value="<?php echo $result['email'] ?>" />
<input type="hidden" name="id" id="id" value="<?php echo $id?>" />
<input type="submit" />
</form>
</body>
</html>
process.php
<?php
include("connection.php");
$id = $_POST['id'];
$name = $_POST['name'];
$email= $_POST['email'];
$query = "UPDATE clients set name= '$name', email ='$email' where id = '$id'";
$sql = mysqli_query($connect, $query);
?>
Update Record
This is not the proper way to submit a form - it won't work at all.
You need to have a form opening and closing tag, the target address is in the action attribute of the form element, and the method is on there too and should be post for this form (method="POST"). In your code you have a link where you should have a submit input so it won't submit the data, it will just redirect you to that URL. You should have something like this:
<input type="submit" value="Update Record" />
http://www.w3schools.com/html/html_forms.asp

Having problems with sessions and pages in php

I really don't understand what I am doing here. I have this page profesor.php in which I want to insert some data into the database. After I submit the data from the form I want to be redirected to another page insert.php and display a message.
So I have profesor.php:
<?php
session_start();
if (isset($_SESSION['id'])) {
$fullname = $_SESSION['name'];
echo "<h1> Welcome " . $fullname . "</h1>";
} else {
$result = "You are not logged in yet";
}
if (isset($_POST['studname'])) {
include_once("dbConnect.php");
$studname = strip_tags($_POST['studname']);
$course = strip_tags($_POST['course']);
$grade = strip_tags($_POST['grade']);
$getStudidStm = "SELECT userid FROM users WHERE name = '$studname'";
$getStudidQuery = mysqli_query($dbCon, $getStudidStm);
$row = mysqli_fetch_row($getStudidQuery);
$studid = $row[0];
$_SESSION['studid'] = $studid;
$_SESSION['course'] = $course;
$_SESSION['grade'] = $grade;
header("Location: insert.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $fullname ;?></title>
</head>
<body>
<div id="wrapper">
<h2>Insert new grade</h2>
<form id="insertForm" action="insert.php" method="post" enctype="multipart/form-data">
Student: <input type="text" name="studname" /> <br />
Course : <input type="text" name="course" /> <br />
Grade : <input type="text" name="grade" /> <br />
<input type="submit" value="Insert" name="Submit" />
</form></div>
</form>
</body>
</html>
and insert.php
<?php
session_start();
if (isset($_SESSION['studid'])) {
include_once("dbConnect.php");
$studid = $_SESSION['studid'];
$course = $_SESSION['course'];
$grade = $_SESSION['grade'];
echo $studid;
echo $course;
echo $grade;
}
My problem is that insert.php doesn't display anything. I really don't understand what I'm doing wrong. Need some help.
your problem is in your form:
<form id="insertForm" action="insert.php" [...]
you send data to insert.php but all the 'magic' with
$_SESSION['studid'] = $studid;
$_SESSION['course'] = $course;
$_SESSION['grade'] = $grade;
you keep in profesor.php
Just change action="insert.php" to action="profesor.php" and it should work fine.

SQL update query not cooperating

I am adding a save/update button to the bottom of my editing form on my admin panel. For some reason, whenever I make a change to the form and click save it just reloads the page with no changes made. I also noticed that ONLY when I try to run the code from the pages.php file(runnning from index then clicking pages is fine) it says:
Undefined variable: dbc in
C:\Users\Jake\Desktop\Xampp\htdocs\jakefordsite\admin\content\pages.php
on line 12
Warning: mysqli_query() expects parameter 1 to be mysqli, null given
in
C:\Users\Jake\Desktop\Xampp\htdocs\jakefordsite\admin\content\pages.php
on line 12
I can get rid of this error by declaring a new $dbc(databaseconnection) variable in pages.php, but I still have the same problem updating my form data.
PAGES.PHP:
<?php ## Page Manager ?>
<h2>Page Manager</h2>
<div class="col sidebar">
<ul>
<?php
$q = "SELECT * FROM pages ORDER BY name ASC";
$r = mysqli_query($dbc, $q);
if ($r)
{
while ($link = mysqli_fetch_assoc($r))
{
echo '<li>'.$link['name'].'</li>';
}
}
?>
</ul>
</div>
<div class="col editor">
<?php
if (isset($_POST['submitted']) == 1) {
$q = "UPDATE pages SET title='$_POST[title]', name='$_POST[name]', body='$_POST[body]', WHERE id = '$_POST[id]'";
$r = mysqli_query($dbc, $q);
}
if (isset($_GET['id'])) {
$q = "SELECT * FROM pages WHERE id = '$_GET[id]' LIMIT 1";
;
$r = mysqli_query($dbc, $q);
$opened = mysqli_fetch_assoc($r);
?>
<form action="?page=pages&id=<?php echo $opened['id'] ?>" method="post">
<p><label>Page title: </label><input type="text" size="30" name="title" value="<?php echo $opened['title']?>"></p>
<p><label>Page name:</label> <input type="text" size="30" name="name" value="<?php echo $opened['name']?>"></p>
<label>Page body: </label><br>
<textarea name="body" cols="30" rows="8"><?php echo $opened['body'] ?></textarea>
<input type="hidden" name="submitted" value="1"/>
<input type="hidden" name="id" value="<?php echo $opened['id'] ?>"/>
<p><input type="submit" name="submit" value="Save Changes"/></p>
</form>
<?php } ?>
</div>
INDEX.PHP:
<?php
// Setup document:
include('config/setup.php');
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title><?php //echo $page_title; ?>JakeForDesign - Admin Panel</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="wrap_overall">
<div class="header"> <?php head(); ?> </div>
<div class="nav_main"> <?php nav_main(); ?> </div>
<div class="content"> <?php include('content/'.$pg.'.php'); ?> </div>
<div class="footer"> <?php footer(); ?> </div>
</div>
</body>
</html>
SETUP.PHP
<?php
## Setup Document
// host(or location of the database), username, //password, database name
$dbc = #mysqli_connect('127.0.0.1', 'root', 'password', 'main') OR die ('Could not connect to the database because: '. mysqli_connect_error() );
include('Functions/sandbox.php');
include('Functions/template.php');
if (isset($_GET['page']) == '')
{
$pg = 'home';
}
else
{
$pg = $_GET['page'];
}
$page_title = get_page_title($dbc, $pg);
?>
on Pages.php you have
$r = mysqli_query($dbc, $q);
$q is fine but you have not mentioned $dbc
on your setup page, create a class for connection, declareing a connection method and then, on PAGES.PHP:
$db_obj = new setup(); /* create object for setup class */
$dbc = $db_obj -> connect_db();/* call connection method */

delete.php page not deleting DB content

I have been working on the admin side of my website now for a while.
I have successfully created an add page to add items to my mobi DB in mysql.
But when adding a delete.php page it does not work quite so well.
The page loads and lists all my articles from the DB in a drop down menu, but when I select the one I no longer require, it goes back to index as coded on the page but check my site it does not delete the article.
my DELETE.PHP page is this:
<?php
session_start();
include_once('../include/connection.php');
include_once('../include/article.php');
$article = new storearticle;
if (isset($_SESSION['logged_in'])) {
if (isset($_GET['title'])) {
$id = $_GET['title'];
$query = $pdo->prepare('DELETE FROM mobi WHERE promo_title = ?');
$query->bindValue(1, $title);
$query->execute();
header('Location: index.php');
}
$articles = $article->fetch_all();
?>
<html>
<head>
<title>Delete Article</title>
<link rel="stylesheet" href="../other.css" />
</head>
<body>
<div class="container">
<b>← Back</b>
<br />
<div align="center">
<h4>Select an article to delete:</h4>
<form action="delete.php" method="get">
<select onchange="this.form.submit();" name="title">
<?php foreach ($articles as $article){ ?>
<option value="<?php echo $article['promo_title']; ?>"><?php echo $article['promo_title']; ?></option>
<?php } ?>
</select>
</form>
</div>
</div>
</body>
</html>
<?php
} else {
header('Location: index.php');
}
?>
Can someone see where I am going wrong?
If you need any more info then please let me know.
You're assigning id by
$id = $_GET['title'];
and then you're trying to get it by $title. I think that is the problem :)

Categories