How to call a set session without displaying blank - php

Im filling a selection box with a session, im then attempting to call this selection box to fill another with the same result. However its returning blank.
i know the session is setting because i can see it in the desired selection box, however i cant call it again and pass it to the other without it returning blank.
//my html where im including the external php.
<form class="seed-form" method="POST" action="action/matchup1-action.php">
<label name="round2-team1" class="round2-teams">
<?php include_once "action/winner-script5.php";?>
</label>
//the matchup 1 script that is called.
if (isset($_POST['go5'])) { // MATCHUP 5 SECOND ROUND MATCHUP 1
include_once 'dbcon.php';
$_SESSION['t_team9'] = $_POST['team-9'];
$_SESSION['t_team10'] = $_POST['team-10'];
$_SESSION['s_score9'] = $_POST['score-9'];
$_SESSION['s_score10'] = $_POST['score-10'];
header("Location: ../tables.php?tables=winner5");
}
//the script that is calling the session from its current selection box
<?php
include_once "dbcon.php";
//PERFORMING CHECK IF ALL REQUIRED SESSION VARIABLES ARE SET
if (isset($_SESSION['s_score9'], $_SESSION['s_score10'], $_SESSION['t_team9'], $_SESSION['t_team10'])) {
$team1 = $_SESSION['t_team9'];
$team2 = $_SESSION['t_team10'];
$score1 = $_SESSION['s_score9'];
$score2 = $_SESSION['s_score10'];
if ($score1 == $score2) {
header("Location: tables.php?table=draw"); //CHECKS IF MATCH IS A DRAW, CANNOT BE A DRAW
exit();
}else {
if ($score1 > $score2){
echo $_SESSION['t_team9'];
}else {
echo $_SESSION['t_team10'];
}
}
}else {
echo "-";
}
?>
// where i want the session variable to output
<label name="round2-team1" class="round2-teams">
<?php include_once "action/winner-script5.php";?>
</label>

Related

PHP submitting a form is destroying my sessions

Hello I am trying to make a form in the page where I created a session so when I submit the form the sessions gets destroyed here is my code:
<form method="post">
<h5>Name</h5>
<input type=text name="name" class="insertname">
<br>
<br>
<input type="submit" name="namesearch" class="insertbutton"></input>
</form>';
PHP code to start the session :
<?php
if(isset($_POST['s']))
{
$a=$_POST['uid']; //accessing value from the text field
$enteredpass = $_POST['pwd']; //accessing value from the text field
$client = new MongoDB\Client('xxxx');
$companydb = $client->test;
$jsons = $companydb->jsons;
$ownerid = $jsons->findOne(
['ID' => $a]
);
$idcoded = $ownerid->ID;
if (empty($idcoded)) {
echo "<h1>empty</h1>";
}
if (!empty($idcoded)) {
$passcoded = $ownerid->pass;
if ($passcoded == $enteredpass) {
session_start();
}
}
}
?>
Make sure you have session_start(); at the top of all the PHP scripts
So, put the session_start(); at the top of the script and set say $_SESSION["uid"] when the user has correctly entered the credentials
A normal practice is to set $_SESSION["uid"]=""; (set initial value) before the user logs in the system, and then use this session variable to determine whether the user has successfully logged in.
So
<?php
session_start();
if(isset($_POST['s']))
{
$a=$_POST['uid']; //accessing value from the text field
$enteredpass = $_POST['pwd']; //accessing value from the text field
$client = new MongoDB\Client('xxxx');
$companydb = $client->test;
$jsons = $companydb->jsons;
$ownerid = $jsons->findOne(
['ID' => $a]
);
$idcoded = $ownerid->ID;
if (empty($idcoded)) {
echo "<h1>empty</h1>";
}
if (!empty($idcoded)) {
$passcoded = $ownerid->pass;
if ($passcoded == $enteredpass) {
$_SESSION["uid"]=$_POST['uid'];
} else {
$_SESSION["uid"]="";
}
}
}
?>

Prevent new rows from being inserted when max records reached?

