delete.php page not deleting DB content - php

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 :)

Related

Why is PHP Session for my E-commerce website is not working?

I'm building an ecommerce website project and right at the start, I kept on having the same problem. For some reason that I don't know, it feels like session_star() is not working or not displaying. I already done so many approach the last thing I have done is copy a source code online made by packetcode on youtube. but no results is showing in my browsers
I was expecting that the results will show but even though I referenced alot of sourece code it's still doesn't work and I have no any idea.
heres the index.php file:
<?php
session_start();
include "db.php";
include "retrieve.php";
include "function.php";
include "logic.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exquisite</title>
</head>
<body>
<div class="container" id="main_cntr">
<div id="intro_cntr">
<div id="title_cntr">
<h2>Welcome to </h1>
<h1>Exquisite</h1>
</div>
<div id="paragraph_cntr">
<p>Here to provide an excellent support for your style!</p>
</div>
</div>
<?php if(empty($_SESSION['username'])){?>
<div class="container" id="form">
<div id="login_cntr">
<form method="POST">
<h2>Login</h2>
<label for="username">Username</label><br>
<input type="text" name="username" placeholder="Enter your Username"><br>
<label for="password">Password</label><br>
<input type="password" name="pass" placeholder="Enter your Password"><br>
<input type="submit" name="login" value="Login">
</form>
</div>
<?php }?>
<div id="signupOption_cntr">
Create an Account
<h4>or</h4>
Login as Admin
</div>
</div>
<?php if(!empty($_SESSION['username'])){?>
<div class="container">
<h1>Hello again<?php echo $_SESSION['username'];?></h1>
<form method="POST">
<button name="logout">Logout</button>
</form>
</div>
<?php }?>
</div>
</body>
</html>
I also devided the codes as seen in packetcode's video.
here the database code:
<?php
$conn = mysqli_connect('localhost', 'root', '', 'exquisite') or die ("Cannot connect to the Database");
?>
heres the account retrieval code:
<?php
if(isset($_REQUEST['login'])){
$uname = $_REQUEST['username'];
$pword = $_REQUEST['pass'];
}
?>
here's the function to take data from the server:
<?php
function login($conn, $uname, $pword){
$sql = "SELECT * FROM `user_acc` WHERE `username` = '$uname'";
$query = mysqli_query($conn, $sql);
return $query;
}
?>
and here's the code for validation:
<?php
if(isset($_REQUEST['login'])){
$result = login($conn, $uname, $pword);
foreach($result as $r){
$passw_check = password_verify($pword, $r['password']);
if($passw_check){
$_SESSION['username'] = $r['username'];
header("location: home.php");
}
}
}
if(isset($_REQUEST['logout'])){
session_destroy();
header("location: index.php");
exit();
}
?>
Need more information.
if you are using separate file to validation make sure you are include sessio_start(); on that file too.
without session_start(); session_destroy(); will not work.
<?php
session_start();
if(isset($_REQUEST['login'])){
$result = login($conn, $uname, $pword);
foreach($result as $r){
$passw_check = password_verify($pword, $r['password']);
if($passw_check){
$_SESSION['username'] = $r['username'];
header("location: home.php");
}
}
}
if(isset($_REQUEST['logout'])){
session_destroy();
header("location: index.php");
exit();
}
?>

PHP/MYSQL image delete button

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

Why doesn't my delete function work? [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 4 years ago.
I followed a tutorial on Youtube about how to make a CMS system:
http://y2u.be/QNxU3Qa6QZs
And i'm stuck at making the delete function, it just refreshes the page without actually deleting something. Can someone help me with this problem?
This is the code i have for the delete page:
<?php
session_start();
include_once('../includes/connection.php');
include_once('../includes/article.php');
$article = new Article;
if (isset($_SESSION['logged_in'])) {
if(isset($_GET['id'])) {
$id = $GET['id'];
$query = $pdo->prepare('DELETE FROM articles WHERE article_id = ?');
$query->bindValue(1, $id);
$query->execute();
header('Location: delete.php');
}
$articles = $article->fetch_all();
?>
<html>
<head>
<title> Blog </title>
<link rel="stylesheet" href="../assets/style.css" />
</head>
<body>
<div class="container">
CMS
<br><br>
<h4>Select an Article to delete </h4>
<form action="delete.php" method="get">
<select onchange="this.form.submit();" name="id">
<?php foreach ($articles as $article) { ?>
<option value="<?php echo $article['article_id']; ?>">
<?php echo $article['article_title']; ?>
</option>
<?php } ?>
</select>
</form>
</div>
</body>
</html>
<?php
} else {
header('Location: index.php');
}
?>
If you have any questions about the code, i'll answer them.
I hope you'll help me find a solution for this problem.
Daniel
You have a typo.
Change
$id = $GET['id'];
To
$id = $_GET['id']; #$_GET is the global array not $GET

How to code a check answer function for every page after submission PHP

