Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] PHP OO ¿Cómo actualizar registros de bases de datos MYSQL con php?

Estas en el tema de ¿Cómo actualizar registros de bases de datos MYSQL con php? en el foro de PHP en Foros del Web. Saludos a todos los del foro Necesito ayuda para actualizar registros de bases de datos mysql con php. este es el ódigo que utlizo y ...
  #1 (permalink)  
Antiguo 17/07/2015, 23:39
 
Fecha de Ingreso: mayo-2010
Mensajes: 30
Antigüedad: 13 años, 11 meses
Puntos: 0
Exclamación ¿Cómo actualizar registros de bases de datos MYSQL con php?

Saludos a todos los del foro
Necesito ayuda para actualizar registros de bases de datos mysql con php.


este es el ódigo que utlizo y no me funciona, no se que hacer. Si alguien me puede ayudar le pediria que me dijera de forma específica que debo hacer ya que soy novato en esto.

CODIGO:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>

<body>
<?php
// ACA CREO LA COEXCIÓN A LABASE DE DATOS
$conexion=mysqli_connect("localhost","root","","si ndicatura") or die("Problemas con la conexión");
$registros=mysqli_query($conexion,"SELECT `id_item`, `item_pedido`, `fecha_peticion`, `costo`, `estado_aprobacion`, `nombre_quien_aprueba`, `fecha_aprobacion` FROM `registros`;") or
die("Problemas en el select:".mysqli_error($conexion)
);

// ACA LEO LA BASE DE DATOS Y LA CONVIERTO EN UN ARRAY EN PHP
$a=0;
while ($reg=mysqli_fetch_array($registros))
{
$registro [$a]=array('id_item'=>$reg['id_item'],'item_pedido'=>$reg['item_pedido'],'fecha_peticion'=>$reg['fecha_peticion'],'costo'=>$reg['costo'],'estado_aprobacion'=>$reg['estado_aprobacion'],'nombre_quien_aprueba'=>$reg['nombre_quien_aprueba'],'fecha_aprobacion'=>$reg['fecha_aprobacion']);
$a++;
}

//ACA CREO EL ENCABEZADO DE LA TABLA PARA LA PRESENTACIÓN DE LA INFORMACION DE LA BASE DE DATOS
echo "<table width='847' border='1' cellspacing='2' cellpadding='3'> \n
<tr>
<th width='85' bgcolor='#999999' scope='col'>ID ITEM</th>
<th width='195' bgcolor='#999999' scope='col'>SOLICITUD</th>
<th width='87' bgcolor='#999999' scope='col'>FECHA DE SOLICITUD</th>
<th width='72' bgcolor='#999999' scope='col'>COSTO</th>
<th width='111' bgcolor='#999999' scope='col'>ESTADO DE LA SOLICITUD</th>
<th width='115' bgcolor='#999999' scope='col'>NOMBRE DE QUIEN APRUEBA</th>
<th width='107' bgcolor='#999999' scope='col'>FECHA DE APROBACION</th>
</tr>
";
?>


<?php
//ACA IMPRIMO LA BASE DE DATOS COMO UN FORMULARIO HTML PARA ENVIARLA COMO UN FORMULARIO DE ACTUALIZACION

for($f=0;$f<count($registro);$f++)
{ ?>
<form action="" method="get">
<td><input type="text" value="<?php echo $registro [$f]['id_item']?>" name="id_item"></td>
<td><input type="text" value="<?php echo $registro[$f]['item_pedido']?>" name="item_pedido"></td>
<td><input type="text" value="<?php echo $registro[$f]['fecha_peticion']?>" name="fecha_peticion" ></td>
<td><input type="text" value="<?php echo $registro[$f]['costo']?>" name="costo"></td>
<td><input type="text" value="<?php echo $registro[$f]['estado_aprobacion']?>" name="estado_aprobacion"></td>
<td><input type="text" value="<?php echo $registro[$f]['nombre_quien_aprueba']?>" name="nombre_quien_aprueba" class="letrapequeña" ></td>
<td><input type="text" value="<?php echo $registro[$f]['fecha_aprobacion']?>" name="fecha_aprobacion"></td>
<td>
<input type="submit" value="APROBAR gastos" name="aprobar" > </form></tr>
<?php
}

