Using an array with SESSION or POST - php

I am trying to use the $_POST feature or $_SESSION feature of php to get an array based off form data from a previous pagepage. Whenever I try using $_SESSION I get the error:"The script tried to execute a method or access a property of an incomplete object." Here is an idea of what I'm trying to do. Any help would be great and thanks in advance!
<?php
// initialize a session
include 'hoteldetails.php';
session_start();
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<center><h1>Room Reserves</h1>
<h2>Search</h2></center>
<h3>Login <br/> Sign up</h3>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
Please enter City, State or Zipcode:<input type="text" name="location">
<input type="date" name="startdate" value="startdate">
to <input type="date" name="enddate" value="enddate">
<br/>
Minimum price:<select name="minimumprice">
<option value="0">No Minimum</option>
<option value="49">$50</option>
<option value="99">$100</option>
<option value="199">$200</option>
</select>
Maximum price:<select name="maximumprice">
<option value="9999">No Maximum</option>
<option value="101">$100</option>
<option value="201">$200</option>
<option value="301">$300</option>
</select>
Beds:<select name="beds">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
Bathrooms:<select name="baths">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br/>
<input type="submit" name="Search" value="Search">
</form>
<?php
try{
$dbconn= pg_connect("host=localhost port=5432 dbname=roomReserves user=superUser password=F3Poriginal");
}
catch(PDOException $e){
echo $e->getMessage();
}
if(isset($_POST['Search'])){
if($_POST['startdate']==null || $_POST['enddate']==null || $_POST['location']==null){
echo "One or more fields are empty, please try again.";
}
else{
$query = "SELECT * FROM hotellist WHERE zip=75007";
$rs = pg_query($dbconn, $query) or die("Cannot execute query: $query\n");
// $hotelsdetail=array();
$_SESSION['hotelsdeets']=array();
$hotelpos=0;
while ($row = pg_fetch_row($rs)) {
$serverInfo= explode(" ",$row[2]);
$hotelsdb= pg_connect("host=$serverInfo[0] port=$serverInfo[1] dbname=$serverInfo[2] user=$serverInfo[3] password=$serverInfo[4]");
$queryresult = "SELECT * FROM hotelinfo where beds= CAST('".$_POST['beds']."' AS INT) AND baths= CAST(baths='".$_POST['baths']."' AS INT) AND price BETWEEN CAST('".$_POST['minimumprice']."' AS INT) AND CAST('".$_POST['maximumprice']."' AS INT)";
$hotelrs = pg_query($hotelsdb, $queryresult) or die("Cannot query hotel info: $queryresult\n");
$matchesquery=false;
while($hotelrow=pg_fetch_row($hotelrs) && $matchesquery==false){
$matchesquery=true;
$date = $_POST['startdate'];
$endDate= $_POST['enddate'];
$hotelroomrs=pg_fetch_row($hotelrs);
$hotelroom=$hotelroomrs[0];
$hotelprice=$hotelroomrs[1];
while ($date < $endDate && $matchesquery==true && $hotelroomrs[0]!=null) {
$isavail="SELECT * FROM hotelinfo where roomnumber= CAST('".$hotelroom."' AS INT) AND date= CAST('".$date."' AS date)";
$roomexists=pg_query($hotelsdb, $isavail);
$roomdets=pg_fetch_row($roomexists);
if($roomdets[0]==null){
$matchesquery=false;
}
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
if($matchesquery==true && $hotelroomrs[0]!=null){
$_SESSION['hotelsdeets'][$hotelpos]=new hoteldetails();
$_SESSION['hotelsdeets'][$hotelpos]->sethotelinfo($row);
$_SESSION['hotelsdeets'][$hotelpos]->setprice($hotelprice);
$hotelpos++;
}
}
pg_close($hotelsdb);
}
header("Location: ./searchresults.php");
}
}
pg_close($dbconn);
?>
<?php
session_start();
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<center><h1>Room Reserves</h1>
<h2>Results</h2></center>
<h3>Login<br/>Sign up</h3>
</head>
<body>
<?PHP
//$searchinfo->$hotelsdetail[0]->sethotelinfo(1);
echo $_SESSION['hotelsdeets'][0]->gethotelinfo()[1];
?>
</body>
</html>
class hoteldetails {
private $hotelinfo;
private $price;
public function sethotelinfo($hotelinfo){
$this->hotelinfo=$hotelinfo;
}
public function setprice($price){
$this->price=$price;
}
public function gethotelinfo(){
return $this->hotelinfo;
}
public function get($var){
return $this->{$var};
}
}
Once again, THANKS!

As a starting point, you need to open and close the php tags:
<?PHP
session_start();
?>
<html>
<head>
</head>
<body>
<?PHP
$colors=array();
array_push($colors, "green");
array_push($colors, "white");
array_push($colors, "yellow");
array_push($colors, "black");
$_SESSION['color']=$colors;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="Search" value="Search">
</form>
<?php
if(isset($_POST['Search'])){
header("Location: page2.php");
}
?>

Related

How to display selected value in combo box in php?

I've written this code... but this fails to display text... not sure what's wrong with the code. I'm new to PHP and trying to create a page that gets customer details and puts it in SQL.
<!DOCTYPE html>
<html>
<body>
<?php
$initial="";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
echo "Customer Intial: $initial <br>";
?>
</body>
</html>
Try this:
$initial="";
if(isset($_POST['initial'])){
$initial=htmlentities($_POST['initial']);
}
You correctly sent the POST however you didn't put the value given with the POST into the $initial variable.
<!DOCTYPE html>
<html>
<body>
<?php
$initial = "";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
$initial = filter_input(INPUT_POST, "initial");//GET the input in post method
if ($initial == 1) {
$initial_value = "Mr.";
} elseif ($initial == 2) {
$initial_value = "Mrs.";
} elseif ($initial == 3) {
$initial_value = "Ms.";
} elseif ($initial == 4) {
$initial_value = "M/s";
} else {
$initial_value = "Select initial";
}
echo "Customer Intial: $initial_value <br>";
?>
</body>
</html>
Try,
<form method="post" action=<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>>
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<br>
<br>
<?php
if(isset($_POST['initial'])){
$initial=$_POST['initial'];
} else {
$initial = "empty";
}
echo "Customer Intial : ".$initial;
?>
</body>
</html>

php mysqli I do not get information not available

I have 2 tables, group1 and group2.
With 3 columns, 'id',
'status' (0-Available, 1-Not available) and
'description' for the number 0 and 1 (Available and Not available)
When there is a 0 in the column it works well, and it shows the 'description' for that number,but
when all the numbers in the column are 1 it will not output my
else statement, dont know why? It standing still in the if statament.
I do not know how to output more rows
when i have more zeros in the table.
<?php include('config.php'); ?>
<!DOCTYPE>
<head>
<title>test</title>
<meta charset="UTF-8">
</head>
<body>
<form name='search' method="post">
<select name='onoroff'>
<option value='group1' >Group 1</option>
<option value='group2' >Group 2</option>
</select>
</datalist>
<input type='submit' name='run' value="Go"/>
</form>
<?php
//////////////////////////////////////////////
$group = ($_POST['onoroff']);
$q= mysqli_query($dbc, "SELECT * FROM $group WHERE status = 0");
$r= mysqli_fetch_assoc($q);
if (isset($_POST['run'])) {
echo '<p>'.$r['description'].'</p>';
} else { echo '<p>Not available</p>';}
///////////////////////////////////////////////
?>
</body>
</html>
You should put the database query inside the if(isset($_POST['run'])) block. Otherwise, you're trying to use $_POST['onoroff'] even though the user hasn't submitted the form yet.
if (isset($_POST['run'])) {
$group = ($_POST['onoroff']);
$q= mysqli_query($dbc, "SELECT * FROM $group WHERE status = 0");
$r= mysqli_fetch_assoc($q);
echo '<p>'.$r['description'].'</p>';
} else {
echo '<p>Not available</p>';
}
Adding the if statements with the returned value of the status will choose what is displayed depending on their status.
<?php include('config.php'); ?>
<!DOCTYPE>
<head>
<title>test</title>
<meta charset="UTF-8">
</head>
<body>
<form name='search' method="post">
<select name='onoroff'>
<option value='group1' >Group 1</option>
<option value='group2' >Group 2</option>
</select>
</datalist>
<input type='submit' name='run' value="Go"/>
</form>
<?php
//////////////////////////////////////////////
$group = ($_POST['onoroff']);
if (isset($_POST['run']))
{
$q= mysqli_query($dbc, "SELECT * FROM $group");
while ($r = mysqli_fetch_assoc($q))
{
if($r['status'] == 0) {
echo '<p>'.$r['id'].' : '.$r['description'].'</p>';
}
else {
echo '<p>'.$r['id'].' Not available</p>';
}
}
}
else
{
echo '<p>You need to submit to choose a group</p>';
}
///////////////////////////////////////////////
?>
</body>
</html>

how to change select tag values based on value coming from database

i want to change tag option based on values coming from database.if value is "1" i want to show different options and different for other values.
<<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
Because
No closing ; on here <option value='4'>Maths</option></select>"
and here too <option value='8'>Maths</option></select>"
First if() condition missing end }.
So final code is
<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number = $_POST['number'];
//echo $number;
if ( $number == "1" )
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
You are missing the ; off of the end of both echos:
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
Note: Sorry about poor layout, quick posting of answer!
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"; //missing ; here
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"; //missing ; here
}
} // missing } here

