Session not passing POSTed data to second script - please advise - php

Tried asking this, it was marked duplicate with a suggestion. I employed the suggestions which still do not seem to be performing the desired result. Could someone actually read the following and advise where I am going wrong?
I am attempting to use sessions to pass the user selected data from this script (allotment.php) to a subsequent script (allotmentreport.php) wherein it is used in a query qualifier (e.g. ...WHERE tablecolumndata=session variable...). I am not getting an error from allotment.php upon selecting the option and clicking SUBMIT but data fails to pass to allotmentreport.php and returns an error for an undefined variable.
Is this and the line after it correct? Is there something I am missing otherwise?
$tourselect=(isset($_POST['submit']));
UPDATE The final and corrected code is displayed below for future users seeking a working example and easy to read solution:
<?php
session_start();
$host="localhost";
$username="HIDDEN";
$password="HIDDEN";
$dbname="bookings2015";
$con = mysql_connect($host, $username, $password, $dbname);
if (!$con)
{
die ('damn thing wont connect to the MYSQL server: Maybe it is retarded '. mysql_error());
}
mysql_select_db($dbname, $con);
?>
<!Doctype html>
<html>
<?php include 'C:\xampp\htdocs\phpproject1\head.php';
include 'config\menu.php';
?>
<div id="dataentry">
<div id="submit">
<?php
echo "Which Tour to check availability? ";?>
<br />
<br />
</br>
</br>
<form method="post" action="allotment_report.php">
<select name='TourCode'>
<?php
$tourselection = "SELECT DISTINCT TourCode FROM toursanddates ORDER BY TourCode";
$result = mysql_query($tourselection);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['TourCode'] . "'>" . $row['TourCode'] . "</option>";
}
?>
</select>
<input type="submit" name="tourselected" value="submit">
</form>
<?php
?>
</div>
</div>
<div id="demographicborder">
<?php
include 'footer.php';?>
</div>
</div>
</body>
</html>
</form>
</form>
</div>
</div>
</div>
</body>
</html>
and here is the allotment_report.php code
<?php
session_start();
$host="localhost";
$username="STILLHIDDEN";
$password="STILLHIDDEN";
$dbname="bookings2015";
$con = mysql_connect($host, $username, $password, $dbname);
if (!$con)
{
die ('damn thing wont connect to the MYSQL server: Maybe it is retarded '. mysql_error());
}
mysql_select_db($dbname, $con);
include 'C:\xampp\htdocs\phpproject1\head.php';
include 'config\menu.php';
?>
<br />
<br />
<?php
//Table Header:
echo " <strong><u>Tour Availability</u>";
echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
<tr>
<td align=:"left"><b>Tour:</b></td>
<td align=:"left"><b>Start Date:</b></td>
<td align=:"left"><b>Seats Avail:</b></td>
<td align=:"left"><b>Rooms Avail:</b></td>
</tr>
';
if(isset($_POST['TourCode'])){
$tour=$_POST['TourCode'];
}
$status="ok";
$ar="SELECT TourCode, DATE_FORMAT (TourStart, '%m%d%y') AS TourStart, SeatsAvail, RoomsAvail FROM toursanddates WHERE TourCode='$tour' AND Status='$status' ORDER BY TourCode, TourStart ASC";
$result=mysql_query($ar);
$num_results = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)){
//Display the allotments fetched in above query
echo '<tr>
<td align=:"left">' . $row['TourCode'] . '</td>
<td align=:"left">' . $row['TourStart'] . '</td>
<td align=:"left">' . $row['SeatsAvail'] . '</td>
<td align=:"left">' . $row['RoomsAvail'] . '</td>
</tr>
';
}
echo '</table>';
//echo "</strong>Tour: ".($row['TourCode']);
//echo "</strong> Start Date: ".($row['TourStart']);
?>
<br />
<?php
echo "<br />";
echo "</p>";
?>
</br>
</br>
</div>
</form>
</div>
<div id="demographicborder">
<?php include 'footer.php';
?>
</div>
</div>
</body>
</html>
</form>
</form>
</div>
</div>
</div>
</body>
</div>
</body>

