PHP session is not storing any data in my array - php

It was working, but only on chrome and only on my PC, suddenly it stopped working completely, after I closed and reopened chrome. Now it wont store any data in my session.
http://xeon.spskladno.cz/~filipt/Fourth.php
<?php
session_start();
print_r($_SESSION);
$num1=$_POST["num1"];
$num2=$_POST["num2"];
$action=$_POST["action"];
$memory=$_POST["memory"];
if($action=='+')
$vys=$num1+$num2;
if($action=='-')
$vys=$num1-$num2;
if($action=='*')
$vys=$num1*$num2;
if($action=='/')
$vys=$num1/$num2;
?>
<!DOCTYPE html>
<html><body>
<form action="Fourth.php" method="post">
Cislo1: <br>
<input type="number" name="num1" value="<?php echo $num1;?>" >
<br>
Cislo2: <br>
<input type="number" name="num2" value="<?php echo $num2;?>">
<br>
Akce: <br>
<select name="action" >
<option value="+">Scitani</option>
<option value="-">Odcitani</option>
<option value="*">Nasobeni</option>
<option value="/">Deleni</option>
</select>
<br>
Pocet ulozenych vypoctu:<br>
<input type="number" name="memory" value="<?php echo $memory;?>">
<br><br><br>
<input type="submit" value="Spocti">
</form>
</body>
</html><?php
if((strlen(trim($_POST["num1"]))==0)||(strlen(trim($_POST["num2"]))==0)||(strlen(trim($_POST["memory"]))==0))
die("Vyplnte prosim obe cisla a pocet ulozenych vypoctu.");
if($action=='+'){
echo "Vysledek prikaldu ",$num1,$action,$num2," je ",$vys;
}
if($action=='-'){
echo "Vysledek prikaldu ",$num1,$action,$num2," je ",$vys;
}
if($action=='*'){
echo "Vysledek prikaldu ",$num1,$action,$num2," je ",$vys;
}
if($action=='/'){
echo "Vysledek prikaldu ",$num1,$action,$num2," je ",$vys;
}
$string="$num1$action$num2=$vys";
array_push($_SESSION['vysledky'],$string);
$con=count($_SESSION['vysledky']);
if($con>$memory){
$rozdil=$con-$memory;
for($i=0; $i<$rozdil; $i++)
array_shift($_SESSION['vysledky']);
}
echo "<br>Ulozene vypocty:<br> ";
foreach ($_SESSION['vysledky'] as $value) {
echo "$value <br> ";
}
?>

Related

having 2 problems with my php code im trying to do a link between php, html and sql and i cant get the alter button to work without some PDO error