I want to select a table from a database and show in a drop down list

I wanted to select a table from a specific database and then the list of tables will appear in an option tag under select tag.
Thanks a lot.
Current Code:
<?php
include 'database/connectdatabase.php';
if(isset($_POST['select_db']))
{
$select_db = $_POST['select_db'];
$selectdb_query = 'SHOW TABLES FROM $select_db';
$query_select = mysql_query($selectdb_query,$connectDatabase);
if(!$query_select)
{
echo 'NO table selected!';
}
?>
<form method="POST" action="selecttable.php" autocomplete="off">
<select name="select_db">
<option selected="selected">Select Database</option>
<option>section_masterfile</option>
</select>
</form>
<form method="POST" action="#" autocomplete="off">
<?php while ($row = mysql_fetch_row($query_select)) {
$num_row = mysql_num_rows($row);?>
<select name="select_table">
<option selected="selected">Select Table</option>
<?php for($i=0;$i>=$num_row;i++){?>
<option><?php echo $row[0];?></option>
<?php}?>
</select>
<?php}?>
</form>
The problem is that you're already fetching rows yet you haven't even submitted the form yet.
I suggest restructure you logic this way:
$con = new mysqli('localhost', 'username', 'password', 'database');
$tables = array();
if(isset($_POST['select_db'])) { // if its submitted
$select_db = $con->real_escape_string($_POST['select_db']); // escape the string
$query = $con->query("SHOW TABLES FROM $select_db");
while($row = $query->fetch_assoc()) {
$tables[] = $row['Tables_in_' . $select_db]; // use associative instead
}
}
?>
<form method="POST" autocomplete="off">
<select name="select_db" onchange="this.form.submit();">
<option disabled selected>Select Database</option>
<option>test</option>
</select>
<br/><br/>
<select name="select_table">
<?php foreach($tables as $table): ?>
<option value="<?php echo $table; ?>"><?php echo $table; ?></option>
<?php endforeach; ?>
</select>
</form>
Sidenote: If you have turned on the error reporting, this should have given some red light to what you are doing wrong. Kindly turn it on.
error_reporting(E_ALL);
ini_set('display_errors', '1');
<select name="select_table">
<option selected="selected">Select Table</option>
<?php while ($row = mysql_fetch_row($query_select)) { ?>
<option value = "<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
<?php } ?>
</select>
Try This
<form method="POST" action="#" autocomplete="off">
<select name="select_table">
<option selected="selected">Select Table</option>
<?php
while ($row = mysql_fetch_row($query_select))
{
?>
<option value="<?php echo $row[0];?>"><?php echo $row[0];?></option>
<?php
}
?>
</select>
</form>
Consider this code:
<form method="POST" action="#" autocomplete="off">
<select name="select_table">
<option selected="selected">Select Table</option>
<?php
while ($row = mysql_fetch_row($query_select)) {
?>
<option><?php echo $row[0];?></option>
<?php
}
?>
</select>
</form>

retrieve data and display in droplist (php/mysql)

can anyone tell me what is the error in this dam code i went crazy searching about the error but without any success
the error is that i do not get any data from the table why is that happening?? plz help me
index.php
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label for="searchByCountry">By Country</label>
<select name="searchByCountry" id="searchByCountry">
<option id="0">--select your country--</option>
<?php
require_once('connfor_lamelchameltest.php');
$getallCountries = mysql_query("SELECT country_name FROM country") or die("could not search for countries");
if(mysql_num_rows($getallCountries) > 0) {
while($viewallCountries = mysql_fetch_array($getallCountries)){
?>
<option id="<?php echo $viewallCountries['country_name']; ?> "></option>
<?php } ?>
<?php } ?>
</select>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label for="searchByCountry">By Country</label>
<select name="searchByCountry" id="searchByCountry">
<option id="0">--select your country--</option>
<?php
require_once('connfor_lamelchameltest.php');
$getallCountries = mysql_query("SELECT country_name FROM country") or die("could not search for countries");
if(mysql_num_rows($getallCountries) > 0) {
while($viewallCountries = mysql_fetch_array($getallCountries)){
?>
<option value="<?php echo $viewallCountries['country_name']; ?> ">
<?php echo $viewallCountries['country_name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
</form>
</body>
</html>

Categories