<?php
//contained within allotment.php
$tourselect=(isset($_POST['tourselected']));
$_SESSION['tourselected']=$tourselect;
?>
It looks like you are expecting $_SESSION['tourselected'] to be set on allotment.php the when a user loads opens the page for the first time. However this is not the case. $_POST data is attached with an HTTP request. When you load alloment.php for the first time, the browser doesn't send any $_POST data to it. This would explain why $_SESSION['tourselected'] is unset when you get to your second script.
That said, if your only goal is to send data from the form built in alloment.php to alloment_report.php you shouldn't be using sessions at all. All of this can be done with only $_POST.
consider the following code:
<!--alloment.php-->
<form method="post" action="alloment_report.php">
<select name='TourCode'>
<?php
//Assume that $options contains stuff pulled from your database.
foreach($options as $option) {
echo "<option value='" . $option . "'>" . $option . "</option>";
}
?>
</select>
<input type="submit" name="tourselected" value="submit">
</form>
When a user completes the form, and clicks submit, alloment_report.php (specified by action="alloment_report") gets the data sent from the form over $_POST (specified by method="post").
<!--alloment_report.php-->
<?php
if(isset($_POST['tourcode'])){
echo "yay! the tour has been selected!";
}
?>

Related

my data isnt inserting into mySQL data table with php