I have an SQL table where I want the max numbers of records to be 8.
I have a form to capture input via PHP and I thought I had an error handler that operates by counting the number of records in the table and, through an IF statement, punts the user back with an error if there are >= 8 records:
<?php
// Connect to database
include_once 'db_connect.php';
// Check for form action and submitted fields
if (isset($_POST["submit"])) {
$artist = $_POST["artist"];
$song = $_POST["song"];
$link = $_POST["link"];
// Link to external error handling functions
require_once 'functions.php';
// Check for empty fields
if (emptyFields($artist, $song, $link) !== false) {
header("location: index.php?error=empty");
exit();
}
// Check if record already exists
if (songExists($conn, $link, $song) !== false) {
header("location: index.php?error=duplicate");
exit();
}
// Check if link is valid
if (invalidLink($link) !== false) {
header("location: index.php?error=invalidlink");
exit();
}
// Check if table records are greater than or equal to 8
$sql = "SELECT COUNT(*) AS Num FROM songs";
$result = mysqli_query($conn, $sql);
if ($dbName['Num'] >= 8) {
header("location: index.php?error=maxreached");
exit();
}
// If no errors from above, add to table
addSong($conn, $artist, $song, $link);
}
else {
header("location: index.php?error=none");
exit();
}
<?php
include_once 'db_connect.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Song Submission</title>
</head>
<body>
<form action="sb_process2.php" method="POST" autocomplete="off">
<input type="text" name="artist">
<br>
<input type="text" name="song">
<br>
<input type="text" name="link">
<br>
<button type="submit" name="submit">Submit Song</button>
</form>
<?php
if (isset($_GET["error"])) {
if ($_GET["error"] == "empty") {
echo "<p>Please enter all fields.</p>";
}
else if ($_GET["error"] == "duplicate") {
echo "<p>This song has already been submitted.</p>";
}
else if ($_GET["error"] == "invalidlink") {
echo "<p>Please enter a valid URL for the Spotify link.</p>";
}
else if ($_GET["error"] == "none") {
echo "<p>Song added!</p>";
}
}
?>
I tested for my first 3 error cases and all are operating as planned (they route back with an error "code"). However, if I try to submit an entry without any errors, I just get a blank page. I ran the code through a simple PHP syntax checker and it didn't come up with any errors.

how to pass a label to POST

Im currently filling a label with a session variable, however i need to use the label data later (send it to another label), or create a new session to do so. However, when i attempt to access the first label, or the session that ive set its tells me its not set. So im assuming the data in the first label is showing but its not actually there?
//this is how im creating the first label
<?php
include_once "dbcon.php";
//PERFORMING CHECK IF ALL REQUIRED SESSION VARIABLES ARE SET
if (isset($_SESSION['s_score1'], $_SESSION['s_score2'], $_SESSION['t_team1'], $_SESSION['t_team2'])) {
$team1 = $_SESSION['t_team1'];
$team2 = $_SESSION['t_team2'];
$score1 = $_SESSION['s_score1'];
$score2 = $_SESSION['s_score2'];
if ($score1 == $score2) {
header("Location: tables.php?table=draw"); //CHECKS IF MATCH IS A DRAW, CANNOT BE A DRAW
exit();
}else {
if ($score1 > $score2){
echo $_SESSION['t_team1'];
}else {
echo $_SESSION['t_team2'];
}
}
}else {
echo "-";
}
?>
//this is how im printing it
<label name="round2-team1" class="round2-teams">
<?php include_once "action/winner-script.php";?>
</label>
the issue appears when i try to use this label above, creating a session variable with it, or moving it to a new label is impossible, its like it doesnt exist, even though i can see it on the page???
<?php
include_once "dbcon.php";
//PERFORMING CHECK IF ALL REQUIRED SESSION VARIABLES ARE SET
if (isset($_SESSION['s_score9'], $_SESSION['s_score10'], $_SESSION['t_team9'], $_SESSION['t_team10'])) {
$team1 = $_SESSION['t_team9'];
$team2 = $_SESSION['t_team10'];
$score1 = $_SESSION['s_score9'];
$score2 = $_SESSION['s_score10'];
if ($score1 == $score2) {
header("Location: tables.php?table=draw"); //CHECKS IF MATCH IS A DRAW, CANNOT BE A DRAW
exit();
}else {
if ($score1 > $score2){
echo $_SESSION['t_team9'];
}else {
echo $_SESSION['t_team10'];
}
}
}else {
echo "-";
}
?>
This is the script, here i am taking my two labels that are displaying on the page, im determining the winner, and then im displaying the session variable which won the game in a different label on the same page. however, when i try using
<?php
if (isset($_SESSION['t_team9'])) {
echo $_SESSION['t_team9'];
}else {
echo "somethings wrong";
}
?>
anywhere on the page where my labels lie, i just get the error something went wrong
ATTEMPTED FIX:
<?php
include_once "dbcon.php";
//PERFORMING CHECK IF ALL REQUIRED SESSION VARIABLES ARE SET
if (isset($_SESSION['s_score13'], $_SESSION['s_score14'], $_SESSION['result'], $_SESSION['result2'])) {
$team1 = $_SESSION['result'];
$team2 = $_SESSION['result2'];
$score1 = $_SESSION['s_score13'];
$score2 = $_SESSION['s_score14'];
if ($score1 == $score2) {
header("Location: tables.php?table=draw"); //CHECKS IF MATCH IS A DRAW, CANNOT BE A DRAW
exit();
}else {
if ($score1 > $score2){
$team1 = $_SESSION['result'];
$_SESSION['result5'] = $team1;
}else {
$team2 = $_SESSION['result2'];
$_SESSION['result5'] = $team2;
}
}
}else {
echo "-";
}
?>

Session variable cannot be passed into query