// EN ESTE PUNTO NO ME REALIZA LA FUNCION DE ACTUALIZACION ***solicito ayuda aqui****

if(isset($_POST['aprobar'])){
mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = 1'");
}

?>

</body>
</html>
  #2 (permalink)  
Antiguo 18/07/2015, 00:48
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

Cita:
// EN ESTE PUNTO NO ME REALIZA LA FUNCION DE ACTUALIZACION ***solicito ayuda aqui****
Ajá, ¿y qué has investigado al respecto? ¿recibes algún error? ¿se queda en blanco? ¿qué sucede? ¿adivinamos?
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #3 (permalink)  
Antiguo 18/07/2015, 08:39
 
Fecha de Ingreso: mayo-2010
Mensajes: 30
Antigüedad: 13 años, 11 meses
Puntos: 0
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

Cita:
Iniciado por pateketrueke Ver Mensaje
Ajá, ¿y qué has investigado al respecto? ¿recibes algún error? ¿se queda en blanco? ¿qué sucede? ¿adivinamos?
Perdón, tienes toda la razón. Vuelvo a enviar el código con un pequeño arreglo en e if final.

ESTE ES EL CÓDIGO:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>

<body>
<?php
// ACA CREO LA COEXCIÓN A LABASE DE DATOS
$conexion=mysqli_connect("localhost","root","","si ndicatura") or die("Problemas con la conexión");
$registros=mysqli_query($conexion,"SELECT `id_item`, `item_pedido`, `fecha_peticion`, `costo`, `estado_aprobacion`, `nombre_quien_aprueba`, `fecha_aprobacion` FROM `registros`;") or
die("Problemas en el select:".mysqli_error($conexion)
);

// ACA LEO LA BASE DE DATOS Y LA CONVIERTO EN UN ARRAY EN PHP
$a=0;
while ($reg=mysqli_fetch_array($registros))
{
$registro [$a]=array('id_item'=>$reg['id_item'],'item_pedido'=>$reg['item_pedido'],'fecha_peticion'=>$reg['fecha_peticion'],'costo'=>$reg['costo'],'estado_aprobacion'=>$reg['estado_aprobacion'],'nombre_quien_aprueba'=>$reg['nombre_quien_aprueba'],'fecha_aprobacion'=>$reg['fecha_aprobacion']);
$a++;
}

//ACA CREO EL ENCABEZADO DE LA TABLA PARA LA PRESENTACIÓN DE LA INFORMACION DE LA BASE DE DATOS
echo "<table width='847' border='1' cellspacing='2' cellpadding='3'> \n
<tr>
<th width='85' bgcolor='#999999' scope='col'>ID ITEM</th>
<th width='195' bgcolor='#999999' scope='col'>SOLICITUD</th>
<th width='87' bgcolor='#999999' scope='col'>FECHA DE SOLICITUD</th>
<th width='72' bgcolor='#999999' scope='col'>COSTO</th>
<th width='111' bgcolor='#999999' scope='col'>ESTADO DE LA SOLICITUD</th>
<th width='115' bgcolor='#999999' scope='col'>NOMBRE DE QUIEN APRUEBA</th>
<th width='107' bgcolor='#999999' scope='col'>FECHA DE APROBACION</th>
</tr>
";
?>


<?php
//ACA IMPRIMO LA BASE DE DATOS COMO UN FORMULARIO HTML PARA ENVIARLA COMO UN FORMULARIO DE ACTUALIZACION
for($f=0;$f<count($registro);$f++)
{ ?>
<form action="" method="get">
<td><input type="text" value="<?php echo $registro [$f]['id_item']?>" name="id_item"></td>
<td><input type="text" value="<?php echo $registro[$f]['item_pedido']?>" name="item_pedido"></td>
<td><input type="text" value="<?php echo $registro[$f]['fecha_peticion']?>" name="fecha_peticion" ></td>
<td><input type="text" value="<?php echo $registro[$f]['costo']?>" name="costo"></td>
<td><input type="text" value="<?php echo $registro[$f]['estado_aprobacion']?>" name="estado_aprobacion"></td>
<td><input type="text" value="<?php echo $registro[$f]['nombre_quien_aprueba']?>" name="nombre_quien_aprueba" class="letrapequeña" ></td>
<td><input type="text" value="<?php echo $registro[$f]['fecha_aprobacion']?>" name="fecha_aprobacion"></td>
<td>
<input type="submit" value="APROBAR gastos" name="aprobar" > </form></tr>
<?php
}

