So I am trying to use the intel I got on the first $_POST on the second $_POST but when the second occurs the intel from the first is lost, what can I do to overcome this?
<?php
if(isset($_POST['entrar'])) {
$nr_processo = $_POST['nr_processo'];
echo "$nr_processo";
$aluno = mysql_query("SELECT * FROM alunos WHERE nr_processo = '$nr_processo'");
$row = mysql_fetch_array($aluno) or die ("Este numero de processo não está registado " . mysql_error()) ;
$aluno_nome = $row['nome'];
$aluno_ano = $row['ano'];
$aluno_turma = $row['turma'];
$aluno_ciclo = $row['ciclo'];
echo $aluno_nome, $aluno_ano, $aluno_turma, $aluno_ciclo;
}
?>
<form method="post" action="">
<label> A que se deve a tua visita ah biblitoeca?</label><br>
<label> Estudo/pesquisa</label>
<input name="estudo" value="1" type="checkbox">
<br>
<label> Leitura Periódica</label>
<input name="leitura" value="1" type="checkbox">
<br>
<label>Internet</label>
<input name="net" value="1" type="checkbox">
<br>
<label> Audiovisuais</label>
<input name="audiovisuais" value="1" type="checkbox"> <br>
<input name="enviar" value="Enviar" type="submit">
</form>
<?php
if(isset($_POST['enviar'])) {
$estudo = $_POST['estudo'];
$leitura = $_POST['leitura'];
$internet = $_POST['net'];
$audiovisuais = $_POST['audiovisuais'];
echo $aluno_nome;
$data = date('Y-m-d ');
$hora = date('H:i:s');
echo $data, $hora;
mysql_query("INSERT INTO entradas VALUES('', '$nr_processo', '$aluno_ano', '$aluno_turma', '$estudo', '$leitura', '$internet', '$audiovisuais', '$data', '$hora' )") ;
?>
<!-- <META http-equiv="refresh" content="0;URL=http://localhost/registosbib/agradecimento.php"> -->
<?php
}
?>
You post entrar and enviar in the same request or 2 different requests ?
Edit:
The 2 $_POST doesn't conflict
Fix:
Use $_SESSION instead
session_start();
$_SESSION['row'] = $row;
Then in the second $_POST use $_SESSION['row']['nome'] instead of $aluno_nome and other $aluno_* variables
Related
I am trying to give the values 0 or 1 to the fields of a database depending on whether the checkbox is checked or not,
but i can't find it and i can't find where is my problem, can you help me?
<?php
//connexion à la base de donnée
include_once "../connexion.php";
//on récupère le id dans le lien
$id = $_GET['id'];
//requête pour afficher les infos d'un employé
$req = mysqli_query($con , "SELECT * FROM lecteur_badge_statique WHERE id = $id");
$row = mysqli_fetch_assoc($req);
//vérifier que le bouton modifier a bien été cliqué
if(isset($_POST['button'])){
//extraction des informations envoyé dans des variables par la methode POST
extract($_POST);
//verifier que tous les champs ont été remplis
if(isset($lb1) && $lb2){
//requête de modification
$req = mysqli_query($con, "UPDATE lecteur_badge_statique SET lb1 = '$lb1' , lb2 = '$lb2' , lb3 = '$lb3, lb4 = '$lb4, lb5 = '$lb5, lb6 = '$lb6, lb7 = '$lb7, lb8 = '$lb8, lb9 = '$lb9, lb10= '$lb10'
lb11 = '$lb11' , lb12 = '$lb12' , lb13 = '$lb13, lb14 = '$lb14, lb15 = '$lb15, lb16 = '$lb16, lb17 = '$lb17, lb18 = '$lb18, lb19 = '$lb19, lb20= '$lb20'
lb21 = '$lb21' , lb22 = '$lb22' , lb23 = '$lb23, lb24 = '$lb24, lb25 = '$lb25, lb26 = '$lb26, lb27 = '$lb27, lb28 = '$lb28, lb29 = '$lb29, lb30= '$lb30'
lb31 = '$lb31' , lb32 = '$lb32' , lb33 = '$lb33, lb34 = '$lb34, lb35 = '$lb35, lb36 = '$lb36, lb37 = '$lb37, lb38 = '$lb38, lb39 = '$lb39, lb40= '$lb40'
lb41 = '$lb41' , lb42 = '$lb42' , lb43 = '$lb43, lb44 = '$lb44, lb45 = '$lb45, lb46 = '$lb46, lb47 = '$lb47, lb48 = '$lb48, lb49 = '$lb49, lb50= '$lb50'
WHERE id = $id");
$error_message = mysqli_error($con);
if($error_message == ""){
echo "No error related to SQL query.";
}else{
echo "Query Failed: ".$error_message;
}
if($req){//si la requête a été effectuée avec succès , on fait une redirection
header("location: index.php");
}else {//si non
$message = "Employé non modifié";
}
}else {
//si non
$message = "Veuillez remplir tous les champs !";
}
}
?>
<div class="form">
<img src="images/back.png"> Retour
<h2>Modifier l'employé : <?=$row['nom']?> </h2>
<p class="erreur_message">
<?php
if(isset($message)){
echo $message ;
}
?>
</p>
<form action="" method="POST">
<label>lb1</label>
<input type="checkbox" name="lb1" value="<?=$row['lb1']?>">
<label>lb2</label>
<input type="checkbox" name="lb2" value="<?=$row['lb2']?>">
<label>lb3</label>
<input type="checkbox" name="lb3" value="<?=$row['lb3']?>">
<label>lb4</label>
<input type="checkbox" name="lb4" value="<?=$row['lb4']?>">
<label>lb5</label>
<input type="checkbox" name="lb5" value="<?=$row['lb5']?>">
<label>lb6</label>
<input type="checkbox" name="lb6" value="<?=$row['lb6']?>">
<label>lb7</label>
<input type="checkbox" name="lb7" value="<?=$row['lb7']?>">
<label>lb8</label>
<input type="checkbox" name="lb8" value="<?=$row['lb8']?>">
<label>lb9</label>
<input type="checkbox" name="lb9" value="<?=$row['lb9']?>">
<label>lb10</label>
<input type="checkbox" name="lb10" value="<?=$row['lb10']?>">
<label>lb11</label>
<input type="checkbox" name="lb11" value="<?=$row['lb11']?>">
<label>lb12</label>
<input type="checkbox" name="lb12" value="<?=$row['lb12']?>">
<label>lb13</label>
<input type="checkbox" name="lb13" value="<?=$row['lb13']?>">
<label>lb14</label>
<input type="checkbox" name="lb14" value="<?=$row['lb14']?>">
<label>lb15</label>
<input type="checkbox" name="lb15" value="<?=$row['lb15']?>">
<label>lb16</label>
<input type="checkbox" name="lb16" value="<?=$row['lb16']?>">
<label>lb17</label>
<input type="checkbox" name="lb17" value="<?=$row['lb17']?>">
<label>lb18</label>
<input type="checkbox" name="lb18" value="<?=$row['lb18']?>">
<label>lb19</label>
<input type="checkbox" name="lb19" value="<?=$row['lb19']?>">
<label>lb20</label>
<input type="checkbox" name="lb20" value="<?=$row['lb20']?>">
<label>lb21</label>
<input type="checkbox" name="lb21" value="<?=$row['lb21']?>">
<label>lb22</label>
<input type="checkbox" name="lb22" value="<?=$row['lb22']?>">
<label>lb23</label>
<input type="checkbox" name="lb23" value="<?=$row['lb23']?>">
<label>lb24</label>
<input type="checkbox" name="lb24" value="<?=$row['lb24']?>">
<label>lb25</label>
<input type="checkbox" name="lb25" value="<?=$row['lb25']?>">
<label>lb26</label>
<input type="checkbox" name="lb26" value="<?=$row['lb26']?>">
<label>lb27</label>
<input type="checkbox" name="lb27" value="<?=$row['lb27']?>">
<label>lb28</label>
<input type="checkbox" name="lb28" value="<?=$row['lb28']?>">
<label>lb29</label>
<input type="checkbox" name="lb29" value="<?=$row['lb29']?>">
<label>lb30</label>
<input type="checkbox" name="lb30" value="<?=$row['lb30']?>">
<label>lb31</label>
<input type="checkbox" name="lb31" value="<?=$row['lb31']?>">
<label>lb32</label>
<input type="checkbox" name="lb32" value="<?=$row['lb32']?>">
<label>lb33</label>
<input type="checkbox" name="lb33" value="<?=$row['lb33']?>">
<label>lb34</label>
<input type="checkbox" name="lb34" value="<?=$row['lb34']?>">
<label>lb35</label>
<input type="checkbox" name="lb35" value="<?=$row['lb35']?>">
<label>lb36</label>
<input type="checkbox" name="lb36" value="<?=$row['lb36']?>">
<label>lb37</label>
<input type="checkbox" name="lb37" value="<?=$row['lb37']?>">
<label>lb38</label>
<input type="checkbox" name="lb38" value="<?=$row['lb38']?>">
<label>lb39</label>
<input type="checkbox" name="lb39" value="<?=$row['lb39']?>">
<label>lb40</label>
<input type="checkbox" name="lb40" value="<?=$row['lb40']?>">
<label>lb41</label>
<input type="checkbox" name="lb41" value="<?=$row['lb41']?>">
<label>lb42</label>
<input type="checkbox" name="lb42" value="<?=$row['lb42']?>">
<label>lb43</label>
<input type="checkbox" name="lb43" value="<?=$row['lb43']?>">
<label>lb44</label>
<input type="checkbox" name="lb44" value="<?=$row['lb44']?>">
<label>lb45</label>
<input type="checkbox" name="lb45" value="<?=$row['lb45']?>">
<label>lb46</label>
<input type="checkbox" name="lb46" value="<?=$row['lb46']?>">
<label>lb47</label>
<input type="checkbox" name="lb47" value="<?=$row['lb47']?>">
<label>lb48</label>
<input type="checkbox" name="lb48" value="<?=$row['lb48']?>">
<label>lb49</label>
<input type="checkbox" name="lb49" value="<?=$row['lb49']?>">
<label>lb50</label>
<input type="checkbox" name="lb50" value="<?=$row['lb50']?>">
<input type="submit" value="Modifier" name="button">
</form>
</div>
I would like that when the page loads, the database fields that contain the value 1 are checked and then we can check/uncheck the boxes that we want to update to 1 or 0 in the DB.
But already when loading the page the boxes are not checked when they are 1 in the DB.
value="<?=$row['']?> with checkbox doesn't work the same as input type text?
Thanks in advance for your help :)
If a checkbox is checked it will have the checked attribute:
<input type="checkbox" name="agree" value="1" checked />
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/checkbox
You can set your checkbox as checked by checking if the value is set in PHP like so:
<input type="checkbox" name="agree" value="1" <?= $row['xyz'] ? 'checked' : null ?> />
As an aside, you should take note of what Dharman said in the comments regarding MySQL injection, I would recommend using PDO and prepared statements, have a quick read through this, it is very helpful https://phpdelusions.net/pdo
Question.php
<?php
include 'Pre-function.php'
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>
<div class="nav">
Home
News
Contact
</div>
<div class="question">
<div class="A4">
<form action="Answer.php" method="post">
<?php getQuestion($conn); ?>
<input type="submit" name="Submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
Its html page to ask question
Pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label for='.$question_body.'>Yes</label>
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="'.$question_id.'" value="No">
<input type="hidden" name="option_b" value="'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
}
}
?>
Basically this form asked question whether yes or no using radio button. $option_a == 'Yes' and $option_b == 'No'. The question is like this "Are you have fever ?". So when i submit the value did not pass to the 'Answer.php' page.
'Answer.php' page.
<?php
include 'conn.php';
if(isset($_POST['Submit']) && !empty($_POST['Submit'])){
echo $_POST['option_a'];
echo 'succeed';
}
else{
echo 'no data';
}
?>
In this page have error undefined_index value but still echo succeed.
Your HTML code should look like the following:
<input type="radio" name="whatevername" value="Yes">
<input type="hidden" name="whatevername" value="No">
You can use PHP to insert whatever values you want but you need the radio button name to be the same.
Then if you want to echo that in PHP you'd use:
echo $_POST['whatevername']; //same name you used in the form
You are passing value as name for radio buttons
name="'.$option_a.'"
This will result you in name ="Yes"
And you are trying to fetch echo $_POST['option_a']; where option_a is not defined.
Try this
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
Same for other radio button
Try this one :
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
echo '<div class="A4">';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<div class="A4">
<h2 class="qtitle">'.$question_body.'</h2>
<form action="Answer.php" method="post">
<label for='.$question_body.'>Yes</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
echo'
<input type="submit" name="Submit" value="Submit">
</form>
</div>';
}
}
?>
<?php
include 'conn.php';
if(isset($_POST['submitted']) && !empty($_POST['submitted'])){
$questionAndOptions = $_POST['radioQuestions'];
foreach ($questionAndOptions as $questionAndOption) {
$arrQuestionAndOption = explode("-", $questionAndOption);
echo $arrQuestionAndOption[0]; //question
echo $arrQuestionAndOption[1]; //option
}
echo 'succeed';
}
else{
echo 'no data';
}
?>
I would like to update a sql table with a html form. I would like to select the element with a drop-down list and later update the values with the form.
Here is my code, the drop-down list and the form works, but I didn't know how to make that the php code get the element that I select.
HTML form:
<?php
require("conectarBD.php");
$select = "SELECT id_serie, nombre FROM series";
$result = $conectar->query($select);
?>
Selecciona la serie que quieres modificar:
<br>
<select>
<?php
while ( $row = $result->fetch_array() )
{
?>
<option value=" <?php echo $row['id_serie'] ?> " >
<?php echo $row['nombre']; ?>
</option>
<?php
}
?>
</select>
<form action="modificar_serie.php" method="post">
<p>
Introduce los cambios a realizar:
</p>
<p>
<label for="textfield">Nombre</label>
<input type="text" name="nom" id="nom" />
<label for="textarea"></label>
</p>
<p>
<label for="textfield">Temporadas</label>
<input type="number" name="temp" id="temp" />
<label for="textarea"></label>
</p>
<p>
<label for="textfield"> Año de estreno</label>
<input type="text" name="est" id="est" />
<label for="textarea"></label>
</p>
<input type="Submit" value="Actualizar">
</form>
PHP:
<?php
require("conectarBD.php");
$nombre = $_POST["nombre"];
$temp = $_POST["temp"];
$est = $_POST["est"];
$query="UPDATE series SET nombre = '.$nombre.', temporadas = '.$temp.', estreno= '.$est.' WHERE nombre='$nombre'";
mysqli_query($conectar,$query);
if(mysqli_affected_rows()>=0){
echo "<p>($nombre) Datos Actualizados<p>";
}else{
echo "<p>($nombre) No se ha podido actualizar en estos momentos<p>";
}
header("Location: ../index.php");
?>
Your select element must be inside form and have a name. Then you will be able to get a value of it from $_POST.
So, I'm getting an id_cliente from another php and I get it correctly from that id. I want to update my database but I can't find a way to do it. I've tried UPDATE, a friend of mine checked the code for syntax error, but I still want to see if any of use were wrong.
Here's my body:
<body>
<div class="maindiv">
<div class="form_div">
<div class="title">
<h2>Insertando datos a la tabla de Cliente.</h2>
</div>
<?php
$id_cliente = $_POST['id_cliente'];
?>
<form action="actualizar.php" method="post">
<?php
$query= "SELECT * FROM cliente WHERE $id_cliente = id_cliente";
include "../conexion/conexion.php";
$sql = mysqli_query($conn, $query);
if(empty($sql)) echo "No se encontró ningún personal que coincida con la búsqueda";
else{
while($row = mysqli_fetch_object($sql)){ ?>
<h2>Llena todos los campos.</h2>
<label>Clave:</label>
<input class="input" name="clave" type="number" value="<?php echo $row->CLAVE ?>">
<label>Nombre:</label>
<input class="input" name="nombre" type="text" value="<?php echo $row->NOMBRE ?>">
<label>Apellido Paterno:</label>
<input class="input" name="apellido_p" type="text" value="<?php echo $row->APELLIDO_P ?>">
<label>Apellido Materno:</label>
<input class="input" name="apellido_m" type="text" value="<?php echo $row->APELLIDO_M ?>">
<label>Direccion:</label>
<textarea cols="25" name="direccion" rows="5"><?php echo $row->DIRECCION ?></textarea><br>
<label>Telefono:</label>
<input class="input" name="telefono" type="text" value="<?php echo $row->TELEFONO ?>">
<label>Correo:</label>
<input class="input" name="correo" type="text" value="<?php echo $row->CORREO ?>">
<label>Fecha de Nacimiento (AA/MM/DD):</label>
<input class="input" name="fecha" type="date" value="<?php echo $row->NACIMIENTO ?>">
<label>Saldo:</label>
<input class="input" name="saldo" type="number" value="<?php echo $row->SALDO ?>">
<?php } $conn->close(); } ?>
<?php
$connection = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($connection,"cajadeahorros");
error_reporting(0);
if(isset($_POST['submit']))
{
if($_POST['id_cliente'] == "") $_POST['id_cliente'] = "NULL";
$clave = $_POST['clave'];
$nombre = $_POST['nombre'];
$apellido_p = $_POST['apellido_p'];
$apellido_m = $_POST['apellido_m'];
$direccion = $_POST['direccion'];
$telefono = $_POST['telefono'];
$correo = $_POST['correo'];
$fecha = $_POST['fecha'];
$saldo = $_POST['saldo'];
$q="select count(1) from cliente where clave='$clave'";
$r=mysqli_query($connection,$q);
$row=mysqli_fetch_row($r);
if($row[0]>=1)
{
$x = 1;
}
else
{
$x = 0;
}
if($clave !=''&&$nombre !=''&&$apellido_p !=''&&$apellido_m !=''&&$direccion !=''&&$telefono !=''&&$correo !=''&&$fecha !=''&&$saldo !=''&&$x==0)
{
$query = mysqli_query($connection, "update cliente set clave = 'a', nombre = 'a', apellido_p='a', apellido_m='a', direccion ='a', correo='a', nacimiento='a', saldo='a' where id_cliente = '$id_cliente'");
echo "<br/><br/><span>Datos ingresados correctamente.</span>";
}
else
{
echo "<p>No se pudo insertar. <br/> Algunos campos estan vacios o la clave ya existe.</p>";
}
}
mysqli_close($connection);
?>
<input class="submit" name="submit" type="submit" value="Insertar">
</form>
</div>
</div>
</body>
Please change the top select query which you have given like
$query= "SELECT * FROM cliente WHERE $id_cliente = id_cliente";
to
$query= "SELECT * FROM cliente WHERE id_cliente = $id_cliente";
I get a var called $cobro in this module php, and i need this variable for calculate a subtraction of a number insert in a input called $vuelto. When i send the form the var $cobro is eliminated and i cant execute the subtraction.
Help, and thx !
<?php
// Disponible desde PHP 4.1.0
date_default_timezone_set("America/Santiago");
if(isset($_POST['fecha'])){
$fecha = date("Y-m-d G:i:s",strtotime($_POST['fecha']));
}
$date1 = strtotime($fecha);
$date2 = time();
$subTime = $date2-$date1;
$y = ($subTime/(60*60*24*365));
$d = ($subTime/(60*60*24))%365;
$h = ($subTime/(60*60))%24;
$m = ($subTime/60)%60;
echo "Diferencia entre ".$fecha." y ".date('Y-m-d H:i:s',$date2)." es:<br/>";
//echo $y." annos<br/>";
echo $d." dias<br/>";
echo $h." horas<br/>";
echo $m." minutos<br/>";
$cobro =0;
$fecha_em= date('Y-m-d H:i:s',$date2);
$h = $h + $m/60 +$d*24;
$cobro = $h*600;
if($cobro<0)
$cobro = $cobro*-1;
if($h < 1)
echo "Debe pagar el minimo: $600";
else
echo "Debe pagar: $".$cobro;
$conexion = mysql_connect("localhost","grupo2","face2014");
mysql_select_db("sisace",$conexion);
/*if ($conexion==0)
echo "Lo sentimos, no se ha podido conectar con la MySQL";
else {
echo "Se logró conectar con MySQL";
echo "<br>";}
*/
$sql="INSERT INTO boleta(fecha_emision,fecha_ingreso,num_boleta,valor_total) ".
"VALUES('$fecha_em','$fecha','1','$cobro')";
mysql_query($sql);
if(isset($_POST['submit'])) {
$vuelto = $_POST['vuelto'];
$vuelto = $vuelto -$cobro;
echo "Su vuelto es el siguiente:";
echo $vuelto;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="number" name="vuelto"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
You didn't put it in Form use input hidden to post it
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="number" name="vuelto"><br>
<input type="hidden" name="cobro" value="<?php echo $cobro; ?>">
<input type="submit" name="submit" value="Submit Form"><br>
</form>
That's how you can pass cobro variable value which will be available in $_POST['cobro']