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
Related
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.
I'm having a big problem.
I have this code and I want to print that values in other page.
FORM
<p>
<input type="hidden" value="" name="reconhecimento_sabado" checked>
<input type="checkbox" value="Sábado, 4 de Março de 2017<br>" name="reconhecimento[]" onclick="chkChecks('reconhecimento[]')"> Sábado, 4 de Março de 2017
</p>
<p>
<input type="hidden" value="" name="reconhecimento_domingo" checked>
<input type="checkbox" value="Domingo, 5 de Março de 2017<br>" name="reconhecimento[]" onclick="chkChecks('reconhecimento[]')"> Domingo, 5 de Março de 2017
</p>
<p>
<input type="hidden" value="" name="reconhecimento_quinta" checked>
<input type="checkbox" value="Quinta-feira, 9 de Março de 2017<br>" name="reconhecimento[]" onclick="chkChecks('reconhecimento[]')"> Quinta-feira, 9 de Março de 2017
</p>
<p>
<input type="hidden" value="" name="reconhecimento_sexta" checked>
<input type="checkbox" value="Sexta-feira, 10 de Março de 2017<br>" name="reconhecimento[]" onclick="chkChecks('reconhecimento[]')"> Sexta-feira, 10 de Março de 2017
</p>
And here I have the code from the other page, like this I can print my values form the previously page.
$reconhecimento = $_POST['reconhecimentos'];
foreach ($reconhecimento as $reconhecimento=>$value) {
echo $value;
}
$html="TESTE de codigo FIM DO TESTE";
echo $html;
But like this I can't, help me please.
$reconhecimento = $_POST['reconhecimentos'];
$html="TESTE de codigo ".foreach ($reconhecimento as $reconhecimento=>$value) { echo $value; }." FIM DO TESTE";
echo $html;
Ensure you aren't self-referencing the array you are looping over:
foreach ($reconhecimento as $r=>$value) { echo $value; }
Also, Don't assign your foreach inside a string. Do this:
echo "TESTE de codigo: ";
foreach ($reconhecimento as $r = >$value) {
echo $value;
}
echo $html;
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
My goal:
To change my MySql table that looks like this:
role_permission:
role_id permissions_id
2 1
1 3
1 4
1 5
1 2
Based upon the HTML Code of this:
<div class="element">
<label for="permissions">Permissions:</label>
<input type="hidden" name="permissions[1]" value="false" checked="">
<label>
<input type="checkbox" name="permissions[1]" value="true" checked=""> canPushAPK</label>
<input type="hidden" name="permissions[2]" value="false">
<label>
<input type="checkbox" name="permissions[2]" value="true"> canBeCool</label>
<input type="hidden" name="permissions[3]" value="false" checked="">
<label>
<input type="checkbox" name="permissions[3]" value="true" checked=""> canEditSettings</label>
<input type="hidden" name="permissions[4]" value="false" checked="">
<label>
<input type="checkbox" name="permissions[4]" value="true" checked=""> canManageRoles</label>
<input type="hidden" name="permissions[5]" value="false" checked="">
<label>
<input type="checkbox" name="permissions[5]" value="true" checked=""> canAddUser</label>
<input type="hidden" name="permissions[6]" value="false">
<label>
<input type="checkbox" name="permissions[6]" value="true"> canFoo</label>
<input type="hidden" name="permissions[7]" value="false">
<label>
<input type="checkbox" name="permissions[7]" value="true"> canTalk</label>
<input type="hidden" name="permissions[8]" value="false">
<label>
<input type="checkbox" name="permissions[8]" value="true"> canTest</label>
<input type="hidden" name="permissions[9]" value="false">
<label>
<input type="checkbox" name="permissions[9]" value="true"> canPoo</label>
</div>
<input type="hidden" name="roleId" value="1">
View as JSFiddle.
Then based off what values are checked for "roleId" = 1, it inserts into the DB. for all unchecked, it deletes this row.
IE:
If all are checked, it will look like:
role_permission:
role_id permissions_id
2 1
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
What am I thinking:
Two queries:
DELETE FROM role_permissions
WHERE role_id = ?
Then:
$query = $this->mysqli->prepare("INSERT INTO role_permission(role_id,permission_id) VALUES (?,?)");
$query->bind_param("ii", $roleId, $permissionId);
$roleId = $_POST['roleId'];
foreach ($_POST['permissions'] as $permissionId=> $value){
if ($value){
$query->execute();
}
}
Your approach will work. you seem to like HTML.
If you use an array of 'permission names' and whether acive or not, then the code is just foreach loops as follows:
Tested code: PHP 5.3.18, mysqli on windows xp (Oi, stop laffin')
<?php // Q22950444
// program smarter not harder...
// arrays are your friend...
$allPermissions =
array('canPushAPK' => false, "canBeCool" => false, "canEditSettings" => false,
"canManageRoles" => false, "canAddUser" => false, "canFoo" => false,
"canTalk" => false, "canTest" => false, "canPoo" => false);
$mysqlhost = 'localhost';
$mysqluser = 'test';
$mysqlpass = 'test';
$myDBname = 'testmysql';
$mysqli = new mysqli($mysqlhost, $mysqluser, $mysqlpass, $myDBname);
if (!empty($_POST['goForIt'])) {
if (!empty($_POST['permissions'])) {
foreach($_POST['permissions'] as $permissionName) {
if (isset($allPermissions[$permissionName])) { // test is valid permission
$allPermissions[$permissionName] = true;
}
}
}
// process db...
$sql = "delete from `role_permissions` where `role_id` = ?";
$deleteQuery = $mysqli->prepare($sql);
if ($deleteQuery === false) { // drat
die('deleteQuery: '. $mysqli->error);
}
$deleteQuery->bind_param('s', $_POST['roleId']);
$allOk = $deleteQuery->execute();
if (!$allOk) { // drat
die('deleteQuery: '. $deleteQuery->error);
}
$sql = "insert into role_permissions (role_id, permission_id) values (?, ?)";
$insertQuery = $mysqli->prepare($sql);
if ($insertQuery === false) { // drat
die('insertQuery: '. $mysqli->error);
}
foreach($allPermissions as $permissionName => $active) {
if ($active) {
// bind the variables to the ALREADY PREPARED query
$insertQuery->bind_param('is', $_POST['roleId'], $permissionName);
$allOk = $insertQuery->execute();
if (!$allOk) { // drat
die("insertQuery: $permissionName : {$insertQuery->error}");
}
}
}
echo "<br />role: {$_POST['roleId']}: now has:<br />";
foreach($allPermissions as $permissionName => $active) {
echo $permissionName, ' : ', $active ? '<strong>Yippee!</strong>' : 'sadly, no', '<br />';
}
}
?>
<form action="" method="post">
<div class="element"><!-- easy peasy way of sending out the current permissions -->
<label for="permissions">Permissions:</label>
<?php foreach(array_keys($allPermissions) as $permissionName): ?>
<div>
<label>
<input type="checkbox" name="permissions[]"
value="<?= $permissionName ?>"
<?= $allPermissions[$permissionName] ? 'checked="checked"' : ''; ?>
> <?= $permissionName; ?></label>
</div>
<?php endforeach; ?>
</div>
<input type="hidden" name="roleId" value="1">
<input type="submit" name="goForIt" value="Go For It!">
</form>
I have a comment BOX, which has 5 fields, one is name, email, RATE, comment, articleid.
Rate field is a radio type, which has 5 radio buttons with value 1,2,3,4,5. IF someone click on rate my product and it should save the rated value in databse. I'm using RATE as INT in database, It stores 0 in it, If i use RATE as TEXT in database, it stores "on" in database. It is not storing rating values like 1,2,3,4,5.
My form Code
<form action="manage_comments.php" method="post">
<span class="rating">
<input type="radio" class="rating-input" id="rate" name="rate" value="1">
<label for="rating-input-1-5" class="rating-star"></label>
<input type="radio" class="rating-input" id="rate" name="rate" value="2">
<label for="rating-input-1-4" class="rating-star"></label>
<input type="radio" class="rating-input" id="rate" name="rate" value="3">
<label for="rating-input-1-3" class="rating-star"></label>
<input type="radio" class="rating-input" id="rate" name="rate" value="4">
<label for="rating-input-1-2" class="rating-star"></label>
<input type="radio" class="rating-input" id="rate" name="rate" value="5">
<label for="rating-input-1-1" class="rating-star"></label>
</span>
<input type='hidden' name='articleid' id='articleid' value='<?php echo $_GET["id"]; ?>' />
<input type="submit" name="submit" value="Publish Now"></p>
</form>
My php Code
<?php
if( $_POST )
{
$con = mysql_connect("localhost","asfi","asfi");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("aw-tech", $con);
$post_rate = (isset($_POST['rate'])) ? $_POST['rate'] : '';
$articleid = (int)isset($_GET['id']);
if(!is_numeric($articleid))
die('invalid article id');
$sql="INSERT INTO `aw-tech`.`comment` (cid, name, email, website, comment, timestamp, rate, articleid) VALUES (NULL, '$_POST[name]', '$_POST[email]', '$_POST[website]', '$_POST[comment]', CURRENT_TIMESTAMP, '$post_rate', ".$articleid.")";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Comment Saved";
mysql_close($con);
}
?>
Secondly My articleid saves always 0.. my page id is .php?id=49 , it is 49 but if i made comment on that page, It saves my article ID always 0.
articleid & rate Both are INT in database, I have used them as TEXT too in database but didn't work
I think I found your problem. Change form declaration to:
<form action="manage_comments.php" method="post">
Remove row:
<input type='hidden' name='articleid' id='articleid' value='<?php echo $_GET["id"]; ?>' />
the Then PHP code from:
$post_rate = (isset($_POST['rate'])) ? $_POST['rate'] : '';
$articleid = (int)isset($_GET['id']);
to:
$post_rate = (isset($_POST['rate'])) ? $_POST['rate'] : 0;
$articleid = (int)isset($_GET['id']) ? $_GET['id'] : 0;
And for debug add this after the $sql variable and insert into a comment here:
print_r($_GET);
print_r($_POST);
echo $sql;
Hope it works.
when you post the form in action you put : manage_comments.php
so the "id" be lost!
in action you can put : manage_comments.php?id=<?php=$_GET[id]?>
or
after submit form
use $_POST[articleid] not $_GET['id']
i think it supposes to be
$post_rate = (isset($_POST['rate'])) ? $_POST['rate'] : '';
$articleid = (int)isset($_POST['articleid']);