// EN ESTE PUNTO NO ME REALIZA LA FUNCION DE ACTUALIZACION ***solicito ayuda aqui****

if(isset($_POST['aprobar'])){
mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = $_POST['id_item']);");
}

?>

</body>
</html>


ESTES ES EL ERRO QUE PRESENTA:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\pruebas\array_mysql3.php on line 59

LA LINEA 59 ES LA LINEA QUE ESTA DENTRO DEL if
  #4 (permalink)  
Antiguo 18/07/2015, 09:43
Avatar de xfxstudios  
Fecha de Ingreso: junio-2015
Ubicación: Valencia - Venezuela
Mensajes: 2.448
Antigüedad: 8 años, 9 meses
Puntos: 263
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

en esta linea
Código PHP:
Ver original
  1. mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = $_POST['id_item']);");

deberia esta cerrada asi
Código PHP:
Ver original
  1. mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = '$_POST[id_item]'");
__________________
[email protected]
HITCEL
  #5 (permalink)  
Antiguo 18/07/2015, 09:45
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

¿Esta linea?
Código PHP:
Ver original
  1. mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = $_POST['id_item']);");

Pues sí, en definitiva está mal.

Estás mezclando comillas de manera incorrecta.

Te sugiero leer el siguiente tema para aprender a usar las comillas: http://www.forosdelweb.com/f18/como-...s-bien-588701/
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #6 (permalink)  
Antiguo 18/07/2015, 10:01
 
Fecha de Ingreso: mayo-2010
Mensajes: 30
Antigüedad: 13 años, 11 meses
Puntos: 0
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

Cita:
Iniciado por pateketrueke Ver Mensaje
¿Esta linea?
Código PHP:
Ver original
  1. mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = $_POST['id_item']);");

Pues sí, en definitiva está mal.

Estás mezclando comillas de manera incorrecta.

Te sugiero leer el siguiente tema para aprender a usar las comillas: [url]http://www.forosdelweb.com/f18/como-usar-las-comillas-bien-588701/[/url]
GRACIAS POR EL CONSEJO, YA CAMBIE LAS COMILLAS ASI:

if(isset($_POST['aprobar'])){
mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = '$_POST[id_item]'");
}


Ya no sale mensaje de error, pero el problema continua, pues no realiza el UPDATE, que necesito para actualizar la base de datos que tengo en MySql...

Mepueden explicar donde esta el error.

También me sirve si alguien sabe un método más fácil para actualizar una base de datos de MySql.

Gracias por sus respuestas.
  #7 (permalink)  
Antiguo 18/07/2015, 10:12
Avatar de xfxstudios  
Fecha de Ingreso: junio-2015
Ubicación: Valencia - Venezuela
Mensajes: 2.448
Antigüedad: 8 años, 9 meses
Puntos: 263
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

la pregunta es la siguiente, estas actualizando un solo registro o mediante un array de registros?
__________________
[email protected]
HITCEL
  #8 (permalink)  
Antiguo 18/07/2015, 10:30
 
Fecha de Ingreso: mayo-2010
Mensajes: 30
Antigüedad: 13 años, 11 meses
Puntos: 0
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

Cita:
Iniciado por xfxstudios Ver Mensaje
la pregunta es la siguiente, estas actualizando un solo registro o mediante un array de registros?
Es para actualizar cada registro de forma individual
  #9 (permalink)  
Antiguo 18/07/2015, 11:00
Avatar de xfxstudios  
Fecha de Ingreso: junio-2015
Ubicación: Valencia - Venezuela
Mensajes: 2.448
Antigüedad: 8 años, 9 meses
Puntos: 263
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

