I want to have members be able to submit items to the site. The submission process works and the items are added to the database correctly, but the session message doesn't apply.
<?php
require 'db2.php';
$email = $mysqli2->escape_string($_POST['email']);
$itemName = $mysqli2->escape_string($_POST['itemName']);
$image = $mysqli2->escape_string($_POST['image']);
$price = $mysqli2->escape_string($_POST['price']);
$wpSite = $mysqli2->escape_string($_POST['wpSite']);
$description = $mysqli2->escape_string($_POST['description']);
$banned = $mysqli2->escape_string($_POST['omitBox']);
$sql2 = "INSERT INTO itemsTable (email, itemName, image, price, wpSite, description, banned) "
. "VALUES ('$email', '$itemName', '$image', '$price', '$wpSite', '$description', '";
if( empty($_POST["omitBox"]) ) {
$sql2 .= "0";
} else {
$sql2 .= "1";
}
$sql2 .= "');";
if ( $mysqli2->query($sql2) ){
$_SESSION['message'] = 'Successfully added this item to your wishlist';
echo'<script> window.location="success.php"; </script> ';
}
else {
$_SESSION['message'] = 'Something went wrong! Please try again!';
echo'<script> window.location="error.php"; </script> ';
}
I expect the information to be inserted into the database (and it is. no problem there), but on the success page, the session message doesn't exist. The success page works on other parts of the site, but from this page, it does not.
Just add session_start() at the top of your file
<?php
session_start();
require 'db2.php';
......
?>
You're missing the session_start()
function. While creating or setting a session you're required to start the session. For the succes.php
and error.php
files it is also required to test if the session exists. So i would advice you to do the follwing two things:
Add session_start()
to your main file:
<?php // Start the session session_start // Rest of the code. ?>
Add a session_start()
+ check to your succes.php
and error.php
files. There are many ways to test this so i'll show you the way using isset
:
Use isset function
function ifsessionExists(){
// check if session exists?
if(isset($_SESSION)){
return true;
}else{
return false;
}
}
You can also use empty function
function ifsessionExists(){
// check if session exists?
if(!empty($_SESSION)){
return true;
}else{
return false;
}
}
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.