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
Related
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();
}
?>
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.
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In the following code I check a username and password with a database and then run an if statement to either allow the user to view the page or just show them a "Please login" message. At this point it is only returning the false values even if the passwords are equal.
I know I can pick up code for this on the internet, but I want to figure out where my logic (only in my head) is going wrong. I also know I haven't sanitized the data, but this is just a tutorial for grade 10 students just learning HTML and CSS with a little bit of PHP. I will get to that protecting data later - one concept at a time.
Thanks for the help!
<?php
// connects to server
include('includes/connect2.php');
// sets post to variables don't know if this is needed
$user=$_POST[username];
$pass=$_POST[password];
?>
<?php
// query to find ifnoramtion in database
$result = mysqli_query($con, "SELECT * FROM social_register WHERE Login='$user'");
mysqli_real_escape_string($con, $user) . "'" );
$row = mysqli_fetch_assoc($result);
if ($pass == $row['Password']) { ?>
<div id="container">
<div id="banner">
The social Site
<div id="site_logo">
<img src="images/logo_thumb.gif" />
<?php
while($row = mysqli_fetch_array($result))
{
echo $row['First_Name'] . " " . $row['Last_Name'];
echo "<br>";
}
mysqli_close($con);
?>
</div>
</div>
<div id="person">
<h1>Welcome to your test web site</h1>
</div>
</div>
<?php
}
else
{
?>
<div id="container">
<div id="banner">
The social Site
<div id="site_logo">
<img src="images/logo_thumb.gif" />
</div>
</div>
<div id="person">
<h1>You have not loged into the site, please login.</h1>
</div>
<?php
}
?>
You should fetch data with mysqli_fetch_assoc before match password
$user = mysqli_real_escape_string($con, $user);
$result = mysqli_query($con, "SELECT * FROM social_register WHERE Login = '".$user."'");
$row = mysqli_fetch_assoc($result);
if ($pass == $row['Password']) { ?>
Well, I have put you an Login.php, and a Check.php, I hope that you enjoy it... :P
Try that:
//Login.php
session_start(); //create the session
if(isset($_POST['login']))
{
$con = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($con));
$user = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM social_register WHERE Login='" . mysqli_real_escape_string($con, $user) . "'" ));
$pass = $user['Password']; //put the pass variable
if ( $pass == $user['Password'] ) {
$_SESSION['ready'] = true;
header('Location: you_page.php'); //redirect to your page when you have the correct pass
exit;
} else
{
?>
<script type="text/javascript">
<!--
alert('Incorrect Password')
//-->
</script>
<?php
}
}
//continue with the next block
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> Login </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<center>
<h3 style="color:#fff;">Password:<h3>
<form method="post" action="">
<input type="password" style="text-align:center;" name="pass"><br>
<input type="submit" name="login" value="Iniciar sesión">
</form>
</center>
</body>
</html>
//Check.php
<?php
session_start();
if (!isset($_SESSION['ready'])
|| $_SESSION['ready'] !== true) {
header('Location: login.php'); //Redirect to the login if you haven't the session set
exit;
}
?>
In your_page.php, you have to put:
require 'check.php';
Logout.php
<?php
session_start();
if (isset($_SESSION['ready'])) {
unset($_SESSION['ready']);
}
header('Location: index.php');
exit;
?>
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 :)