ok
Código PHP:
Ver original
  1. mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = $_POST['id_item']);");

Que es sindicatura???
Que es registros????
son 2 tablas o la base y la tabla????

porque alli puede estar el problema
__________________
[email protected]
HITCEL
  #10 (permalink)  
Antiguo 18/07/2015, 11:06
 
Fecha de Ingreso: mayo-2010
Mensajes: 30
Antigüedad: 13 años, 11 meses
Puntos: 0
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

Cita:
Iniciado por xfxstudios Ver Mensaje
ok
Código PHP:
Ver original
  1. mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = $_POST['id_item']);");

Que es sindicatura???
Que es registros????
son 2 tablas o la base y la tabla????

porque alli puede estar el problema
SINDICATURA es la base de datos y REGISTROS es una tabla de esa base de datos
  #11 (permalink)  
Antiguo 18/07/2015, 11:09
Avatar de xfxstudios  
Fecha de Ingreso: junio-2015
Ubicación: Valencia - Venezuela
Mensajes: 2.448
Antigüedad: 8 años, 9 meses
Puntos: 263
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

si la tienes especificada en tu archivo de conexión no hace falta alli, prueba asi

Código PHP:
Ver original
  1. mysqli_query($conexion,"UPDATE registros SET estado_aprobacion = 'APROBADO'  WHERE registros.id_item = '$_POST[id_item]'");
__________________
[email protected]
HITCEL
  #12 (permalink)  
Antiguo 18/07/2015, 11:24
 
Fecha de Ingreso: mayo-2010
Mensajes: 30
Antigüedad: 13 años, 11 meses
Puntos: 0
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

Ya lo prové y no actualiza nada.

Mira yo tengo este otro código, que es muy parecido y que actualiza, un solo registro el id_item que ya tengo previamente en el codigo selecionado, este codigo hace la actualización bien, pero el problema es que necesito que sea de cada uno de los registros que selecione con el boton.

A continuación el código que solo actualiza un registro:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>

<body>
<p class="mio">VER REGISTRO DE GASTOS DE LOS FRAILES ESTUDIANTES
<form action="" method="post">
<span class="mio">
<input type="submit" value="Ver registro de gastos de todos los frailes" name="ver_lista_gastos" >
</span>
</form>
<table style="width: 100%">
<?php
$conexion=mysqli_connect("localhost","root","","si ndicatura") or die("Problemas con la conexión");
$consulta=mysqli_query($conexion,"SELECT `id_item`, `item_pedido`, `fecha_peticion`, `costo`, `estado_aprobacion`, `nombre_quien_aprueba`, `fecha_aprobacion` FROM `registros`;") or
die("Problemas en el select:".mysqli_error($conexion));
echo "<table width='847' border='1' cellspacing='2' cellpadding='3'> \n
<tr>
<th width='195' bgcolor='#999999' scope='col'>SOLICITUD</th>
<th width='87' bgcolor='#999999' scope='col'>FECHA DE SOLICITUD</th>
<th width='72' bgcolor='#999999' scope='col'>COSTO</th>
<th width='111' bgcolor='#999999' scope='col'>ESTADO DE LA SOLICITUD</th>
<th width='115' bgcolor='#999999' scope='col'>NOMBRE DE QUIEN APRUEBA</th>
<th width='107' bgcolor='#999999' scope='col'>FECHA DE APROBACION</th>
</tr>
";

?>
<form action="" method="post">
<?php
while($registro = mysqli_fetch_array($consulta))
{
?>

<td><input type="text" value="<?php echo $registro['item_pedido']?>" name="nombres[]"></td>
<td><input type="text" value="<?php echo $registro['fecha_peticion']?>" name="departamento[]" class="letrapequeña" ></td>
<td><input type="text" value="<?php echo $registro['costo']?>" name="idempleado[]"></td>
<td><input type="text" value="<?php echo $registro['estado_aprobacion']?>" name="nombres[]"></td>
<td><input type="text" value="<?php echo $registro['nombre_quien_aprueba']?>" name="departamento[]" class="letrapequeña" ></td>
<td><input type="text" value="<?php echo $registro['fecha_aprobacion']?>" name="idempleado[]"></td>
<td>
<input type="submit" value="APROBAR gastos" name="aprobar" ></td>

</tr>

<?php
}

