Foros aprenderaprogramar.com
Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: snake07n en 10 de Enero 2019, 19:50
-
Hola a todos...no logro modificar los campos de mi base de datos con la sentencia SQL UPDATE, me da error... Posibles causas?
He adjuntado 2 paginas creadas para que veáis el procedimiento que he seguido... si alguien puede identificar el error, házmelo saber...
Estoy en fase de aprendizaje y aun no logro tener la suficiente experiencia para identificar el problema...
Mil gracias por vuestro tiempo..
Pagina 1
<?php
$conexion= mysqli_connect("localhost: 3306", "root", "", "taxicorp")or die("Error en la conexion con la BD");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Modificar datos</title>
<style>
@font-face{
font-family: 'RobotoMono';
src: url(Tipografias/RobotoMono.ttf);
font-style: normal;
}
table{
width: 765px; height: 20px;
border: ridge thick aliceblue;
background-color: antiquewhite;
table-layout: inherit;
}
#estatic{
position: absolute;
left: 185px; top: 85px;
}
</style>
</head>
<body bgcolor="#000000">
<div id="estatic">
<form action="mod_dat_tax2.php" method="post">
<table border="1">
<tr>
<td><b>*</b></td>
<td><b>Matricula</b></td>
<td><b>Modelo</b></td>
<td><b>Conductor</b></td>
</tr>
<?php
$sel= "SELECT * FROM taxis";
$exec= mysqli_query($conexion, $sel);
while($datos= mysqli_fetch_row($exec)){
?>
<tr>
<td><input type="radio" name="taxi_radio" value="<?php echo($datos[0]);?>"></td>
<td><?php echo($datos[0]);?></td>
<td><?php echo($datos[1]);?></td>
<td><?php echo($datos[2]." ".$datos[3]);?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center">
<input type="submit" name="botonEnviar" value="Modificar Datos"></td>
</tr>
</table>
</form>
<a href="contenido.php" style="float: right; font: 14px bold RobotoMono; color: aliceblue">Volver</a>
</div>
</body>
</html>
Pagina 2
<?php
$conexion= mysqli_connect("localhost: 3306", "root", "", "taxicorp")or die("Uppsss!!... ALgo ha ido mal...");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Modificar datos</title>
</head>
<style>
@font-face{
font-family: 'RobotoMono';
src: url(Tipografias/RobotoMono.ttf);
font-style: normal;
}
table{
width: 765px; height: 20px;
border: ridge thick aliceblue;
background-color: antiquewhite;
table-layout: inherit;
}
#estatic{
position: absolute;
left: 185px; top: 85px;
}
h3{
color: aliceblue;
}
</style>
<body bgcolor="#000000">
<?php
if(!isset($_POST['boton_enviar'])){
$conexion= mysqli_connect("localhost: 3306", "root", "", "taxicorp")or die("Uppsss!!... ALgo ha ido mal...");
$sql="SELECT * FROM taxis WHERE Matricula='".$_POST['taxi_radio']."'";
$exec= mysqli_query($conexion, $sql);
$registro= mysqli_fetch_row($exec);
?>
<div id="estatic">
<form action="mod_dat_tax2.php" method="post">
<table border="1">
<tr>
<td colspan="2" align="center"><b>Matricula: <font color="#BB0B0E">
<?php echo($_POST['taxi_radio']);?></font></b></td>
</tr>
<tr>
<td><b>Modelo:</b></td>
<td><input type="text" name="Modelo" size="40" value="<?php echo($registro[1]);?>"></td>
</tr>
<tr>
<td><b>Nombre y Apellidos:</b></td>
<td>
<input type="text" name="Nombre" value="<?php echo($registro[2]);?>">
<br>
<input type="text" name="Apellidos" size="35" value="<?php echo($registro[3]);?>"></td>
</tr>
<tr>
<td><b>Libre:</b></td>
<td><input type="checkbox" name="libre" <?php if($registro[4]) echo("checked");?>></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="boton_enviar" value="Modificar Datos"></td>
</tr>
</table>
</form>
<a href="mod_dat_tax.php" style="float: right; font: 14px bold RobotoMono; color: aliceblue">Volver</a>
</div>
<?php
}else{
$update ="UPDATE taxis SET Modelo='".$_POST['Modelo']."',"."Nombre='".$_POST['Nombre']."',";
$update.="Apellidos='".$_POST['Apellidos']."',Ocupado=";
if (isset($_POST['libre']))
$update.="true";
else
$update.="false";
$update.=" WHERE Matricula='";
$update.=$_POST["Modelo"].$_POST["Nombre"].$_POST["Apellidos"]."'";
// Sentencia SQL
$exec= mysqli_query($conexion, $update);
echo("<br>");
// Registro en la tabla taxis
if(!$exec)
echo("<h3><center>Datos Modificados!!</center></h3>");
else
echo("<h3><center>Error!!</center></h3>");
mysqli_close($conexion);
}
?>
</body>
</html>
-
Hola! ¿Tienes activado php debugging? (Ver https://aprenderaprogramar.com/foros/index.php?topic=1261.0) ¿Has logrado acceder a la base de datos con php en alguna ocasión o es la primera vez que lo intentas?
Saludos!
-
Hola... tengo activado el XAMPP y si que logro acceder a la base de datos el problema persiste en la sentencia SQL donde me informa de que no tengo definida el $_POST[‘taxi_radio’] y la verdad no sé si hago bien la sentencia SQL en la página 2
-
Hola! El XAMPP funciona como servidor web, mysql, php, perl, pero a su vez lo que te comentaba es si tienes activado el php debugging porque si no lo tienes activado no visualizarás los mensajes de error y si no los visualizas lo tienes mucho más difícil para depurar.
Veo que boton_enviar lo tienes definido en el formulario, sin embargo no veo ningún campo del formulario que se llame taxi_radio
¿Te muestra el valor de taxi_radio en este fragmento o te aparece en blanco?
<td colspan="2" align="center"><b>Matricula: <font color="#BB0B0E">
<?php echo($_POST['taxi_radio']);?></font></b></td>
¿Puedes probar esto y ver qué te muestra?
<?php
if(!isset($_POST['taxi_radio'])){ echo "¡¡¡¡¡NO ESTOY RECIBIENDO TAXI_RADIO!!!!!!";}
else { echo "SI ESTOY RECIBIENDO TAXI_RADIO CON VALOR".taxi_radio; }
?>
Aparte se ve raro
$update ="UPDATE taxis SET Modelo='".$_POST['Modelo']."',"."Nombre='".$_POST['Nombre']."',";
$update.="Apellidos='".$_POST['Apellidos']."',Ocupado=";
¿Ahí no te falta definir qué registro vas a modificar con un where?
Echale un vistazo a este hilo a ver si te ayuda -- > https://aprenderaprogramar.com/foros/index.php?topic=3157.0 o a este curso https://aprenderaprogramar.com/index.php?option=com_content&view=category&id=70&Itemid=193
Saludos!
-
Primero de todo, mil gracias por tu tiempo, agradezco enormemente tu ayuda, Segundo, acabo de darme cuenta que en la Pagina 2 me falto la parte del WHERE completarla, mil perdones, causa las múltiples pruebas que estaba realizando, este seria el codigo completo de la Pagina 2
<?php
$conexion= mysqli_connect("localhost: 3306", "root", "", "taxicorp")or die("Uppsss!!... ALgo ha ido mal...");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Modificar datos</title>
</head>
<style>
@font-face{
font-family: 'RobotoMono';
src: url(Tipografias/RobotoMono.ttf);
font-style: normal;
}
table{
width: 765px; height: 20px;
border: ridge thick aliceblue;
background-color: antiquewhite;
table-layout: inherit;
}
#estatic{
position: absolute;
left: 185px; top: 85px;
}
h3{
color: aliceblue;
}
</style>
<body>
<?php
if(!isset($_POST['boton_enviar'])){
$conexion= mysqli_connect("localhost: 3306", "root", "", "taxicorp")or die("Uppsss!!... ALgo ha ido mal...");
$sql="SELECT * FROM taxis WHERE Matricula='".$_POST['taxi_radio']."'";
$exec= mysqli_query($conexion, $sql);
$registro= mysqli_fetch_row($exec);
?>
<div id="estatic">
<form action="mod_dat_tax2.php" method="post">
<table border="1">
<tr>
<td colspan="2" align="center"><b>Matricula: <font color="#BB0B0E">
<?php echo($_POST['taxi_radio']);?></font></b></td>
</tr>
<tr>
<td><b>Modelo:</b></td>
<td><input type="text" name="Modelo" size="40" value="<?php echo($registro[1]);?>"></td>
</tr>
<tr>
<td><b>Nombre y Apellidos:</b></td>
<td>
<input type="text" name="Nombre" value="<?php echo($registro[2]);?>">
<br>
<input type="text" name="Apellidos" size="35" value="<?php echo($registro[3]);?>"></td>
</tr>
<tr>
<td><b>Libre:</b></td>
<td><input type="checkbox" name="libre" <?php if($registro[4]) echo("checked");?>></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="boton_enviar" value="Modificar Datos"></td>
</tr>
</table>
</form>
<a href="mod_dat_tax.php" style="float: right; font: 14px bold RobotoMono; color: aliceblue">Volver</a>
</div>
<?php
}else{
$update ="UPDATE taxis SET Modelo='".$_POST['Modelo']."',"."Nombre='".$_POST['Nombre']."',";
$update.="Apellidos='".$_POST['Apellidos']."',Ocupado=";
if (isset($_POST['libre']))
$update.="true";
else
$update.="false";
$update.=" WHERE Matricula='".$_POST['taxi_radio']."'";
// Sentencia SQL
$exec= mysqli_query($conexion, $update);
echo("<br>");
// Registro en la tabla taxis
if(!$exec)
echo("<br><br><center><h3>Datos Modificados!!</h3></center><br>
<a href='mod_dat_tax.php'><center><input type='button' name='button' value='Volver'></center></a>");
else
echo("<br><br><center><h3>Error!!</h3></center><br>
<a href='mod_dat_tax.php'><center><input type='button' name='button' value='Volver'></center></a>");
mysqli_close($conexion);
}
?>
</body>
</html>
Tercero el error que me da es este Notice: Undefined index: taxi_radio in C:\xampp\htdocs\Proyecto_final\mod_dat_tax2.php on line 89
Error!!
y por último cuarto, el fin es modificar los datos de Modelo, Nombre, Apelilidos y Ocupado (SI o No) de la matricula del vehiculo y no se que sentencia SQL adoptar.
-
¿Has podido probar lo otro que te comentaba?
-
Buenos días...sobre tu pregunta he consultado desde el mismo hilo que indicabas anteriormente y no he logrado activarlo, vamos que no me aclaro aún, seguiré con la consulta ...
Referente al pequeño problema con mi código, sigo insistiendo que no está bien declarada la sentencia Update, a ver si saco algo en claro antes de agotar mi paciencia 😂😂
-
Hola! El update no te funcionará si no hay ningún registro que cumpla la condición que le impongas. El mensaje de error que estás recibiendo parece indicar que no está definido taxi_radio. Por esto te preguntaba qué ocurre si pides mostrar por pantalla esto:
<?php
if(!isset($_POST['taxi_radio'])){ echo "¡¡¡¡¡NO ESTOY RECIBIENDO TAXI_RADIO!!!!!!";}
else { echo "SI ESTOY RECIBIENDO TAXI_RADIO CON VALOR ".taxi_radio; }
?>
-
¡¡¡¡¡NO ESTOY RECIBIENDO TAXI_RADIO!!!!!!
Notice: Undefined index: taxi_radio in C:\xampp\htdocs\Proyecto_final\mod_dat_tax2.php on line 95
Vale... ahora toca preguntar como defino taxi_radio del cual es el nombre de la selección de vehículos anteriormente elegida en la pagina 1,
<input type="radio" name="taxi_radio" id="taxi_radio" value="<?php echo($dat[0]);?>">