page1.php has this code
<?php
session_start();
$_SESSION['supp_id'] = 4;
?>
page2.php has this code
<?php
session_start();
include "db2.php";
$supp_id = $_SESSION['supp_id'];
var_dump($supp_id);
if(isset($_POST['accept']))
{
$cust_id = $_POST['cust_id'];
$cartype = $_POST['cartype'];
$q=mysqli_query($conn,"INSERT INTO `availablesupp` (`Cust_ID`,`Supp_ID`,`CarType`) VALUES ('$cust_id','$supp_id','$cartype')");
if($q)
echo "ok";
else
echo "error";
}
?>
$supp_id did not get passed into the query, and the database is updated with Supp_ID = 0;
However changing $supp_id = $_SESSION['supp_id'] to $supp_id = 4 works fine. var_dump produced identical results in both cases, int(4).
How do I solve this?

404 error when submitting form with empty field

I have a php page with several html forms. When a submit button is pressed while a text field is empty, I want an error to appear when the page refreshes, but instead I'm getting a 404 error. What's weird is I don't get a 404 if the fields are not empty.
This is the code for /books/index.php and the 404 error states The requested URL /books/index.php was not found on this server.
<?php
//Books
//Variables on refresh:
// $_POST['pgNum'] //Page number
// $_POST['id'] //If page is being updated. ID of the book being updated
// $_POST['newBook'] //If new book is added. Name of the new book
// $_GET['empty'] //If the form to add a new book was submitted with 1 or more empty fields.
//Equals 1 if book name is empty, 2 if page number is empty, and 3 if both are empty.
// $_GET['emptyPgNum'] //If an update button was pressed with no page number in the corresponding field,
//this variable will exist on the redirect
require_once 'connection.php'; //Function to connect to MySQL database
require_once 'queryFunctions.php'; //Functions for sending MySQL queries
if(isset($_GET['emptyPgNum'])) {
//Reload from submission with an empty page number field
echo '<font color="red">Missing page number</font><br>';
}
//Check if this page loaded from a successful form submission
if(isset($_POST['id'])) { //If page update was submitted
//Check for empty form field
if(empty($_POST['pgNum'])) {
//Empty page number field. Redirect with error
unset($_POST);
header('Location: index.php?emptyPgNum=3');
}
//Update the pageNumber field and redirect back to index.php
$conn = getConnection();
$update = "UPDATE Books SET pageNumber={$_POST['pgNum']}
WHERE id={$_POST['id']}";
booleanQuery($conn, $update);
mysqli_close($conn);
header('Location: index.php');
} else if(isset($_POST['newBook'])) { //If new book is being added
//Check if any form fields were submitted empty
if(empty($_POST['newBook']) && empty($_POST['pgNum'])) {
//Both fields empty
unset($_POST);
header('Location: index.php?empty=3');
} else if(empty($_POST['newBook'])) {
//Empty book name
unset($_POST);
header('Location: index.php?empty=1');
} else if(empty($_POST['pgNum'])) {
//Empty page number
unset($_POST);
header('Location: index.php?empty=2');
}
//Insert the new book into the database and redirect back to index.php
$conn = getConnection();
$update = "INSERT INTO Books (name, pageNumber) VALUES(\"{$_POST['newBook']}\", {$_POST['pgNum']});";
booleanQuery($conn, $update);
mysqli_close($conn);
header('Location: index.php');
} else {
//No POST data
//HTML to add page title
?>
<head>
<title>Books</title>
</head>
<?php
//Retrieve the list of books
$conn = getConnection();
$query = 'SELECT * FROM Books ORDER BY id;';
$result = mysqli_query($conn, $query);
if(mysqli_errno($conn)) { //Display error if query failed
echo "<br>Query error:" . mysqli_error($conn) . "<br>" . mysqli_errno($conn) . "<br>";
exit();
}
mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html>
<body OnLoad="document.form1.pgNum.focus()"> <!--Set cursor focus to the first form field-->
<?php
//For each book in the database, show it's current page number and create a form for submitting a page update
$formNumber = 1;
while($row = mysqli_fetch_array($result)) {
echo "\"{$row['name']}\" page number: {$row['pageNumber']}<br>";
echo '<form action="index.php" method="post" name="form' . $formNumber . '">';
echo 'New Page Number: <input type="text" name="pgNum">';
echo '<input type="hidden" name="id" value="' . $row['id'] . '">';
echo '<input type="submit" value="Update">';
echo '</form><br>';
$formNumber++;
}
echo "<br>";
//Check if this page loaded from an unsuccessful form submission
if(isset($_GET['empty'])) {
//$_GET['empty'] equals 1 if book name is empty, 2 if page number is empty, and 3 if both are empty.
switch($_GET['empty']) {
case 1:
echo '<font color="red">Missing book name</font><br>';
break;
case 2:
echo '<font color="red">Missing page number</font><br>';
break;
case 3:
echo '<font color="red">Empty fields</font><br>';
break;
}
}
?>
<!--Create a form for adding a new book to the database-->
<form method="post">
New Book: <input type="text" name="newBook"><br>
Page Number: <input type="text" name="pgNum">
<input type="submit" value="Add">
</form>
</body>
</html>
Edit: I used to have the full qualified http:// address in all the header lines and still had the same error. I changed them to the shortened versions during debugging.

Categories