if(isset($_POST['aprobar'])){
$id_item2=$registro['id_item'];

mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = 1;");
}

?>
</form>

</table>
</body>
</html>

Yo a este codigo no le encuentro diferencia con el anterior que no funciona, la diferencia es con el anterior que busco hacer la actualización de forma dinamica para cada registro.
  #13 (permalink)  
Antiguo 18/07/2015, 11:44
 
Fecha de Ingreso: mayo-2010
Mensajes: 30
Antigüedad: 13 años, 11 meses
Puntos: 0
Respuesta: ¿Cómo actualizar registros de bases de datos MYSQL con php?

YA ME DIO LA SOLUCIÓN: No fue necesario el FOR sino solo con el WHILE y poner el FORM dentro del WHILE.

ESTE ES EL CÓDIGO:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>

<body>
<p class="mio">VER REGISTRO DE GASTOS DE LOS FRAILES ESTUDIANTES
<form action="" method="post">
<span class="mio">
<input type="submit" value="Ver registro de gastos de todos los frailes" name="ver_lista_gastos" >
</span>
</form>
<table style="width: 100%">
<?php
$conexion=mysqli_connect("localhost","root","","si ndicatura") or die("Problemas con la conexión");
$consulta=mysqli_query($conexion,"SELECT `id_item`, `item_pedido`, `fecha_peticion`, `costo`, `estado_aprobacion`, `nombre_quien_aprueba`, `fecha_aprobacion` FROM `registros`;") or
die("Problemas en el select:".mysqli_error($conexion));
echo "<table width='847' border='1' cellspacing='2' cellpadding='3'> \n
<tr>
<th width='85' bgcolor='#999999' scope='col'>ID ITEM</th>
<th width='195' bgcolor='#999999' scope='col'>SOLICITUD</th>
<th width='87' bgcolor='#999999' scope='col'>FECHA DE SOLICITUD</th>
<th width='72' bgcolor='#999999' scope='col'>COSTO</th>
<th width='111' bgcolor='#999999' scope='col'>ESTADO DE LA SOLICITUD</th>
<th width='115' bgcolor='#999999' scope='col'>NOMBRE DE QUIEN APRUEBA</th>
<th width='107' bgcolor='#999999' scope='col'>FECHA DE APROBACION</th>
</tr>
";

?>

<?php
while($registro = mysqli_fetch_array($consulta))
{
?><form action="" method="post">
<td><input type="text" value="<?php echo $registro['id_item']?>" name="id_item"></td>
<td><input type="text" value="<?php echo $registro['item_pedido']?>" name="nombres[]"></td>
<td><input type="text" value="<?php echo $registro['fecha_peticion']?>" name="departamento[]" class="letrapequeña" ></td>
<td><input type="text" value="<?php echo $registro['costo']?>" name="idempleado[]"></td>
<td><input type="text" value="<?php echo $registro['estado_aprobacion']?>" name="nombres[]"></td>
<td><input type="text" value="<?php echo $registro['nombre_quien_aprueba']?>" name="departamento[]" class="letrapequeña" ></td>
<td><input type="text" value="<?php echo $registro['fecha_aprobacion']?>" name="idempleado[]"></td>
<td>
<input type="submit" value="APROBAR gastos" name="aprobar" ></form> </td>

</tr>

<?php
}

if(isset($_POST['aprobar'])){
$id_item2=$registro['id_item'];

mysqli_query($conexion,"UPDATE `sindicatura`.`registros` SET `estado_aprobacion` = 'APROBADO' WHERE `registros`.`id_item` = '$_POST[id_item]';");
}

?>


</table>
</body>
</html>


Gracias a las personas del foro que se interesaron por ayudarme.

Etiquetas: bases, fecha, formulario, html, mysql, registro, registros, select, sql, tabla
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:56.