I am writing a quizzer for a particular project and I already have all the basic functions being set up.
Basically what it does right now is:
Start quiz
Attempt multiple choice quiz
Go to results page to see your score
Usually this is the usual way of creating a quizzer. However, I would like to allow the answerers to know whether their answer is correct or not on that particular question page itself after he has submitted an answer on that page directly.
But i have no idea how to do it.
I tried searching online for tutorials but there are none I can find that allows the answerer to check the answers on the spot at the page of the question they are answering.
I also tried comparing their selected choice with the correct answer using PHP variables but the computer feedbacks and say that the variable is undefined even though it is defined properly.
Then i shifted the entire process.php file into the question.php file to make sure that the variables on defined in question.php. Yet the computer still feedback to me that the variables $correct_choice and $selected_choice are not defined.
Below is the code for question.php where the questions are being set FYI:
<?php
include 'database.php';
session_start();
$number=(int)$_GET['n'];
$query = "SELECT*FROM questions
WHERE question_number = $number";
$result = mysqli_query($con,$query);
$question = mysqli_fetch_assoc($result);
$query2 = "SELECT*FROM choices
WHERE question_number = $number";
$choices = mysqli_query($con,$query2);
$query="SELECT* FROM questions";
$results= mysqli_query($con,$query);
$total=$results->num_rows;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>BTT Quizzer</title>
<link rel="stylesheet" href="css/style.css" type="text/css"/>
</head>
<body>
<header>
<div class="container">
<h1>BTT Quizzer</h1>
</div>
</header>
<main>
<div class="container">
<div class="current">Question <?php echo $question['question_number']; ?> of <?php echo $total; ?></div>
<p class="question">
<?php echo $question['text']?>
</p>
<form method="post" action="">
<ul class="choices">
<?php while($row=mysqli_fetch_assoc($choices)): ?>
<li><input name="choice" type="radio" value="<?php echo $row['id']?>"/><?php echo $row['text']?></li>
<?php endwhile; ?>
</ul>
<input type="submit" value="submit" name="submit"/>
<input type="hidden" name="number" value="<?php echo $number; ?>"/>
</form>
<div class="feedback">
<?php if($correct_choice == $selected_choice){
echo "Thats Correct!";
}
?>
</div>
<div class="feedback2">
<?php if($correct_choice != $selected_choice){
echo "Thats Wrong!";
}
?>
</div>
</div>
</main>
<footer>
<div class="container">
Copyright ©2014,BTT Quizzer
</div>
</footer>
</body>
</html>
Below is the code for process.php FYI:
include 'database.php';
session_start();
if(!isset($_SESSION['score'])){
$_SESSION['score'] = 0;
}
if(isset($_POST['submit'])){
$number=$_POST['number'];
$selected_choice = $_POST['choice'];
$next=$number+1;
$query="SELECT*FROM questions";
$results= mysqli_query($con,$query);
$total=$results->num_rows;
$query = "SELECT*FROM`choices` WHERE question_number = $number AND is_correct=1";
$results= mysqli_query($con,$query);
$row=$results->fetch_assoc();
$correct_choice=$row['id'];
if($correct_choice == $selected_choice){
$_SESSION['score']++;
}
if($number == $total){
header("Location: final.php");
exit();
} else {
header("Location: question.php?n=".$next);
}
}
SOLVED:
Use a super global $_SESSION to get the variables from process.php and echo it into question.php with conditionals.

Making two HTML forms into one

So I got this form which saves text into a session variable with PHP. I got another form which makes all my session variables save into my database.
I'm trying to make it more efficient bij making the 2 forms into one form with one button. Here is my complete page code:
<?php
session_start();
if( $_SESSION['myusername'] == "")
{
header('Location: index.php');
}
?>
<html>
<head>
<link rel="stylesheet" href="css/main.css">
<style>body {background-image:url("stopphp.jpg");}</style>
</head>
<?php
if($_POST)
{
// $_SESSION["textarea"] = "";
$_SESSION["textarea"] = $_POST["textarea"];
header("Location:stop.php");
}
if($_GET)
{
$_SESSION["klantnummer"] = $_GET["klantnummer"];
header("Location:stop.php");
}
if($_GET)
{
$_SESSION["klantnaam"] = $_GET["klantnaam"];
header("Location:stop.php");
}
$textarea = $_SESSION["textarea"];
?>
<div id="notities">
<span style="font-family:Cursive;font-size:14px;font-style:normal;font-weight:bold;text-decoration:none;text-transform:none;color:000000;">Notities:</span>
<p>
<form action="" method="post">
<textarea cols="60" rows="10" name="textarea"></textarea><br/>
<input type="submit" value="Sla tekst op">
</form>
<p>
<form action="writeaway.php" method="post">
<input type="submit" value="Bij einde gesprek gelieve hier te drukken">
</form>
</div>
</html>
All suggestions are appreciated, sorry for the nooby question.
EDIT:
here is my writeaway code:
<?php
session_start();
if( $_SESSION['myusername'] == "")
{
header('Location: index.php');
}
?>
<html>
<?php
$date=date_create();
$_SESSION["stopdatum"] = date_format($date,"Y/m/d H:i:s");
?>
<?php include 'database.php'; ?>
<?php
mysqli_query($con,"INSERT INTO suplog (login,sstart,sstop,remark,klantnummer,klantnaam) VALUES ('".mysql_real_escape_string($_SESSION['myusername'])."',
'".mysql_real_escape_string($_SESSION['startdatum'])."',
'".mysql_real_escape_string($_SESSION['stopdatum'])."',
'".mysql_real_escape_string($_SESSION['textarea'])."',
'".mysql_real_escape_string($_SESSION['klantnummer'])."',
'".mysql_real_escape_string($_SESSION['klantnaam'])."')"); ?>
<?php
session_destroy();
?>
<?php header('Location: start.php'); ?>
</html>
I do not get your problem...
But you should try to assign your session variables with the values in the same step like you write them into a database! In your case in writeaway.php... Why would you do that in two different steps?
Hope that helps

Categories