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.
Related
I'm trying to use checkbox to look into my DB but i don't know why, the answer is made of all the data of the table and not the specific entry I want!
Code
$req_lieux = $sqlQuery->query("select lieux.nom from lieux, type_lieu WHERE lieux.id_type_lieu = type_lieu.ID");
PHP code:
<fieldset>
<legend class="title-color-bloc">Couleur</legend>
<?php
require_once 'search.php';
/* la requête est dans search.php */
while ($couleurs = $query_check_color->fetch())
{?>
<input class="color-choice" type="checkbox" id="color-choice<?php echo $couleurs['ID']; ?>" name="color-choice[]" value="<?php echo $couleurs['ID']; ?>">
<label for="color-choice<?php echo $couleurs['ID']; ?>"><?php echo $couleurs['couleur']; ?></label>
<?php
}
$query_check_color->closeCursor();
?>
</fieldset>
<fieldset>
<legend class="title-color-bloc">Lieux</legend>
<?php
/* la requête est dans search.php */
while ($types = $query_check_types->fetch())
{?>
<input class="type-choice" type="checkbox" id="type-choice<?php echo $types['ID']; ?>" name="type-choice[]" value="<?php echo $types['ID']; ?>">
<label for="type-choice<?php echo $types['ID']; ?>"><?php echo $types['libelle']; ?></label>
<?php
}
$query_check_types->closeCursor();
?>
</fieldset>
<input type="submit" value="Afficher" class="submit-btn" name="submit-btn">
</form>
<?php
if(isset($_POST))
{
if(!empty($_POST['type-choice']))
{
$type = implode("," , $_POST['type-choice']);
while ($type = $req_lieux->fetch())
{ ?>
<h1><?php echo $type['nom'] ?></h1>
<?php }
}
}
?>
Thanks for your help!
You're not restricting the results of your query to any specific type.
select lieux.nom from lieux, type_lieu WHERE lieux.id_type_lieu = type_lieu.ID
This query will select every record from lieux where a record exists in type_lieu.
So if you have a specific type_lieu you're trying to use then you do something like the following:
SELECT l.nom from lieux AS l INNER JOIN type_lieu AS tl ON l.id_type_lieu = tl.ID WHERE tl.type = xxxx
I have this code and i'm doing a dropdown to people choose what notice they want to delete, but i don't know what is wrong in my code......i'm new..so sorry for my ignorance
PHP code:
<?php
include('configdb.php')
if(isset($_POST['delete']))
{
$query_delete = "DELETE from artigos where idartigos >0";
$resultado_delete = mysqli_query($mysqli,$query_delete) or
die(mysqli_error($mysqli));
if($resultado_delete)
{
echo "
<script language='JavaScript'>
window.alert('Notícia eliminada com sucesso. Clique para voltar à página inicial.')
window.location.href='index.php';
</script>";
}
else
{
echo "
<script language='JavaScript'>
window.alert('Não foi possível apagar a sua notícia. Tente novamente, sff. Clique para voltar à página inicial.')
window.location.href='index.php';
</script>";
}
}
?>
HTML CODE:
<h1>Apagar notícia</h1>
<a>Notícias disponíveis</a><br><br>
<select name="delete" style="width:332px">
<?php echo $artigos; ?>
</select><br><br>
<p>eliminar
<input type="submit" name="submit" id="submit" action= "delete.php" value="delete" />
</p>
This is how it looks:
<form action="delete.php">
<select name="delete">
<option value=" ">blank</option>
<option value=" <?php echo $artigos; ?> "> <?php echo $artigos; ?> </option>
</select>
<input type="submit" value="Submit">
</form>
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> ";
}
?>
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
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";