This is my main code without the conn.php
im having problems to do a button that alter the current register
on the sql
the html boxes are always with the
Warning: Undefined variable $row in
C:\xampp\htdocs\atv3\index.php on line 55Warning: Trying to access array offset on value of type null
in C:\xampp\htdocs\atv3\index.php on line 55
error
and when i click to alter the register i get the PDO error that i posted a image
its
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter
number: number of bound variables does not match number of tokens in
C:\xampp\htdocs\atv3\index.php:94 Stack trace: #0
C:\xampp\htdocs\atv3\index.php(94): PDOStatement->execute() #1 {main}
thrown in C:\xampp\htdocs\atv3\index.php on line 94
LINE 55:
<input type="text" name="pdc"
placeholder="preço de custo" value="<?php echo $row['custo_prod'];?>"/><br/>
Line 94:
$altera->execute();
MAIN CODE
<form action="index.php" method="POST">
<input type="text" name="ndp"
placeholder="nome do produto"/><br/>
<br/>
<select name="op">
<option value="s1">setor 1</option>
<option value="s2">setor 2</option>
<option value="s3">setor 3</option>
<option value="s4">setor 4</option>
</select><br/>
<br/>
<input type="text" name="pdc"
placeholder="preço de custo"/><br/>
<br/>
<input type="text" name="pdv"
placeholder="preço de venda"/><br/>
<br/>
<input type="text" name="estoque"
placeholder="estoque"/><br/>
<br/>
<input type="submit" name="grava"
value="Gravar"/>
</form>
<br/>
<br/>
<?php
include "conn.php";
if(isset($_GET['alterar'])){
$id=$_GET['id'];
$consu=$conn->prepare('
SELECT * FROM `cadastro`
WHERE `id_prod`= :pid;');
$consu->bindValue(':pid',$id);
$consu->execute();
$row=$consu->fetch();
}
?>
<form action="index.php" method="POST">
<input type="hidden" name="id"
value="<?php echo $row['id_prod']; ?>">
<input type="text" name="nome_prod"
placeholder="Nome" value="<?php echo $row['nome_prod'];?>"/><br/>
<input type="text" name="setor_prod"
placeholder="setor" value="<?php echo $row['setor_prod'];?>"/><br/>
<input type="text" name="custo_prod"
placeholder="preço de custo" value="<?php echo $row['custo_prod'];?>"/><br/>
<input type="text" name="venda_prod"
placeholder="preco de venda" value="<?php echo $row['venda_prod'];?>"/><br/>
<input type="text" name="estoque_prod"
placeholder="estoque" value="<?php echo $row['estoque_prod'];?>"/><br/>
<br/>
<input type="submit" name="altera"
value="Alterar"/>
</form>
<?php
include "conn.php";
if(isset($_POST['altera'])){
$nome=$_POST['nome_prod'];
$op=$_POST['setor_prod'];
$preco_de_custo=$_POST['custo_prod'];
$preco_de_venda=$_POST['venda_prod'];
$estoque=$_POST['estoque_prod'];
$situacao_do_produto=1;
$altera=$conn->prepare('
UPDATE `cadastro` SET
`nome_prod` = :pnome,
`setor_prod` = :pop,
`venda_prod` = :pvenda,
`custo_prod` = :pcusto,
`estoque_prod` = :pestoque,
`situacao_prod` = :psituacao;
WHERE `cadastro`.`id_prod` = :pid;');
$altera->bindValue(':pnome',$nome);
$altera->bindValue(':pop',$op);
$altera->bindValue(':pvenda',$preco_de_custo);
$altera->bindValue(':pcusto',$preco_de_venda);
$altera->bindValue(':pestoque',$estoque);
$altera->bindvalue(':psituacao', $situacao_do_produto);
$altera->execute();
echo "Cadastro alterado com sucesso!";
}
if(isset($_POST['grava'])){
$nome=$_POST['ndp'];
$op=$_POST['op'];
$preco_de_custo=$_POST['pdc'];
$preco_de_venda=$_POST['pdv'];
$estoque=$_POST['estoque'];
$situacao_do_produto=1;
$grava=$conn->prepare('INSERT INTO
`cadastro` (`id_prod`, `nome_prod`,
`setor_prod`, `custo_prod`, `venda_prod`, `estoque_prod`, `situacao_prod`) VALUES
(NULL, :pnome, :pop, :pcusto, :pvenda, :pestoque, :psituacao);');
$grava->bindValue(':pnome',$nome);
$grava->bindValue(':pop',$op);
$grava->bindValue(':pvenda',$preco_de_custo);
$grava->bindValue(':pcusto',$preco_de_venda);
$grava->bindValue(':pestoque',$estoque);
$grava->bindValue(':psituacao',$situacao_do_produto);
$grava->execute();
echo "Gravado!";
}
if(isset($_GET['excluir'])){
$id=$_GET['id'];
$excluir=$conn->prepare('DELETE
FROM cadastro WHERE
`cadastro`.`id_prod` = :pid');
$excluir->bindValue(':pid',$id);
$excluir->execute();
echo "Excluído com sucesso!";
}
?>
<table border="1">
<tr>
<th>Nome do produto</th>
<th>Setor do produto</th>
<th>Preço de custo</th>
<th>Preço de venda</th>
<th>Estoque</th>
<th></th>
</tr>
<?php
$exibir=$conn->prepare('
SELECT * FROM `cadastro`');
$exibir->execute();
if($exibir->rowCount()==0){
echo "Não há registros";
}else{
while($row=$exibir->fetch()){
echo "<tr>";
echo "<td>".$row['nome_prod']."</td>";
echo "<td>".$row['setor_prod']."</td>";
echo "<td>".$row['custo_prod']."</td>";
echo "<td>".$row['venda_prod']."</td>";
echo "<td>".$row['estoque_prod']."</td>";
echo "<td><a href='index.php?excluir&id=".$row['id_prod']."'>Excluir</a></td>";
echo "<td><a href='index.php?alterar&id=".$row['id_prod']."'>Alterar</a></td>";
echo "</tr>";
}
}
?>
</table>

post dropbox value populated from database

iam trying to populate my dropbox from my database and then retrieve it in my php code...is this possible?
<form method="POST">
<select>
while ($fethc_items = mysql_fetch_assoc($items))
{
$item_id= $fethc_items['item_id'];
$itemname = $fethc_items['item_name'];
?>
<option value="<? echo $itemname;?>">Volvo</option>
<?}?>
<input type="submit" value="Submit" />
</form>
<?
if (isset($_POST['?']))
{
}
?>
Yes you can..
Try this code. I hope its will help you.
<form method="POST">
<select name="item">
<?php
while ($fethc_items = mysql_fetch_array($items))
{
$item_id= $fethc_items['item_id'];
$itemname = $fethc_items['item_name'];
?>
<option value="<?php echo $item_id;?>"><?php echo $itemname;?> </option>
<?php } ?>
</select>
<input type="submit" value="Submit" />
</form>
<?php
if (isset($_POST['item'])) {
}
?>
Thank You!

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>

Display database values on option

I am trying to display the saved data in the database on select before a user can update other choice. I am not really sure how to code it. Can anyone come out with possible solution? Had tried many ways but unable to do so.
Update: I have found out the problem why the options does not show.
This is my code:
`
$consentId = $_GET['consent_id'];
$retrieveConsent = "SELECT * FROM consent, leavetype WHERE consent.type_of_leave = leavetype.type_of_leave";
$retrieveResult = mysqli_query($link, $retrieveConsent) or die("Retrieve Error" . mysqli_error($link));
$queryleavetype = "SELECT * FROM leavetype";
$queryleaveresult = mysqli_query($link, $queryleavetype) or die("Leave Retrieve Error " . mysqli_error($link));
$row = mysqli_fetch_array($retrieveResult);
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edit Consent </title>
</head>
<body>
<div class="ui-content">
<h3><b>Edit Leave</b></h3>
<form action="doEditConsent.php" method="post" data-ajax="false">
<input type="hidden" name="cId" value="<?php echo $row['consent_id']; ?>"/>
<label for="dateFrom" ><b>Date From:</b></label>
<input type="date" id="dateFrom" name="newDateFrom" value="<?php echo $row['consent_date_from']; ?>" required>
<br>
<label for="dateTo" ><b>Date To:</b></label>
<input type="date" id="dateTo" name="newDateTo" value="<?php echo $row['consent_date_to']; ?>" required>
<br>
<label for="reason" ><b>Leave Type:</b></label>
<select name="leaveType" id="leaveType" data-mini="true">
<?php
while ($rowleave = mysqli_fetch_array($queryleaveresult)) {
?>
<option value="<?php echo $rowleave['type_of_leave']; ?>">
<?php echo $rowleave['leave_type']; ?>
</option>
<?php
};
?>
</select>
<br>
<button class="ui-btn ui-corner-all" type="submit" >Submit</button>
</form>
</div>
<?php
}
}
?>
</body>
</html>`
Try this, hopefully it will help you:
<select name="leaveType" id="leaveType" data-mini="true">
<option value=""></option>
<?php
$previous_selected_value = 'previous_selected_value';//get the previous value and assign it to this variable
while ($rowleave = mysqli_fetch_array($queryleaveresult)) {
$selected = ($rowleave['type_of_leave'] == $previous_selected_value) ? " selected='selected'" : "";
echo '<option value="'.$rowleave['type_of_leave'].'"'.$selected.'>'.$rowleave['leave_type'].'</option>';
}
?>
</select>

Passing function to another page in PHP

So this has been bugging me for sometime, I want to pass a calculation which I have stored in a function onto another page, I can pass field entries no problem (sorry im newish at PHP) but how do i pass my calculation from:
// calculation section (calculator.php) - this is a include on every page
The calculation is made by a users entries which is in a include on every page
// The thank-you.php page outputs a thank you comment and sends the email
I receive all the other info fine but the function won't come through in my email.
The output from the calculation is also stored in calculator.php which is the include but it outputs to the screen fine just not to my email :(.
Am I missing something?
Sorry (edit) here is my code:
<?php
error_reporting(E_ALL);
if(isset($_POST['name']) && isset($_POST['to'])){
ini_set('date.timezone', 'Europe/Madrid');
$now = date("H:i");
$cutoff = "06:00";
$higherthan = "22:00";
$name = $_REQUEST['name'];
$telephone = $_REQUEST['telephone'];
$from = $_REQUEST['from'];
$to = $_REQUEST['to'];
$date = $_REQUEST['date'];
$returndate = $_REQUEST['returndate'];
$people = $_REQUEST['people'];
$return = $_REQUEST['return'];
$myemail = $_REQUEST['myemail'];
include_once('includes/config.php');
$settingsSql = mysql_query("SELECT * FROM transfers_in WHERE location='$to' AND no_passengers='$people'");
$settings = mysql_fetch_assoc($settingsSql);
echo "From: ".$from." To: ".$settings['location']."<br />";
echo "Number of passengers: ".$settings['no_passengers']."<br />";
ini_set('date.timezone', 'Europe/Madrid');
$now = date("H:i");
$cutoff = "06:00";
$higherthan = "22:00";
echo "Time cost: ".$settings['price']." euros<br /><hr />Total: ";
function timeCost() {
$to = $_REQUEST['to'];
$people = $_REQUEST['people'];
$return = $_REQUEST['return'];
include_once('includes/config.php');
$settingsSql = mysql_query("SELECT * FROM transfers_in WHERE location='$to' AND no_passengers='$people'");
$settings = mysql_fetch_assoc($settingsSql);
//echo $return;
if ($return == "No"){
if ((strtotime($now) < strtotime($cutoff)) || (strtotime($now) > strtotime($higherthan))){
echo number_format($settings['price']) + 1.40;
} else {
echo number_format($settings['price']) + 0.00;
}
} elseif ($return == "Yes") {
if ((strtotime($now) < strtotime($cutoff)) || (strtotime($now) > strtotime($higherthan))){
echo number_format($settings['price']) * 2 + 1.40;
} else {
echo number_format($settings['price']) * 2 + 0.00;
}
}
echo " in euros<br /><br />";
}
echo timeCost();
} else { ?>
<form method="POST" action="thank-you.php" name="chooseDateForm" id="chooseDateForm">
<label>Name:</label>
<input type="text" value="" name="name" />
<label>Telephone:</label>
<input type="text" value="" name="telephone" />
<label>Email:</label>
<input type="text" value="" name="myemail" />
<label>From:</label>
<select name="from">
<option selected="selected">Malaga</option>
</select>
<div class="clr"></div>
<label>To:</label>
<select name="to">
<?php foreach ($data as $place => $price){
echo "<option>{$place}</option>\n";
}
echo '</select>
<div class="clr"></div>
<label>Date:</label>
<input type="text" value="dd/mm/yyyy" id="date" name="date" class="date-pick" />
<span id="calendar"></span>
<div id="return-journey">
<label>Return Date:</label>
<input type="text" value="dd/mm/yyyy" id="returndate" name="returndate" class="date-pick" />
<span id="calendar"></span>
</div>
<label>Number of people:</label>
<select id="people" name="people">
<option value="4">4</option>
<option value="6">6</option>
<option value="8">8</option>
</select>
<div class="clr"></div>
<div id="return">
<label>Is this a return<br />journey?</label>
<div class="clr"></div>
<div id="radio-buttons">
<input type="radio" name="return" value="Yes" class="radio returning" />Yes<br />
<input type="radio" name="return" value="No" class="radio" checked />No
</div>
</div>
<div class="clr"></div>
<input type="submit" name="submit" class="fauxButton" />
</form>';
}
?>
If you are using sessions, you can store the variable, results, array -- whatever into a session variable and then retrieve it on a new page.
session_start();
$_SESSION['test_var'] = 'Jake';
Then when I navigate to a new page and retrieve the var:
session_start();
echo $_SESSION['test_var']
// outputs 'Jake'

Categories