Acceso a Base de datos en PHP y ASP (parte 2)
En esta ocasi贸n muestro como creo y edito un registro de la tabla Noticias:
tabla (Noticias)
IdNoticia (int)
Nombre (varchar)
fecha (datetime)
Tendremos dos p谩ginas (se puede hacer con una sola, con una llamada a si m铆sma).
Noticias.php (asp)
Gnoticias.php (asp)
________________________________________________
C贸digo en php
—–Noticias.php——
<?php header("Content-Type: text/html; charset=UTF-8");
$TIP=$_GET["TIP"]; nota: Esta variable la mando por la URL de la forma http://nombrepagina.php?TIP=Editar (Editar o nuevo)
if ($TIP=="Editar")
{
$id=$_GET["ID"];
$conexion=mysql_connect($DBserver,$DBuser,$DBpassword);
mysql_select_db($DBname,$conexion) or die(mysql_error());
mysql_query("SET NAMES ‘utf8′");
$SQL = "SELECT Nombre,date_format(fecha,’%d/%m/%Y’) FROM Noticias where IDnoticia=".$id;
$tabla=mysql_query($SQL);
if (!$tabla) {
echo "No se pudo ejecutar el query: " . mysql_error();
exit;
}
mysql_close($conexion);
$rows=mysql_num_rows($tabla);
if($rows== 1)
{
$row = mysql_fetch_array($tabla);
$vNombre=$row[0];
$vFecha=$row[1];
}
mysql_free_result($tabla);
}
?>
<form id="form" method="post" action="GNoticias.php?TIP=<?php echo $TIP;?>">
<?php echo $TIP;?> noticia
<br><br>
<input name="txtID" type="hidden" value="<?php echo $id;?>"
Nombre: <input name="txtNombre" id="txtNombre" value="<?php echo $vNombre?>" size="40" /><br>
<input name="txtFecha" id="txtFecha" value="<?php echo $vFecha?>" size="20" maxlength="12" /><br>
<input class="submit" type="submit" value="Guardar">
</form>
——Gnoticias.php—–
<?php header(’Content-Type: text/html; charset=UTF-8′);
$rT=$_POST["txtNombre"];
$rD=$_POST["txtFecha"];
$TIP=$_GET["TIP"];
if ($TIP=="Editar")
{
$id=$_POST["txtID"];
$SQL="Update Noticias set Nombre=’".$rT."’,Fecha=’".MysqlDate($rD)."’ where idNoticia=".$id;
}
else
$SQL="Insert into Noticias ( Nombre,Fecha) values(’".$rT."’,'".MysqlDate($rD)."’)";
$conexion=mysql_connect($DBserver,$DBuser,$DBpassword);
mysql_select_db($DBname,$conexion) or die(mysql_error($conexion));
mysql_query("SET NAMES ‘utf8′");
mysql_query($SQL);
if (mysql_error($conexion)!=”")
echo "No se ha podido guardar el registro";
else
echo "El registro se guardo con exito";
mysql_close($conexion);
?>
_______________Funci贸n MysqlDate____________________
function MysqlDate($dateR)
{
$date1=split(”/”,$dateR);
$Fecha1=$date1[2].”-”.$date1[1].”-”.$date1[0];
return $Fecha1;
}
_______________________________________________________________
C贸digo en asp
——Noticias.asp——
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
strCon="Driver={MySQL ODBC 5.1 Driver};Server=TUDIRECCIONIP;Database=TUDB; User=TUUSUARIO;Password=TUPASSWORD;Option=3;"
response.expires=-1
dim vNombre
dim vFecha
dim id
TIP=request.QueryString("TIP")
if TIP="Editar" then
id=request.QueryString("ID")
set Conn=Server.CreateObject("ADODB.connection")
Conn.Open(strCon)
set rs = Server.CreateObject("ADODB.recordset")
SQL = "SELECT Nombre,date_format(fecha,’%d/%m/%Y’) FROM Noticias where IDnoticia="&id
On error Resume Next
rs.Open SQL, Conn
if Err<>0 then
response.Write(Err& " -"&ST&"- "&SQL)
rs.Close
set rs = nothing
Conn.Close
set Conn = nothing
else
vNombre=rs(0)
vFecha=rs(1)
rs.Close
Conn.Close
Set Conn = Nothing
end if
end if
%>
<form id="form" method="post" action="GNoticias.asp?TIP=<%=TIP%>">
<%=TIP%> noticia
<br><br>
<input name="txtID" type="hidden" value="<%=id%>"
Nombre: <input name="txtNombre" id="txtNombre" value="<%=vNombre%>" size="40" /><br>
<input name="txtFecha" id="txtFecha" value="<%=vFecha%>" size="20" maxlength="12" /><br>
<input class="submit" type="submit" value="Guardar">
</form>
——Gnoticias.asp——
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
strCon="Driver={MySQL ODBC 5.1 Driver};Server=TUDIRECCIONIP;Database=TUDB; User=TUUSUARIO;Password=TUPASSWORD;Option=3;"
dim rN,rD,date1,TIP
rN=request.Form("txtNombre")
rD=request.Form("txtFecha")
TIP=request.QueryString("TIP")
date1=split(rD,"/")
Fecha1=date1(2)&"-"&date1(1)&"-"&date1(0)
iF TIP="Editar" then
id=request.Form("txtID")
SQL=”Update Noticias set Nombre=’"&rN&"’,Fecha=’"&Fecha&"’ where idNoticia="&id
else
SQL=”Insert into Noticias ( Nombre,Fecha) values(’"&rN&"’,'"&Fecha&"’)"
end iF
set Conn=Server.CreateObject("ADODB.connection")
Conn.Open(strCon)
On error Resume Next
Conn.Execute(SQL)
if Err<>0 then
response.Write("<span class=’Estilo4′>No se ha podido guardar el registro</span> ")
else
response.Write("<span class=’Estilo4′>El registro se guardo con exito</span>")
end if
Conn.Close
set Conn = nothing
%>
_______________Funci贸n MysqlDate____________________
function MysqlDate(dateR)
dim date1,Fecha1
date1=split(dateR,"/")
Fecha1=date1(2)&"-"&date1(1)&"-"&date1(0)
MysqlDate=Fecha1
end function
Puedes seguir cualquier respuesta a esta entrada mediante el canal RSS 2.0. Puedes dejar un comentario o enviar un trackback desde tu propio sitio.
Dejar un comentario
Debes ingresar para dejar un comentario.