I need help on my code. When data is entered on my site, it does not show up in the mySQL data table. The insert function that I used might be the problem, but I cannot figure out how to get it to actually insert and show up in my table in my database. Can someone please guide me in the right direction with my code?
<?php
session_start();
include("db_connect.php");
if(isset($_POST['submit'])){
$item = $_POST['item'];
if(empty($item)) {
$errors = "you must enter something";
}
else{
mysqli_query("INSERT INTO a4_todolist (item) VALUES ('$item')");
header('location: index.php');
}
}
$a4_todolist = mysqli_query("SELECT * FROM a4_todolist");
?>
<!DOCTYPE html>
<html>
<head>
<title> Assignment 4 - To Do List </title>
<link rel ="stylesheet" type ="text/css" href="style.css">
</head>
<body>
<div class "head">
<h2> To Do </h2>
</div>
<form method= "POST" action = "index.php">
<?php if (isset($errors)) { ?>
<p><?php echo $errors; ?></p>
<?php } ?>
Item <input type = "text" name= "item" class="item_input">
Author <input type = "text" name= "author" class="author_input">
<button type = "submit" class="add-btn" name="submit"> Add Task
</button>
</form>
<table>
<tbody>
<?php while ($row = mysqli_fetch_array($a4_todolist)) { ?>
<tr>
<td class="id"> <?php print $row['id']; ?> </td>
<td class="item"> <?php echo $row['item']; ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
</thread>
</body>
</html>
You have missed the sql connection variable which is coming from db_connect.php file inside your mysqli_query. Your mysqli_query() should be like this
mysqli_query($connection,"INSERT INTO a4_todolist (item) VALUES ('$item')");
Also this
$a4_todolist = mysqli_query($connection,"SELECT * FROM a4_todolist");
It seems that you are a beginner so I recommend you to learn Prepared statements which is more efficient and safe to use.
You should pass the connection link identifier as well as you can check for errors.
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Also after executing query you can again check for error.
if (!mysqli_query($con,"INSERT INTO a4_todolist (item) VALUES ('$item')")) {
echo("Error description: " . mysqli_error($con));
}

php: Unable to take value from the user by using submit button

This is my db : link link
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM org_insert");
echo "<!doctype html>
<html lang=\"en\">
<head>
<!-- Bootstrap CSS -->
<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css\">
<title>Hello, world!</title>
</head>
<body>
<table border='1'>
<tr>
<th>below_whom</th>
<th>name</th>
</tr>";
$row = mysqli_fetch_array($result);
#echo '<pre>'; print_r($row); echo '</pre>';
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['below_whom'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<div class="form-group">
<label for="usr">below_whom:</label>
<input type="text" name ="below_whom" id="below_whom" class="form-control">
</div>
<div class="form-group">
<label for="usr">name:</label>
<input type="text" name ="name" id="name" class="form-control">
</div>
<form method="post">
<input type="button" name="submit" id="submit" class="btn btn-primary" value="submit"/>
</form>
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit']))
{
if($below_whom !=''||$name !=''){
$below_whom=$_POST['below_whom'];
$name=$_POST['name'];
$query=mysqli_query("INSERT INTO org_insert VALUES ('$below_whom','$name');");
$query_run = mysqli_query($con,$query);
echo "<p>query inserted.</p>";
}else{
echo "<p>Insertion Failed.</p>";
}
}
mysqli_close($con);
echo"</body>
</html>";
?>
The text under p tag isn't getting executed, ie, the program is not going inside the if statement itself. I have rechecked the syntax, what is the problem? Is the syntax incorrect? I am pretty sure the connection with sql is correct. I have also refereed to some articles, still I am stuck here.
use post variables before if loop as shown below
if(isset($_POST['submit']))
{
$below_whom=$_POST['below_whom'];
$name=$_POST['name'];
if($below_whom !=''||$name !=''){
$query=mysqli_query("INSERT INTO org_insert VALUES ('$below_whom','$name');");
$query_run = mysqli_query($con,$query);
echo "<p>query inserted.</p>";
}else{
echo "<p>Insertion Failed.</p>";
}
}
and in HTML Code add type as submit and start form tag before div as
<form method="post" action=""> and closes after input tag
<input type="submit" name="submit" id="submit" class="btn btn-primary" value="submit"/>
One of the issues that I am able to see is that Your query should be:
$query=mysqli_query("INSERT INTO `org_insert`(`below_whom`,`name`) VALUES ('$below_whom','$name')");
Hope this helps.
Change the mysqli_fetch_array to mysqli_fetch_assoc or add a parameter too
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);

GET and POST in PDO

I have problem in sample page that will retrieve student id from the url ' update.php?id=1' to use it in query , when I write the id manually in the query it's worked .. can't figured out
update.php
<?php
require 'database.php';
$id=$_GET['id'];
if (!empty($_POST)) {
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$pdo=Database::connect();
$sql="UPDATE students SET name=? , mobile=? WHERE id=?";
$q=$pdo->prepare($sql);
$q->execute(array($name,$mobile,$id));
Database::disconnect();
header('Location: index.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div>
<div>
<div>
<h3>Update Student Information</h3>
</div>
<form action="update.php" method="post">
<div>
<label>Name</label>
<div>
<input name="name" type="text">
</div>
</div>
<div>
<label>Mobile No.</label>
<div>
<input name="mobile" type="text">
</div>
</div>
<div>
<button type="submit">Add</button>
Back
</div>
</form>
</div>
</div>
</body>
</html>
UPDATE : To make this clear the id retrieve from url address from other page like I said not from the html form ... url ' update.php?id=1' how should I do it ?
index.php
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM students';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['mobile'] . '</td>';
echo '<td>Show</td>';
echo '<td>Update</td>';
echo '</tr>';
}
Database::disconnect();
?>
To see what error the PDO throws, add the following code:
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
also you should add these (at the top of your file):
ini_set('display_errors', 1);
ini_set('display_startup_errors',1);
error_reporting(-1);
Another idea: Maybe the stuff from post is empty?
Why not send the id variable with the POST form as well?
<input type="hidden" name="id" value="x">
Then retrieve it with $_POST['id'] instead of $_GET['id']
your didnt use $id in your query
it should be some thing like $sql="update students SET name=? , mobile=? WHERE id=".$id." "

open files(pdf,word,excel) in php(mysql database is used)

i am new to php and mysql.i have created a php page for uploading files and these files are saved to my local drive.In another page when a user selects a option files relating to the option are displayed in table format.one column will be link to the file in local drive.upto here i was successful.
Problem is that when clicking on the file hyperlink nothing is displayed.but when i copy address link of the file and paste in new window it is opening.please help me I am stuck for 2 weeks with problem.
To experiment I Have created simple php file just to see the hyperlink working.but no success.I will paste here the trails i have used
<?php echo "<a href=`file:///C|/Inetpub/wwwroot/help.gif' >hi</a>";?>-----not worked
<?php echo "<a href=http://www.google.com>hi</a>";?> ------ working.
even " " tags are not working if files are saved as php file.....i am using ISp server.my php files are saved in "C:\Inetpub\wwwroot\phpsample" path
please help me find solution
----my original code----
<form action="http://localhost/phpsample/upload/upload2.php" method='post'enctype='multipart/form-data'>
Instrument:<select name="flist">
<option>select</option>
<option>gt1</option>
<option>gt2</option>
<option>gt3</option>
<option>gt4</option>
</select>
</br>
Date: <input type="date" name="fdate" /></br>
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" />
</form>
<?php
global $dest;
global $name;
$dest='E:\divya';
$name=$_FILES["file"]["name"];
echo $dest;
move_uploaded_file($_FILES["file"]["tmp_name"],"$dest/$name");
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("upload_db", $con);
mysql_query("INSERT INTO upload_tb (Instrument, date_upload, filename)
VALUES('$_POST[flist]','$_POST[fdate]','$name')");
mysql_close($con);
?>
</body>
</html>
----my original code to download file from table----
<form action="http://localhost/phpsample/display/display.php" method='post'enctype='multipart/form-data'>
Instrument:
<select name="flist">
<option>select</option>
<option>gt1</option>
<option>gt2</option>
<option>gt3</option>
<option>gt4</option>
</select>
</br>
Date:
<input type="date" name="fdate" /></br>
<input type="submit" />
</form>
<?php
echo $_POST['fdate'];
echo $_POST['flist'];
$path='file:\\\C:\\Inetpub\\wwwroot\\phpsample\\display\\NewTextDocument.txt';
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("upload_db", $con);
$result = mysql_query("SELECT * FROM upload_tb WHERE Instrument='$_POST[flist]'");
echo "<table border='1'>
<tr>
<th>Slno</th>
<th>Name</th>
<th>comment</th>
</tr>";
fopen("c:\Inetpub\wwwroot\phpsample\display\NewTextDocument.txt","r");
while($row = mysql_fetch_array($result))
{
$filename=$row['filename'];
echo $filename;
echo "<tr>";
echo "<td>" . $row['Instrument'] . "</td>";
echo "<td>" . $row['date_upload'] . "</td>";
echo "<td>"."<a href= $path>". $row['filename'] ."</a>"."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
Put this script into download.php in the same folder as upload2.php, and invoke it as
http://localhost/phpsample/upload/download.php?file=FILE
where FILE is the filename that was uploaded.
<?php
$con = mysqli_connect("localhost","root","", "upload_db");
if (!$con) die('Could not connect: ' . mysqli_connect_error());
$name = $_GET['file'];
$query_filename = mysqli_real_escape_string($dbh, $name);
$src = 'E:\divya';
$data = mysqli_query($con, "select filename from upload_tb where filename = '$query_filename'");
if (mysqli_num_rows($data) ! 1) die ('File not uploaded: ' . htmlentities($name));
$path = "$src/$name";
http_send_content_disposition($name);
$type = mime_content_type($path);
http_send_content_type($type);
http_send_file($path);
exit;
?>

HTML Radiobutton form not POSTing

I have written a small HTML form and added it to the page. My goal is for it to POST the value of the Checked button to a PHP page which I have also written. The PHP page is not getting the value for some reason. I am also not getting any PHP errors. The codes are below.
form.php
<form action="http://www.zbrowntechnology.com/InsaneBrain/quiz.php" method="POST">
<font color="white" size="3">
<?php
$con = mysql_connect("HOST", "USER", "PASS");
if(!$con) {
die('Unable to connect to MySQL: '.mysql_error());
}
mysql_select_db("zach_insaneB", $con);
$result = mysql_query("SELECT Name FROM quiz");
while($row = mysql_fetch_assoc($result)) {
$qname = $row['Name'];
echo "<input type='radio' name='button1' id='$qname'>";
echo "<label for='$qname'><font color='white'/>$qname</font></label>";
}
?>
</font>
</div>
</div>
<div id="Oobj12">
<div id="Gcode234" class="dfltc">
<input type="image" src="http://www.zbrowntechnology.com/InsaneBrain/begin.png" alt="Begin" />
</form></div>
</div>
getdata.php
<?php
$data = $_POST['button1'];
echo $data;
?>
Actually, I see the problem...you don't actually have a value in the radio button. You need something like:
echo "<input type='radio' name='button1' id='$qname' value='$some_value'>";

Categories