Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/06/2008, 20:03
blindex
 
Fecha de Ingreso: junio-2008
Mensajes: 1
Antigüedad: 15 años, 10 meses
Puntos: 0
Problema PHP y MYSql SOy novato.

buenas, es mi primer post en este foro, y por ende soy nuevo en esto de php, mi problema es el siguiente, tengo un formulario con campos "nombre, apellido y edad" y abajo una tabla que me muestra los mismos campos pero con registros de un mysql, al lado de cada registro tengo un link que dice modificar, cuando activo ese link se completan los datos del registros en el formulario y ahi me permitiria modificar los datos. Si no presiono modificar, y lleno los campos por mi cuenta, lo que hace es agregar a la base de datos los datos que coloque. Ahora bien, este es mi problema, yo quiero que este todo en una misma pagina, y lo que me esta sucediendo es que cuando hago el siguiente procedimiento no funciona como corresponde.

1. Abre la pagina, todo perfecto, debajo se ven los registros cargados en la base y los campos del form estan vacios

2. si presiono el link modificar, se llenan los campos, perfecto, mando el submit y los modifica

3. aca viene uno de mis problemas, si ahora lleno los campos por mi cuenta por q quiero agregar un registro a la base de datos, me vuelve a modificar el campo anterior..., yo lo que quiero en este paso es q agregue uno nuevo.

Les dejo aca el codigo, y disculpen que soy nuevo en esto, si necesitan mas datos diganme, gracias.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<?php

$link= mysql_connect("localhost","prueba","prueba");

mysql_select_db("prueba",$link);

if($_GET[modificar])
{
$consulta= "SELECT * FROM inv WHERE id='$_GET[modificar]'";
$result= mysql_query($consulta, $link);
$row2 = mysql_fetch_array($result);
$b= 1;
}

if($_POST[Submit])
{
if ( $b == 1 )
{
$consulta= "UPDATE inv SET nombre= '$_POST[nombre]', apellido= '$_POST[apellido]', edad= '$_POST[edad]' WHERE id='$_GET[modificar]'" ;
mysql_query($consulta, $link);
$b= 0;
}
elseif($_POST[Submit])
{$consulta= "INSERT INTO inv VALUES(NULL, '$_POST[nombre]', '$_POST[apellido]', '$_POST[edad]')";
mysql_query($consulta, $link);
}

}
var_dump($b, $consulta);

$consulta= "SELECT * FROM inv";

$result= mysql_query($consulta, $link);


?>
<form id="form" name="form" method="post" action="">
<table width="338" border="1">
<tr>
<th width="144" scope="col"><label>NOMBRE</label></th>
<th width="178" scope="col"><div align="center">


<input type="text" name="nombre" id="textfield" value="<? if ($_GET[modificar] && !$_POST[Submit])
{echo "$row2[nombre]";} ?> "/>
</div></th>
</tr>
<tr>
<th scope="row">APELLIDO</th>
<td><div align="center">
<input type="text" name="apellido" id="textfield2" value="<? if ($_GET[modificar] && !$_POST[Submit])
{echo "$row2[apellido]";} ?> " />
</div></td>
</tr>
<tr>
<th scope="row">EDAD</th>
<td><div align="center">
<input type="text" name="edad" id="textfield3" value="<? if ($_GET[modificar] && !$_POST[Submit])
{echo "$row2[edad]";} ?> " />
</div></td>
</tr>
</table>
<p>
<label>
<input type="submit" name="Submit" id="button" value="Submit" />
</label>
</p>
</form>

<table width="50%" border="1">
<tr>
<th scope="col">NOMBRE</th>
<th scope="col">APELLIDO</th>
<th scope="col">EDAD</th>
<th scope="col">&nbsp;</th>
</tr>
<? while($row = mysql_fetch_array($result)) {?>
<tr>
<td><? echo $row[nombre]; ?></td>
<td><? echo $row[apellido]; ?></td>
<td><? echo $row[edad]; ?></td>
<td><? echo "<a href= '?modificar=$row[id]'>Modificar</a>";?> </td>
</tr>
<? } ?>
</table>


</body>
</html>