<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" ><channel><title>unijimpe &#187; PHP</title> <atom:link href="http://blog.unijimpe.net/category/php/feed/" rel="self" type="application/rss+xml" /><link>http://blog.unijimpe.net</link> <description>Desarrollo Web, SEO, Adsense</description> <lastBuildDate>Mon, 28 Nov 2011 03:18:21 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Manejo de sesiones en PHP</title><link>http://blog.unijimpe.net/manejo-de-sesiones-en-php/</link> <comments>http://blog.unijimpe.net/manejo-de-sesiones-en-php/#comments</comments> <pubDate>Sun, 30 Oct 2011 03:42:50 +0000</pubDate> <dc:creator>unijimpe</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[depurar]]></category> <category><![CDATA[sesiones]]></category> <category><![CDATA[session]]></category><guid isPermaLink="false">http://blog.unijimpe.net/?p=1297</guid> <description><![CDATA[<p>Las sesiones en PHP son una forma de mantener ciertos datos de un visitante entre los diversos accesos una web. De esta forma se pueden crear aplicaciones donde se le muestra al usuarios datos personalizados de &#8230;</p>]]></description> <content:encoded><![CDATA[<p>Las sesiones en PHP son una forma de mantener ciertos datos de un visitante entre los diversos accesos una web. De esta forma se pueden crear aplicaciones donde se le muestra al usuarios datos personalizados de acuerdo a las preferencias o acciones del usuario.</p><p>Las sesiones se manejan internamente por un identificador llamado SID (Session ID) el cual esta asociado a una única sesión y que ayuda a identificar el contenido de la sesión actual. Esta variable se propaga automaticamente a traves de Cookies o mediante URL.</p><p><strong>Manejo de sesiones en PHP</strong></p><p>Todas la variables de sesión son accesibles mediante arrays en la variable global $_SESSION. Adicionalmente ofrece una serie de funciones que permiten manejar detalladamente las sesiones de manera sencilla:</p><ul><li><strong>session_start</strong> inicializa una sesión y crea el identificador de sesión.</li><li><strong>session_id</strong> devuelve el identificador de la sesión actual.</li><li><strong>session_regenerate_id</strong> genera un nuevo identificador para la sesión actual.</li><li><strong>session_unset</strong> limpia todas la variables de sesión registradas.</li><li><strong>session_destroy</strong> elimina todas la variables de sesión registradas.</li></ul><p><strong>Crear variables de Sesión</strong></p><p>Si deseamos crear variables de sesión para que se puedan utilizar a través de diferentes páginas inicializamos el manejo de sesiones con al función <code>session_start</code> y luego guardamos el dato deseado como variable de sesión utilizando la variable global <code>$_SESSION</code>.</p><div class="syntax_hilite"><div id="php-8"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// home.php</span></div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/session_start"><span style="color:#000066;">session_start</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$_SESSION</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"country"</span><span style="color:#006600;">&#93;</span> = <span style="color:#FF0000;">"Peru"</span>;</div></li></ol></div></div></div><p></p><p>Podríamos mejorar el código si verificamos la existencia de la variable de sesión, si no existe entonces le asignamos un valor.</p><div class="syntax_hilite"><div id="php-9"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// home.php</span></div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/session_start"><span style="color:#000066;">session_start</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span>!<a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_SESSION</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"country"</span><span style="color:#006600;">&#93;</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$_SESSION</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"country"</span><span style="color:#006600;">&#93;</span> = <span style="color:#FF0000;">"Peru"</span>;&nbsp;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p><strong>Recuperar variables de Sesión</strong></p><p>En las páginas siguientes podríamos acceder a las variables de sesión utilizando:</p><div class="syntax_hilite"><div id="php-10"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// user.php</span></div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/session_start"><span style="color:#000066;">session_start</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$_SESSION</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"country"</span><span style="color:#006600;">&#93;</span>;</div></li></ol></div></div></div><p></p><p>Podríamos utilizar las variables de sesión para verificar si se han hecho acciones previas por parte del usuario, como haber seleccionado tu país por ejemplo si aún no lo ha hecho lo redireccionamos a la página inicial.</p><div class="syntax_hilite"><div id="php-11"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// user.php</span></div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/session_start"><span style="color:#000066;">session_start</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span>!<a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_SESSION</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"country"</span><span style="color:#006600;">&#93;</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"Location: home.php"</span><span style="color:#006600;">&#41;</span>;&nbsp; &nbsp;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#006600;">&#125;</span></div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$_SESSION</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"country"</span><span style="color:#006600;">&#93;</span>;</div></li></ol></div></div></div><p></p><p><strong>Eliminando datos de sesión</strong></p><p>Si deseamos eliminar una determinada variable de sesión le asignamos un valor vacío.</p><div class="syntax_hilite"><div id="php-12"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// clear.php</span></div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/session_start"><span style="color:#000066;">session_start</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$_SESSION</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"country"</span><span style="color:#006600;">&#93;</span> = <span style="color:#FF0000;">""</span>;</div></li></ol></div></div></div><p></p><p>Ahora si deseamos eliminar todas las variables de sesión lo hacemos de la siguiente forma:</p><div class="syntax_hilite"><div id="php-13"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// clear.php</span></div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/session_start"><span style="color:#000066;">session_start</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp;</div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/session_unset"><span style="color:#000066;">session_unset</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><a href="http://www.php.net/session_destroy"><span style="color:#000066;">session_destroy</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p><strong>Depurando Sesiones</strong></p><p>Si se desea depurar las variables de sesión con sus respectivos valores se puede utilizar la función <em>print_r</em>.</p><div class="syntax_hilite"><div id="php-14"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// debug.php</span></div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/session_start"><span style="color:#000066;">session_start</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_SESSION</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Para mas información acerca del manejo y de las funciones disponibles para el manejo de sesiones pueden leer <a href="http://www.php.net/manual/en/ref.session.php">PHP: Session Functions</a>. Para concluir les dejo los archivos fuente de este artículo para que puedan hacer sus pruebas.</p><p style="text-align:left;"><a href="http://blog.unijimpe.net/download/php-session.zip" title="Sesiones en PHP"><img src="http://blog.unijimpe.net/btdown.png" border="0"></a></p><p style="font-size: 10px;border-top: 1px solid #666666;margin-top=12px;padding-top:6px;">&copy; 2006 - 2011 <a href="http://blog.unijimpe.net">unijimpe</a> - Utiliza este feed solo para uso personal, partes de este feed pueden ser utilizados mencionando al autor, no esta permitido publicar enteramente este feed para uso comercial sin permiso del autor.</p><br /> ]]></content:encoded> <wfw:commentRss>http://blog.unijimpe.net/manejo-de-sesiones-en-php/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>PHP con MySQL: Eliminar Registros en MySQL</title><link>http://blog.unijimpe.net/php-con-mysql-eliminar-registros-en-mysql/</link> <comments>http://blog.unijimpe.net/php-con-mysql-eliminar-registros-en-mysql/#comments</comments> <pubDate>Wed, 12 Oct 2011 04:42:56 +0000</pubDate> <dc:creator>unijimpe</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[delete]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[select]]></category> <category><![CDATA[sql]]></category><guid isPermaLink="false">http://blog.unijimpe.net/?p=1258</guid> <description><![CDATA[<p>En artículos previos hemos explicado como interactuar con MySQL utilizando PHP, desde consultar, insertar y editar registros, ahora veremos como eliminar registros de manera sencilla. Si es que no has leído los artículos anteriores te recomiendo &#8230;</p>]]></description> <content:encoded><![CDATA[<p>En artículos previos hemos explicado como interactuar con MySQL utilizando PHP, desde consultar, insertar y editar registros, ahora veremos como eliminar registros de manera sencilla. Si es que no has leído los artículos anteriores te recomiendo revisar:</p><ul><li><a href="http://blog.unijimpe.net/php-con-mysql-introduccion-a-sql/">PHP con MySQL: Introducción a SQL</a></li><li><a href="http://blog.unijimpe.net/php-con-mysql-crear-una-conexion-a-mysql/">PHP con MySQL: Crear una conexión a MySQL</a></li><li><a href="http://blog.unijimpe.net/php-con-mysql-insertar-datos-en-mysql/">PHP con MySQL: Insertar datos en MySQL</a></li><li><a href="http://blog.unijimpe.net/php-con-mysql-consultar-datos-en-mysql/">PHP con MySQL: Consultar datos en MySQL</a></li><li><a href="http://blog.unijimpe.net/php-con-mysql-editar-registros-con-mysql/">PHP con MySQL: Editar Registros con MySQL</a></li></ul><p>Para eliminar registros seguiremos los siguientes pasos: primero haremos un listado de todos los registros de la tabla elegida, luego en el listado agregaremos un enlace a un archivo <em>delete.php</em> que se encargará de hacer la eliminación. Antes de eliminar el registro le preguntaremos al usuario si desea proceder con la eliminación. Finalmente eliminaremos el registro y redireccionaremos al usuario a listado de registros.</p><p><strong>Crear el Listado de Registros</strong></p><p>Para nuestro ejemplo utilizaremos la base de datos de ejemplo que contiene una tabla llamada <em>empresa</em> (Descargar el <a href="http://samples.unijimpe.net/demo-empresa.sql">Script SQL</a>). Creamos una consulta a la tabla 'empresa' y lo mostramos en una tabla HTML con un enlace a eliminar el registro.</p><div class="syntax_hilite"><div id="html-17"><div class="html"><ol><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php</div></li><li style="color:#26536A;"><div style="">// conexión a mysql</div></li><li style="color:#3A6A8B;"><div style="">require<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"conexion.php"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">require<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"funciones.php"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">// consulta de registros</div></li><li style="color:#26536A;"><div style="">$query = <span style="color: #ff0000;">"SELECT * FROM empresa ORDER BY nombre ASC"</span>;</div></li><li style="color:#3A6A8B;"><div style="">$queEmp = mysql_query<span style="color: #66cc66;">&#40;</span>$query, $conexion<span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">?<span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000;">&lt;html&gt;</span></a></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000;">&lt;head&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/title.html"><span style="color: #000000;">&lt;title&gt;</span></a></span>Listado de Empresas<span style="color: #009900;"><span style="color: #000000;">&lt;/title&gt;</span></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/head&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000;">&lt;body&gt;</span></a></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/table.html"><span style="color: #000000;">&lt;table&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php while <span style="color: #66cc66;">&#40;</span>$rsEmp = mysql_fetch_assoc<span style="color: #66cc66;">&#40;</span>$quEmp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> ?<span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#26536A;"><div style="">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000;">&lt;tr&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000;">&lt;td&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php echo $rsEmp<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'nombre'</span><span style="color: #66cc66;">&#93;</span>; ?<span style="color: #000000;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000;">&lt;/td&gt;</span></span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000;">&lt;td&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php echo $rsEmp<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'direccion'</span><span style="color: #66cc66;">&#93;</span>; ?<span style="color: #000000;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000;">&lt;/td&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000;">&lt;td&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/a.html"><span style="color: #000000;">&lt;a</span></a> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">"editar.php?id=&lt;?php echo $rsEmp['id']; ?&gt;</span>&quot;&gt;Editar<span style="color: #009900;">&lt;/a&gt;</span><span style="color: #009900;">&lt;/td&gt;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;">&lt;td&gt;</span><span style="color: #009900;">&lt;a href="</span>#<span style="color: #ff0000;">" onclick="</span>delEmpresa<span style="color: #66cc66;">&#40;</span><span style="color: #000000;">&lt;</span></a>?php echo $rsEmp<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">&#93;</span>; ?<span style="color: #000000;">&gt;</span></a></span>);&quot;&gt;Eliminar<span style="color: #009900;"><span style="color: #000000;">&lt;/a&gt;</span></span><span style="color: #009900;"><span style="color: #000000;">&lt;/td&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/tr&gt;</span></span></div></li><li style="color:#26536A;"><div style="">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php <span style="color: #66cc66;">&#125;</span> ?<span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/table&gt;</span></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text/javascript"</span><span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style="">function delEmpresa(id) {</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; if (window.confirm(&quot;Aviso:\nDesea eliminar el registro seleccionado?&quot;))&nbsp;{</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; window.location = &quot;delete.php?action=del<span style="color: #ddbb00;">&amp;id=&quot;+id;</span>&nbsp; &nbsp;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; }</div></li><li style="color:#3A6A8B;"><div style="">}</div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/script&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/body&gt;</span></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/html&gt;</span></span></div></li></ol></div></div></div><p></p><p>Hay que notar que en lugar de colocar un enlace directo a <em>delete.php</em> estamos llamando a una función Javascript <code>delEmpresa</code>, esta función le pregunta al usuario si esta seguro de eliminar el registro (Para evitar eliminar registros de manera accidental). Si el usuario acepta entonces redireccionamos a otra página donde pasamos el id del registro que deseamos eliminar.</p><p><strong>Eliminar el registro</strong></p><p>Creamos un archivo llamado <em>delete.php</em> el cual recibe dos parámetros: el identificador del registro un otro parámetro para verificar que acción se quiere realizar. Creamos la consulta para eliminar el registro y finalmente redireccionar al listado de registros.</p><div class="syntax_hilite"><div id="php-18"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// file: delete.php</span></div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"conexion.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"funciones.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$idempresa</span> = getParam<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"id"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">"-1"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$action</span> = getParam<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"action"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">""</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp;</div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$action</span> == <span style="color:#FF0000;">"del"</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$sql</span> = <span style="color:#FF0000;">"DELETE FROM empresa WHERE id = "</span>.sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$idempresa</span>, <span style="color:#FF0000;">"int"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$sql</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"location: listado.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Notar que estamos utilizando dos funciones especiales:</p><ul><li><code>getParam</code> permite filtrar los datos enviados por el usuario y definir un valor por defecto (En el ejemplo si no se ha enviado el parámetro id devuelve como valor -1).</li><li><code>sqlValue</code> permite formatear el tipo de dato que acepta la consulta (Por ejemplo se puede definir que el campo es entero <em>int</em>, si el usuario envía un cadena de texto esta función la convertirá a un entero).</li></ul><p>Para finalizar les dejo los archivos con el código fuente que incluye el archivo de conexión y las funciones utilizadas en nuestro ejemplo.</p><p style="text-align:left;"><a href="http://blog.unijimpe.net/download/php-mysql-delete.zip" title="PHP con MySQL: Eliminar registros"><img src="http://blog.unijimpe.net/btdown.png" border="0"></a></p><p style="font-size: 10px;border-top: 1px solid #666666;margin-top=12px;padding-top:6px;">&copy; 2006 - 2011 <a href="http://blog.unijimpe.net">unijimpe</a> - Utiliza este feed solo para uso personal, partes de este feed pueden ser utilizados mencionando al autor, no esta permitido publicar enteramente este feed para uso comercial sin permiso del autor.</p><br /> ]]></content:encoded> <wfw:commentRss>http://blog.unijimpe.net/php-con-mysql-eliminar-registros-en-mysql/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>Obtener thumbnails de los videos de Youtube</title><link>http://blog.unijimpe.net/obtener-thumbnails-de-los-videos-de-youtube/</link> <comments>http://blog.unijimpe.net/obtener-thumbnails-de-los-videos-de-youtube/#comments</comments> <pubDate>Mon, 04 Jul 2011 02:36:06 +0000</pubDate> <dc:creator>unijimpe</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Tools]]></category> <category><![CDATA[Youtube]]></category> <category><![CDATA[thumbnail]]></category> <category><![CDATA[video]]></category><guid isPermaLink="false">http://blog.unijimpe.net/?p=1281</guid> <description><![CDATA[<p>Al crear webs que muestren vídeos de Youtube surge la necesidad de mostrar las imágenes del vídeos, esto para los usuarios que no tienen flash o para dispositivos móviles. Obtener estas imágenes es muy sencillo, solo &#8230;</p>]]></description> <content:encoded><![CDATA[<p>Al crear webs que muestren vídeos de Youtube surge la necesidad de mostrar las imágenes del vídeos, esto para los usuarios que no tienen flash o para dispositivos móviles. Obtener estas imágenes es muy sencillo, solo hace falta tener el identificador del vídeo.</p><p><a href="http://samples.unijimpe.net/youtube-image.php"><img src="http://blog.unijimpe.net/wp-content/uploads/2011/07/php-youtube-image.jpg" alt="" title="php-youtube-image" width="400" height="286" /></a></p><p><strong>Obtener la ruta de las Imágenes</strong></p><p>La ruta de las imágenes de los vídeos están basadas en el identificador del video que lo representamos como <strong>ID</strong> en nuestros ejemplos.</p><p>La imagen utilizada por defecto utilizada por Youtube es:</p><div class="syntax_hilite"><div id="code-25"><div class="code"><ol><li style="color:#3A6A8B;"><div style="">http:<span style="color:#FF9933; font-style:italic;">//img.youtube.com/vi/ID/default.jpg </span></div></li></ol></div></div></div><p></p><p>Además se pueden obtener 4 imágenes adicionales, donde la primera es en alta calidad (<em>480x360 pixels</em>) y las siguientes en baja calidad (<em>120x90 pixels</em>).</p><div class="syntax_hilite"><div id="code-26"><div class="code"><ol><li style="color:#3A6A8B;"><div style="">http:<span style="color:#FF9933; font-style:italic;">//img.youtube.com/vi/ID/0.jpg</span></div></li><li style="color:#26536A;"><div style="">http:<span style="color:#FF9933; font-style:italic;">//img.youtube.com/vi/ID/1.jpg</span></div></li><li style="color:#3A6A8B;"><div style="">http:<span style="color:#FF9933; font-style:italic;">//img.youtube.com/vi/ID/2.jpg</span></div></li><li style="color:#26536A;"><div style="">http:<span style="color:#FF9933; font-style:italic;">//img.youtube.com/vi/ID/3.jpg </span></div></li></ol></div></div></div><p></p><p><strong>Obtener las imágenes de Forma Manual</strong></p><p>Lo primero que se necesita es el identificador del video, para ello nos fijamos en el URL del vídeo que deseamos e identificamos el parámetro GET con nombre <strong>v</strong>.</p><div class="syntax_hilite"><div id="code-27"><div class="code"><ol><li style="color:#3A6A8B;"><div style="">http:<span style="color:#FF9933; font-style:italic;">//www.youtube.com/watch?v=aDaOgu2CQtI </span></div></li></ol></div></div></div><p></p><p>En el URL de ejemplo tendríamos el identificador del video: <em>aDaOgu2CQtI</em>. Luego de ello si deseamos mostrar la imágen por defecto tendríamos:</p><div class="syntax_hilite"><div id="html-28"><div class="html"><ol><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">"http://img.youtube.com/vi/aDaOgu2CQtI/default.jpg"</span> /<span style="color: #000000;">&gt;</span></a></span></div></li></ol></div></div></div><p></p><p><strong>Obtener las Imágenes con PHP</strong></p><p>Lo que necesitamos en este caso es crear una función que devuelva el identificador del vídeo para posteriormente utilizarlo en la creación del URL de la imagen. Entonces creamos una función llamada <em>getYoutubeID</em> que parsea el URL ingresado y obtiene el Identificador y si la ruta es invalida devuelve una cadena vacía.</p><div class="syntax_hilite"><div id="php-29"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#000000;">function</span> getYoutubeID<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$url</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$tube</span> = <a href="http://www.php.net/parse_url"><span style="color:#000066;">parse_url</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$url</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$tube</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"path"</span><span style="color:#006600;">&#93;</span> == <span style="color:#FF0000;">"/watch"</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/parse_str"><span style="color:#000066;">parse_str</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$tube</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"query"</span><span style="color:#006600;">&#93;</span>, <span style="color:#0000FF;">$query</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$id</span> = <span style="color:#0000FF;">$query</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"v"</span><span style="color:#006600;">&#93;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#006600;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$id</span> = <span style="color:#FF0000;">""</span>;&nbsp; &nbsp;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#006600;">&#125;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$id</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Finalmente podemos mostrar el thumbnail del video en alta calidad de la siguiente forma:</p><div class="syntax_hilite"><div id="html-30"><div class="html"><ol><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php $url = <span style="color: #ff0000;">"http://www.youtube.com/watch?v=aDaOgu2CQtI"</span>; ?<span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">"http://img.youtube.com/vi/&lt;?php echo getYoutubeID($url); ?&gt;</span>/0.jpg&quot; /&gt; </span></div></li></ol></div></div></div><p></p><p>Para finalizar he creado una página de ejemplo: <a href="http://samples.unijimpe.net/youtube-image.php">PHP Youtube Image</a> donde pueden ingresar el URL del vídeo y visualizar todas las imágenes disponibles. Tambíen les dejo el código fuente de este ejemplo para que lo pueden utilizar libremente.</p><p style="text-align:left;"><a href="http://blog.unijimpe.net/download/youtube-image.zip" title="PHP Youtube Image"><img src="http://blog.unijimpe.net/btdown.png" border="0"></a></p><p style="font-size: 10px;border-top: 1px solid #666666;margin-top=12px;padding-top:6px;">&copy; 2006 - 2011 <a href="http://blog.unijimpe.net">unijimpe</a> - Utiliza este feed solo para uso personal, partes de este feed pueden ser utilizados mencionando al autor, no esta permitido publicar enteramente este feed para uso comercial sin permiso del autor.</p><br /> ]]></content:encoded> <wfw:commentRss>http://blog.unijimpe.net/obtener-thumbnails-de-los-videos-de-youtube/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Protege tu web del SPAM con PHP y Akismet</title><link>http://blog.unijimpe.net/protege-tu-web-del-spam-con-php-y-akismet/</link> <comments>http://blog.unijimpe.net/protege-tu-web-del-spam-con-php-y-akismet/#comments</comments> <pubDate>Tue, 31 May 2011 05:10:37 +0000</pubDate> <dc:creator>unijimpe</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Servicios Web]]></category> <category><![CDATA[akismet]]></category> <category><![CDATA[spam]]></category> <category><![CDATA[WordPress]]></category><guid isPermaLink="false">http://blog.unijimpe.net/?p=1271</guid> <description><![CDATA[<p><a href="http://akismet.com/">Akismet</a> el servicio que filtra los mensajes considerados como SPAM que es ampliamente utilizado en <strong>WordPress</strong> también tiene un API que permite utilizarlo en cualquier proyecto web. Esto permitirá hacer una revisión previa de los comentarios &#8230;</p>]]></description> <content:encoded><![CDATA[<p><a href="http://akismet.com/">Akismet</a> el servicio que filtra los mensajes considerados como SPAM que es ampliamente utilizado en <strong>WordPress</strong> también tiene un API que permite utilizarlo en cualquier proyecto web. Esto permitirá hacer una revisión previa de los comentarios enviados por los usuarios antes de aceptarlos y guardarlos en base de datos.</p><p><a href="http://akismet.com"><img src="http://blog.unijimpe.net/wp-content/uploads/2011/05/akismet.jpg" alt="" title="akismet" width="218" height="60" /></a></p><p><strong>Metodología para verificar el SPAM</strong></p><p>Antes de que cualquier mensaje enviado por el usuario sea publicado en nuestra página, este debe ser aprobado previamente por el administrador, esto para evitar que se publiquen mensajes no deseados.</p><p>Por ello es necesario primero verificar el mensaje si es considerado como spam por Akismet y posteriormente guardar estos datos en una base de datos para ser verificados por el administrador. Luego en el administrador de nuestro sitio debe haber un panel donde podríamos realizar las siguientes acciones:</p><ul><li><strong>Aprobar el Mensaje</strong>: el mensaje se publica y es visible en nuestra página.</li><li><strong>Marcar como SPAM</strong>: se reporta el contenido del mensaje como spam a los servidores de Akismet .</li><li><strong>Marcar como NO-SPAM</strong>: si el mensaje se ha detectado como spam, nosotros podemos revisarlo y marcar el mensaje como no-spam.</li></ul><p><strong>Clase PHP para utilizar Akismet</strong></p><p>Para interactuar con el API de Akismet se puede descargar el archivo <a href="http://www.epictrim.com/files/epictrim-akismet-class.zip">class.akismet.php</a> desarrollada por <a href="http://www.epictrim.com">epictrim</a> y que ofrece métodos sencillos para verificar el contenido ingresado por el usuario.</p><p>Para utilizar esta clase, incluimos el archivo <code>class.akismet.php</code> al inicio de nuestro script PHP, creamos una instancia de la clase y asignamos los siguientes parámetros.</p><ul><li><strong>apikey</strong>: es tu <em>API Key</em> de Akismet . (Obtén tu Key en <a href="https://akismet.com/signup/">API key signup</a>)</li><li><strong>name</strong>: es el nombre del usuario que envía el formulario.</li><li><strong>email</strong>: email del usuario que envía el formulario.</li><li><strong>message</strong>: mensaje que se desea verificar.</li><li><strong>type</strong>: tipo de formulario (<em>comment</em>, <em>trackback</em>, <em>pingback</em>, <em>registration</em>)</li></ul><p><strong>Verificando si el mensaje es SPAM</strong></p><p>Si se desea verificar si los datos enviados por el usuario se puede utilizar el método <em>isSpam</em> el cual devuelve verdadero en el caso que el mensaje sea considerado como spam en la base de datos de Akismet.</p><div class="syntax_hilite"><div id="php-34"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"class.akismet.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#FF9933; font-style:italic;">// creamos una instancia de la clase</span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span> = <span style="color:#000000;">new</span> Akismet<span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">apikey</span> = <span style="color:#FF0000;">"your-akismet-key"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">name</span> = <span style="color:#FF0000;">"Demo User"</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">email</span> = <span style="color:#FF0000;">"demo@example.com"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">message</span> = <span style="color:#FF0000;">"Hello my name is Akismet..."</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#FF9933; font-style:italic;">// verificamos el mensaje</span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">isSpam</span><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Mensaje Spam!!!"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#006600;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Mensaje Valido"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p><strong>Marcar un Mensaje como SPAM</strong></p><p>Akismet ofrece un método mediante el cual se puede reportar un mensaje como spam de tal forma que se registre en su base de datos para que posteriormente sea considerado automáticamente como spam. La clase con la que trabajamos ofrece el método <em>submitSpam</em> con el cual reportamos un mensaje como spam.</p><div class="syntax_hilite"><div id="php-35"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"class.akismet.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#FF9933; font-style:italic;">// creamos una instancia de la clase</span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span> = <span style="color:#000000;">new</span> Akismet<span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">apikey</span> = <span style="color:#FF0000;">"your-akismet-key"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">name</span> = <span style="color:#FF0000;">"Viagra"</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">email</span> = <span style="color:#FF0000;">"viagra@example.com"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">message</span> = <span style="color:#FF0000;">"Hello..."</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#FF9933; font-style:italic;">// marcamos el mensaje como spam</span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">submitSpam</span><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p><strong>Marcar el Mensaje como NO-SPAM</strong></p><p>En el caso que un mensaje ha sido considerado como spam por Akismet, nosotros podemos marcarlo como no-spam, para ello haremos uso del método <em>submitHam</em> de la siguiente forma.</p><div class="syntax_hilite"><div id="php-36"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"class.akismet.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#FF9933; font-style:italic;">// creamos una instancia de la clase</span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span> = <span style="color:#000000;">new</span> Akismet<span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">apikey</span> = <span style="color:#FF0000;">"your-akismet-key"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">name</span> = <span style="color:#FF0000;">"Valid Friend"</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">email</span> = <span style="color:#FF0000;">"friend-name@example.com"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">message</span> = <span style="color:#FF0000;">"Hello..."</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#FF9933; font-style:italic;">// marcamos el mensaje como no-spam</span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$akismet</span>-&gt;<span style="color:#006600;">submitHam</span><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Como se puede observar interactuar con Akismet es sencillo utilizando esta clase. Con los tres métodos ofrecidos podemos verificar si el mensaje es considerado con spam, podemos reportar spam y también desmarcar el spam.</p><p style="text-align:left;"><a href="http://blog.unijimpe.net/download/php-akismet.zip" title="PHP Akismet"><img src="http://blog.unijimpe.net/btdown.png" border="0"></a></p><p><strong>Más Información</strong></p><ul><li><a href="http://akismet.com/development/api/">Akismet - API documentation (v1.3)</a></li><li><a href="https://akismet.com/signup/">Akismet - API key signup - It's easy</a></li><li><a href="http://www.epictrim.com/ideas/stop-spam-akismet-php-class/">Stop SPAM with Akismet</a></li><li><a href="http://blog.unijimpe.net/utilizar-akismet-con-php/">Utilizar Akismet con PHP</a></li></ul><p style="font-size: 10px;border-top: 1px solid #666666;margin-top=12px;padding-top:6px;">&copy; 2006 - 2011 <a href="http://blog.unijimpe.net">unijimpe</a> - Utiliza este feed solo para uso personal, partes de este feed pueden ser utilizados mencionando al autor, no esta permitido publicar enteramente este feed para uso comercial sin permiso del autor.</p><br /> ]]></content:encoded> <wfw:commentRss>http://blog.unijimpe.net/protege-tu-web-del-spam-con-php-y-akismet/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Acortar URLs con PHP y Goo.gl</title><link>http://blog.unijimpe.net/acortar-urls-con-php-y-goo-gl/</link> <comments>http://blog.unijimpe.net/acortar-urls-con-php-y-goo-gl/#comments</comments> <pubDate>Mon, 21 Mar 2011 01:36:09 +0000</pubDate> <dc:creator>unijimpe</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[Google]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Servicios Web]]></category> <category><![CDATA[API]]></category> <category><![CDATA[class]]></category> <category><![CDATA[goo.gl]]></category> <category><![CDATA[key]]></category><guid isPermaLink="false">http://blog.unijimpe.net/?p=1259</guid> <description><![CDATA[<p><a href="http://goo.gl">Goo.gl</a> es el servicio de Google para acortar URLs semejante a Bit.ly que ofrece un API que permite acortar y expandir las URLs de manera sencilla. Además de ello ofrece estadísticas de visitas a nuestras URLs &#8230;</p>]]></description> <content:encoded><![CDATA[<p><a href="http://goo.gl">Goo.gl</a> es el servicio de Google para acortar URLs semejante a Bit.ly que ofrece un API que permite acortar y expandir las URLs de manera sencilla. Además de ello ofrece estadísticas de visitas a nuestras URLs acortadas para una análisis del rendimiento.</p><p><strong>Creando un API Key</strong><br /> El proceso completo para hacer uso del API esta explicado en <a href="http://code.google.com/apis/urlshortener/v1/getting_started.html">Getting Started - Google URL Shortener API</a>. Donde se explica que para hacer poder acortar las URLs es necesario tener un API Key. Este key es gratuito y para obtenerlo hay que seguir los siguientes pasos.</p><ul><li>Entra a <a href="http://code.google.com/apis/console/">Google API Console</a>.</li><li>Crea un nuevo proyecto y activa "<em>URL Shortener API</em>".</li><li>Click en el menú "<em>Identity</em>" y en la sección <em>Access Key</em> encontrarás tu key.</li></ul><p><strong>Creando la Clase para Goo.gl</strong></p><p>De acuerdo a la documentación creamos un clase en PHP con dos métodos <em>shorten</em> para acortar un URL y <em>expand</em> para expandir URLs.</p><div class="syntax_hilite"><div id="php-41"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#000000;">class</span> Googl <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#000000;">var</span> <span style="color:#0000FF;">$path</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#000000;">function</span> Googl<span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">path</span> = <span style="color:#FF0000;">"https://www.googleapis.com/urlshortener/v1"</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#006600;">&#125;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#000000;">function</span> shorten<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$url</span>, <span style="color:#0000FF;">$key</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$ch</span> = curl_init<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">path</span>.<span style="color:#FF0000;">"/url?key="</span>.<span style="color:#0000FF;">$key</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color:#000000;">true</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$ch</span>, CURLOPT_POST, <span style="color:#000000;">true</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$ch</span>, CURLOPT_HTTPHEADER, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">'Content-type: application/json'</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$ch</span>, CURLOPT_POSTFIELDS, json_encode<span style="color:#006600;">&#40;</span><a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">'longUrl'</span> =&gt; <span style="color:#0000FF;">$url</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$rpta</span> = curl_exec<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$ch</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$data</span> = json_decode<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$rpta</span>, <span style="color:#000000;">true</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; curl_close<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$ch</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$data</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"id"</span><span style="color:#006600;">&#93;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#006600;">&#125;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#000000;">function</span> expand<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$url</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$ch</span> = curl_init<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">path</span>.<span style="color:#FF0000;">"/url?shortUrl="</span>.<span style="color:#0000FF;">$url</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color:#000000;">true</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$rpta</span> = curl_exec<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$ch</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$data</span> = json_decode<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$rpta</span>, <span style="color:#000000;">true</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; curl_close<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$ch</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$data</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"longUrl"</span><span style="color:#006600;">&#93;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#006600;">&#125;</span>&nbsp; &nbsp;</div></li><li style="color:#26536A;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Este código lo guardamos en un archivo llamado <em>googl.php</em>.</p><p><strong>Utilizando la clase</strong></p><p>Para utilizar la clase lo primero es incluir la clase al inicio de nuestro archivo PHPy luego creamos una instancia del objeto para utilizarlo posteriormente.</p><div class="syntax_hilite"><div id="php-42"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"googl.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$googl</span> = <span style="color:#000000;">new</span> Googl<span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Entonces si deseamos acortar un URL haremos uso del método <em>shorten</em> el cual recibe dos parámetros el primero el URL que deseamos acortar y el segundo es tu Key.</p><div class="syntax_hilite"><div id="php-43"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"googl.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$googl</span> = <span style="color:#000000;">new</span> Googl<span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$urlmin</span> = <span style="color:#0000FF;">$googl</span>-&gt;<span style="color:#006600;">shorten</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"http://blog.unijimpe.net/tubeplayer/"</span>, <span style="color:#FF0000;">"tu-api-key"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"urlmin: "</span>.<span style="color:#0000FF;">$urlmin</span>;</div></li></ol></div></div></div><p></p><p>Si por el contrario deseamos expandir un URL ya acortado, haremos uso del método <em>expand</em> el cual recibe un único parámetro que el URL acortado que deseamos expandir.</p><div class="syntax_hilite"><div id="php-44"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"googl.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$googl</span> = <span style="color:#000000;">new</span> Googl<span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$urlmax</span> = <span style="color:#0000FF;">$googl</span>-&gt;<span style="color:#006600;">expand</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"http://goo.gl/E6pAi"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"urlmax: "</span>.<span style="color:#0000FF;">$urlmax</span>;</div></li></ol></div></div></div><p></p><p>Como se puede ver el uso de esta clase es muy sencilla, solo hay que indicar el URL que deseamos acortar junto con nuestro API y generaremos automáticamente la versión acortada del mismo. Además de ello esta URL se anexará a nuestra cuenta de Goo.gl y podremos ver las estadísticas de visitas.</p><p style="text-align:left;"><a href="http://blog.unijimpe.net/download/googl-class.zip" title="PHP Goo.gl Class"><img src="http://blog.unijimpe.net/btdown.png" border="0"></a></p><p style="font-size: 10px;border-top: 1px solid #666666;margin-top=12px;padding-top:6px;">&copy; 2006 - 2011 <a href="http://blog.unijimpe.net">unijimpe</a> - Utiliza este feed solo para uso personal, partes de este feed pueden ser utilizados mencionando al autor, no esta permitido publicar enteramente este feed para uso comercial sin permiso del autor.</p><br /> ]]></content:encoded> <wfw:commentRss>http://blog.unijimpe.net/acortar-urls-con-php-y-goo-gl/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>PHP con MySQL: Editar Registros con MySQL</title><link>http://blog.unijimpe.net/php-con-mysql-editar-registros-con-mysql/</link> <comments>http://blog.unijimpe.net/php-con-mysql-editar-registros-con-mysql/#comments</comments> <pubDate>Tue, 01 Mar 2011 03:53:54 +0000</pubDate> <dc:creator>unijimpe</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[consulta]]></category> <category><![CDATA[query]]></category> <category><![CDATA[select]]></category> <category><![CDATA[sql]]></category> <category><![CDATA[update]]></category><guid isPermaLink="false">http://blog.unijimpe.net/?p=1256</guid> <description><![CDATA[<p>Continuando con la serie de artículos para trabajar con datos de MySQL desde PHP, es el momento de editar los datos guardados en nuestras tablas. Para ello haremos uso de la sentencia UPDATE explicada anteriormente en &#8230;</p>]]></description> <content:encoded><![CDATA[<p>Continuando con la serie de artículos para trabajar con datos de MySQL desde PHP, es el momento de editar los datos guardados en nuestras tablas. Para ello haremos uso de la sentencia UPDATE explicada anteriormente en <a href="http://blog.unijimpe.net/php-con-mysql-introduccion-a-sql/">PHP con MySQL: Introducción a SQL</a>.</p><p><img src="http://blog.unijimpe.net/wp-content/uploads/2011/02/editar-mysql.gif" alt="" title="editar-mysql" width="313" height="163" /></p><p>El proceso será: primero mostrar un formulario con los datos que tenemos guardados, luego el usuario puede modificar los datos y enviar el formulario. Continuando, recibimos los datos enviados desde el formulario, con ello creamos la consulta SQL y lo enviamos a MySQL con la función <em>mysql_query</em>.</p><p><strong>Preparar el listado de datos</strong></p><p>Para nuestro ejemplo seguiremos utilizando la base de datos de ejemplo (Pueden descargar el <a href="http://samples.unijimpe.net/demo-empresa.sql">Script SQL</a>). Entonces haremos un listado de todos los registros de la tabla <em>empresa</em> y para cada registro crearemos un enlace hacia otra página donde se hará la edición de datos.</p><div class="syntax_hilite"><div id="html-50"><div class="html"><ol><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php</div></li><li style="color:#26536A;"><div style="">// creamos la conexión a mysql</div></li><li style="color:#3A6A8B;"><div style="">require<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"conexion.php"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">require<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"funciones.php"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">// hacemos la consulta de registros</div></li><li style="color:#26536A;"><div style="">$query = <span style="color: #ff0000;">"SELECT * FROM empresa ORDER BY nombre ASC"</span>;</div></li><li style="color:#3A6A8B;"><div style="">$queEmp = mysql_query<span style="color: #66cc66;">&#40;</span>$query, $conexion<span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">?<span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000;">&lt;html&gt;</span></a></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000;">&lt;head&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/title.html"><span style="color: #000000;">&lt;title&gt;</span></a></span>Listado de Empresas<span style="color: #009900;"><span style="color: #000000;">&lt;/title&gt;</span></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/head&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000;">&lt;body&gt;</span></a></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/table.html"><span style="color: #000000;">&lt;table&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php while <span style="color: #66cc66;">&#40;</span>$rsEmp = mysql_fetch_assoc<span style="color: #66cc66;">&#40;</span>$quEmp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> ?<span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#26536A;"><div style="">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000;">&lt;tr&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000;">&lt;td&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php echo $rsEmp<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'nombre'</span><span style="color: #66cc66;">&#93;</span>; ?<span style="color: #000000;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000;">&lt;/td&gt;</span></span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000;">&lt;td&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php echo $rsEmp<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'direccion'</span><span style="color: #66cc66;">&#93;</span>; ?<span style="color: #000000;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000;">&lt;/td&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000;">&lt;td&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/a.html"><span style="color: #000000;">&lt;a</span></a> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">"editar.php?id=&lt;?php echo $rsEmp['id']; ?&gt;</span>&quot;&gt;Editar<span style="color: #009900;">&lt;/a&gt;</span><span style="color: #009900;">&lt;/td&gt;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; <span style="color: #009900;">&lt;/tr&gt;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; <span style="color: #009900;">&lt;?php } ?&gt;</span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;">&lt;/table&gt;</span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;">&lt;/body&gt;</span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;">&lt;/html&gt;</span> </span></div></li></ol></div></div></div><p></p><p>Como se puede ver hemos creado un enlace al archivo <em>editar.php</em> al cual le pasamos como parámetro GET el identificador del registro.</p><p><strong>Consultando los detalles del Registro</strong></p><p>El siguiente paso es obtener los datos del registro seleccionado, para ello utilizamos como filtro el identificador del registro el cual lo estamos pasando como parámetro GET.</p><div class="syntax_hilite"><div id="php-51"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// editar.php</span></div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"conexion.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"funciones.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$idempresa</span> = getParam<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"id"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">"-1"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#FF9933; font-style:italic;">// consultamos los datos</span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$sql</span> = <span style="color:#FF0000;">"SELECT * FROM empresa WHERE id = "</span>.sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$idempresa</span>, <span style="color:#FF0000;">"int"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$queEmp</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$sql</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$rsEmp</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color:#000066;">mysql_fetch_assoc</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$queEmp</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$total</span> = <a href="http://www.php.net/mysql_num_rows"><span style="color:#000066;">mysql_num_rows</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$queEmp</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$total</span> == <span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// si no hay datos redireccionamos al listado</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"location: listado.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Notar que estamos utilizando la función creada anteriormente <em>sqlValue</em>, la cual formatea los datos para ser compatibles con el tipo de dato aceptado por la base de datos. En el ejemplo el id lo pasamos como un número entero. Finalmente antes de mostrar los datos verificamos que nuestra consulta ha devuelto resultados, si no hay resultados, el registro no existe y redireccionamos a la lista de empresas.</p><p><strong>Creando el Formulario</strong></p><p>Ahora mostramos el formulario con los datos recién consultados, para ello imprimimos los datos de la variable <em>$rsEmp</em> y accedemos a cada campo como si fuera un array:</p><div class="syntax_hilite"><div id="html-52"><div class="html"><ol><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/form.html"><span style="color: #000000;">&lt;form</span></a> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">"post"</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">"editar.php?action=edit"</span><span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000;">&lt;strong&gt;</span></a></span>Nombre<span style="color: #009900;"><span style="color: #000000;">&lt;/strong&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"nombre"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"&lt;?php echo $rsEmp['nombre']; ?&gt;</span>&quot; /&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;">&lt;strong&gt;</span>Dirección<span style="color: #009900;">&lt;/strong&gt;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;">&lt;input type="</span><span style="color: #000066;">text</span><span style="color: #ff0000;">" name="</span>direccion<span style="color: #ff0000;">" value="</span><span style="color: #000000;">&lt;</span></a>?php echo $rsEmp<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'direccion'</span><span style="color: #66cc66;">&#93;</span>; ?<span style="color: #000000;">&gt;</span></a></span>&quot; /&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000;">&lt;strong&gt;</span></a></span>Telefono<span style="color: #009900;"><span style="color: #000000;">&lt;/strong&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"telefono"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"&lt;?php echo $rsEmp['telefono']; ?&gt;</span>&quot; /&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;">&lt;input type="</span>hidden<span style="color: #ff0000;">" id="</span><span style="color: #000066;">id</span><span style="color: #ff0000;">" name="</span><span style="color: #000066;">id</span><span style="color: #ff0000;">" value="</span><span style="color: #000000;">&lt;</span></a>?php echo $rsEmp<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"id"</span><span style="color: #66cc66;">&#93;</span>; ?<span style="color: #000000;">&gt;</span></a></span>&quot; /&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/button.html"><span style="color: #000000;">&lt;button</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"submit"</span><span style="color: #000000;">&gt;</span></a></span>Guardar<span style="color: #009900;"><span style="color: #000000;">&lt;/button&gt;</span></span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/button.html"><span style="color: #000000;">&lt;button</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"reset"</span><span style="color: #000000;">&gt;</span></a></span>Limpiar<span style="color: #009900;"><span style="color: #000000;">&lt;/button&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/form&gt;</span></span></div></li></ol></div></div></div><p></p><p>Como se puede ver, hemos incluido un campo oculto con el identificador del registro, es oculto por que este dato no se puede editar y nos servirá como condición para actualizar los datos.</p><p><strong>Guardando los Datos</strong></p><p>El paso final es recibir los datos del formulario, crear la consulta para actualizar los datos. Para ello primero recogemos la variable <em>action</em> y verificamos si se esta intentando editar.</p><div class="syntax_hilite"><div id="php-53"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$action</span> = getParam<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"action"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">""</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$action</span> == <span style="color:#FF0000;">"edit"</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$idempresa</span> = sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"id"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">"int"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$nombre</span> = sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"nombre"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">"text"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$direccion</span> = sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"direccion"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">"text"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$telefono</span> = sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"telefono"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">"int"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$sql</span> = <span style="color:#FF0000;">"UPDATE empresa SET "</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$sql</span>.= <span style="color:#FF0000;">"nombre="</span>.<span style="color:#0000FF;">$nombre</span>.<span style="color:#FF0000;">", direccion="</span>.<span style="color:#0000FF;">$direccion</span>.<span style="color:#FF0000;">", telefono="</span>.<span style="color:#0000FF;">$telefono</span>.<span style="color:#FF0000;">" "</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$sql</span>.= <span style="color:#FF0000;">"WHERE id="</span>.<span style="color:#0000FF;">$idempresa</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$sql</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"location: listado.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Lo único que hemos hecho es crear la consulta en base a los datos recibidos del formulario, luego de ello redireccionamos al listado de registros.</p><p><strong>Uniendo todos los elementos</strong></p><p>Unimos todo el código para obtener el script para editar registros.</p><div class="syntax_hilite"><div id="html-54"><div class="html"><ol><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;</span></a>?php</div></li><li style="color:#26536A;"><div style="">require<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"conexion.php"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">require<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"funciones.php"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp;</div></li><li style="color:#3A6A8B;"><div style="">$idempresa = getParam<span style="color: #66cc66;">&#40;</span>$_GET<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"id"</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">"-1"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">$action = getParam<span style="color: #66cc66;">&#40;</span>$_GET<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"action"</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">""</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp;</div></li><li style="color:#26536A;"><div style="">if <span style="color: #66cc66;">&#40;</span>$action == <span style="color: #ff0000;">"edit"</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; $idempresa = getParam<span style="color: #66cc66;">&#40;</span>$_POST<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"id"</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">"-1"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; $nombre = sqlValue<span style="color: #66cc66;">&#40;</span>$_POST<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"nombre"</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">"text"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; $direccion = sqlValue<span style="color: #66cc66;">&#40;</span>$_POST<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"direccion"</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">"text"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; $telefono = sqlValue<span style="color: #66cc66;">&#40;</span>$_POST<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"telefono"</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">"int"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; $sql = <span style="color: #ff0000;">"UPDATE empresa SET "</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; $sql.= <span style="color: #ff0000;">"nombre="</span>.$nombre.<span style="color: #ff0000;">", direccion="</span>.$direccion.<span style="color: #ff0000;">", telefono="</span>.$telefono.<span style="color: #ff0000;">" "</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; $sql.= <span style="color: #ff0000;">"WHERE id="</span>.$idempresa;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; mysql_query<span style="color: #66cc66;">&#40;</span>$sql, $conexion<span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; header<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"location: listado.php"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #66cc66;">&#125;</span></div></li><li style="color:#26536A;"><div style="">&nbsp;</div></li><li style="color:#3A6A8B;"><div style="">$sql = <span style="color: #ff0000;">"SELECT * FROM empresa WHERE id = "</span>.sqlValue<span style="color: #66cc66;">&#40;</span>$idempresa, <span style="color: #ff0000;">"int"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">$queEmp = mysql_query<span style="color: #66cc66;">&#40;</span>$sql, $conexion<span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">$rsEmp = mysql_fetch_assoc<span style="color: #66cc66;">&#40;</span>$queEmp<span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">$total = mysql_num_rows<span style="color: #66cc66;">&#40;</span>$queEmp<span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">if <span style="color: #66cc66;">&#40;</span>$total == <span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; header<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"location: listado.php"</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; exit;</div></li><li style="color:#26536A;"><div style=""><span style="color: #66cc66;">&#125;</span></div></li><li style="color:#3A6A8B;"><div style="">?<span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000;">&lt;html&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000;">&lt;head&gt;</span></a></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/title.html"><span style="color: #000000;">&lt;title&gt;</span></a></span>Editar Empresa<span style="color: #009900;"><span style="color: #000000;">&lt;/title&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/head&gt;</span></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000;">&lt;body&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/h3.html"><span style="color: #000000;">&lt;h3&gt;</span></a></span>Editar Empresa<span style="color: #009900;"><span style="color: #000000;">&lt;/h3&gt;</span></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/form.html"><span style="color: #000000;">&lt;form</span></a> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">"post"</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">"editar.php?action=edit"</span><span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000;">&lt;strong&gt;</span></a></span>Nombre<span style="color: #009900;"><span style="color: #000000;">&lt;/strong&gt;</span></span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"nombre"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"&lt;?php echo $rsEmp['nombre']; ?&gt;</span>&quot; /&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;">&lt;strong&gt;</span>Dirección<span style="color: #009900;">&lt;/strong&gt;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;">&lt;input type="</span><span style="color: #000066;">text</span><span style="color: #ff0000;">" name="</span>direccion<span style="color: #ff0000;">" value="</span><span style="color: #000000;">&lt;</span></a>?php echo $rsEmp<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'direccion'</span><span style="color: #66cc66;">&#93;</span>; ?<span style="color: #000000;">&gt;</span></a></span>&quot; /&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000;">&lt;strong&gt;</span></a></span>Telefono<span style="color: #009900;"><span style="color: #000000;">&lt;/strong&gt;</span></span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"telefono"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"&lt;?php echo $rsEmp['telefono']; ?&gt;</span>&quot; /&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;">&lt;input type="</span>hidden<span style="color: #ff0000;">" id="</span><span style="color: #000066;">id</span><span style="color: #ff0000;">" name="</span><span style="color: #000066;">id</span><span style="color: #ff0000;">" value="</span><span style="color: #000000;">&lt;</span></a>?php echo $rsEmp<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"id"</span><span style="color: #66cc66;">&#93;</span>; ?<span style="color: #000000;">&gt;</span></a></span>&quot; /&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/button.html"><span style="color: #000000;">&lt;button</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"submit"</span><span style="color: #000000;">&gt;</span></a></span>Guardar<span style="color: #009900;"><span style="color: #000000;">&lt;/button&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/button.html"><span style="color: #000000;">&lt;button</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"reset"</span><span style="color: #000000;">&gt;</span></a></span>Limpiar<span style="color: #009900;"><span style="color: #000000;">&lt;/button&gt;</span></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/form&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/body&gt;</span></span></div></li><li style="color:#26536A;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/html&gt;</span></span></div></li></ol></div></div></div><p></p><p>Como se puede ver, editar datos de MySQL es sencillo, hay que crear la consulta y para ello concatenamos los datos enviados desde el formulario. En el ejemplo solo tenemos tres campos pero podríamos aplicarlo a cualquier cantidad de campos.</p><p style="text-align:left;"><a href="http://blog.unijimpe.net/download/update-mysql.zip" title="Update MySQL"><img src="http://blog.unijimpe.net/btdown.png" border="0"></a></p><p style="font-size: 10px;border-top: 1px solid #666666;margin-top=12px;padding-top:6px;">&copy; 2006 - 2011 <a href="http://blog.unijimpe.net">unijimpe</a> - Utiliza este feed solo para uso personal, partes de este feed pueden ser utilizados mencionando al autor, no esta permitido publicar enteramente este feed para uso comercial sin permiso del autor.</p><br /> ]]></content:encoded> <wfw:commentRss>http://blog.unijimpe.net/php-con-mysql-editar-registros-con-mysql/feed/</wfw:commentRss> <slash:comments>18</slash:comments> </item> <item><title>PHP con MySQL: Consultar datos en MySQL</title><link>http://blog.unijimpe.net/php-con-mysql-consultar-datos-en-mysql/</link> <comments>http://blog.unijimpe.net/php-con-mysql-consultar-datos-en-mysql/#comments</comments> <pubDate>Thu, 03 Feb 2011 05:50:40 +0000</pubDate> <dc:creator>unijimpe</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[buscador]]></category> <category><![CDATA[consulta]]></category> <category><![CDATA[like]]></category> <category><![CDATA[query]]></category> <category><![CDATA[select]]></category> <category><![CDATA[sql]]></category><guid isPermaLink="false">http://blog.unijimpe.net/?p=1249</guid> <description><![CDATA[<p>Consultar o extraer datos es una de las tareas mas comunes al interactuar con bases de datos, de esta forma podemos extraer los datos previamente almacenados, por ejemplo para mostrar la lista de productos de una &#8230;</p>]]></description> <content:encoded><![CDATA[<p>Consultar o extraer datos es una de las tareas mas comunes al interactuar con bases de datos, de esta forma podemos extraer los datos previamente almacenados, por ejemplo para mostrar la lista de productos de una tienda online o también para un buscador de documentos o el login de usuarios.</p><p><strong>Requerimientos</strong></p><p>Antes de continuar, es necesario tener una conexión abierta a MySQL, para ello pueden leer <a href="http://blog.unijimpe.net/php-con-mysql-crear-una-conexion-a-mysql/">Crear una conexión a MySQL</a> y luego crear la base de datos de ejemplo con el siguiente <a href="http://samples.unijimpe.net/demo-empresa.sql">Script SQL</a>.</p><p><strong>Extrayendo datos de MySQL</strong></p><p>Para extraer datos utilizaremos la función <em>mysql_query</em> mediante la cual enviamos un consulta SQL. Entonce primero incluimos el archivo para crear la conexión e inmediatamente creamos la consulta para extraer todos los datos de las empresas en nuestra base de datos.</p><div class="syntax_hilite"><div id="php-64"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// incluimos la conexión</span></div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"conexion.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// creamos la consulta</span></div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$query</span> = <span style="color:#FF0000;">"SELECT * FROM empresa ORDER BY nombre ASC"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// enviamos la consulta a MySQL</span></div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$queEmp</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$query</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>También se puede agregar una sentencia para mostrar si hay algún error en la conexión o consulta.</p><div class="syntax_hilite"><div id="php-65"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"conexion.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$query</span> = <span style="color:#FF0000;">"SELECT * FROM empresa ORDER BY nombre ASC"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$queEmp</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$query</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span> or <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color:#000066;">mysql_error</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p><strong>Mostrando los Datos</strong></p><p>El siguiente paso es mostrar los datos extraídos en la consulta, para ello podemos utilizar <em>mysql_fetch_assoc</em> el cual devolverá un array asociativo con el siguiente registro devuelto por la consulta. Entonces para mostrar todos los registros devueltos por la consulta se tendría:</p><div class="syntax_hilite"><div id="php-66"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">while</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$resEmp</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color:#000066;">mysql_fetch_assoc</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$queEmp</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp;<a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$resEmp</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">'nombre'</span><span style="color:#006600;">&#93;</span>.<span style="color:#FF0000;">"&lt;br&gt;"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Donde se puede ver que para acceder a cada campo del registro lo hacemos como un array donde le pasamos el nombre del campo que deseamos acceder (En el ejemplo estamos mostrando todos los nombres de las empresas consultadas).</p><p>Ahora, solo depende como deseamos mostrar los datos para modificar nuestro script. Por ejemplo si deseamos mostrar los datos como una lista tendríamos</p><div class="syntax_hilite"><div id="php-67"><div class="php"><ol><li style="color:#3A6A8B;"><div style="">&lt;ul&gt;</div></li><li style="color:#26536A;"><div style=""><span style="color:#000000;">&lt;?php</span> <span style="color:#616100;">while</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$rsEmp</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color:#000066;">mysql_fetch_assoc</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$quEmp</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span> <span style="color:#000000;">?&gt;</span></div></li><li style="color:#3A6A8B;"><div style="">&lt;li&gt;&lt;?php <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$rsEmp</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">'nombre'</span><span style="color:#006600;">&#93;</span>; ?&gt;&lt;/li&gt;</div></li><li style="color:#26536A;"><div style=""><span style="color:#000000;">&lt;?php</span> <span style="color:#006600;">&#125;</span> <span style="color:#000000;">?&gt;</span></div></li><li style="color:#3A6A8B;"><div style="">&lt;/ul&gt;</div></li></ol></div></div></div><p></p><p>Ahora también podríamos mostrar los resultados en una tabla o datagrid, para ello modificamos nuestro código donde utilizaremos una tabla para mostrar en varias columnas los datos consultados.</p><div class="syntax_hilite"><div id="php-68"><div class="php"><ol><li style="color:#3A6A8B;"><div style="">&lt;table&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &lt;tr&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &lt;th&gt;Nombre&lt;/th&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &lt;th&gt;Dirección&lt;/th&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &lt;th&gt;Teléfono&lt;/th&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &lt;/tr&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; <span style="color:#000000;">&lt;?php</span> <span style="color:#616100;">while</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$rsEmp</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color:#000066;">mysql_fetch_assoc</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$quEmp</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span> <span style="color:#000000;">?&gt;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &lt;tr&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &lt;td&gt;&lt;?php <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$rsEmp</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">'nombre'</span><span style="color:#006600;">&#93;</span>; ?&gt;&lt;/td&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &lt;td&gt;&lt;?php <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$rsEmp</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">'direccion'</span><span style="color:#006600;">&#93;</span>; ?&gt;&lt;/td&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &lt;td&gt;&lt;?php <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$rsEmp</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">'telefono'</span><span style="color:#006600;">&#93;</span>; ?&gt;&lt;/td&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &lt;/tr&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; <span style="color:#000000;">&lt;?php</span> <span style="color:#006600;">&#125;</span> <span style="color:#000000;">?&gt;</span></div></li><li style="color:#26536A;"><div style="">&lt;/table&gt;</div></li></ol></div></div></div><p></p><p><strong>Crear un Buscador</strong></p><p>Ahora, podríamos crear un buscador básico de empresas, para ello creamos un formulario para ingresar el criterio de búsqueda.</p><div class="syntax_hilite"><div id="php-69"><div class="php"><ol><li style="color:#3A6A8B;"><div style="">&lt;form id=<span style="color:#FF0000;">"frbuscar"</span> method=<span style="color:#FF0000;">"get"</span> action=<span style="color:#FF0000;">""</span>&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &lt;input name=<span style="color:#FF0000;">"criterio"</span> id=<span style="color:#FF0000;">"criterio"</span> type=<span style="color:#FF0000;">"text"</span> /&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &lt;input type=<span style="color:#FF0000;">"submit"</span> id=<span style="color:#FF0000;">"btbuscar"</span> value=<span style="color:#FF0000;">"Buscar"</span> /&gt;</div></li><li style="color:#26536A;"><div style="">&lt;/form&gt;</div></li></ol></div></div></div><p></p><p>Antes de continuar decidimos el tipo de consulta SQL que vamos a utilizar, en nuestro caso la búsqueda será por las primeras letras del nombre, para ello utilizaremos la sentencia LIKE explicada en el artículo <a href="http://blog.unijimpe.net/php-con-mysql-introduccion-a-sql/">PHP con MySQL: Introducción a SQL</a>.</p><div class="syntax_hilite"><div id="sql-70"><div class="sql"><ol><li style="color:#3A6A8B;"><div style=""><span style="color: #993333;">SELECT</span> * <span style="color: #993333;">FROM</span> empresa <span style="color: #993333;">WHERE</span> nombre <span style="color: #993333;">LIKE</span> <span style="color: #ff0000;">'A%'</span></div></li></ol></div></div></div><p></p><p>Luego, una vez enviado el formulario procedemos a recuperar los datos para proceder a crear la consulta y enviarla a MySQL. Primero verificamos que se ha enviado un criterio de búsqueda luego creamos la consulta no sin antes normalizar los datos (para prevenir ataques de inyección SQL), esto lo hacemos con la función sqlValue que hemos creado en <a href="http://blog.unijimpe.net/php-con-mysql-insertar-datos-en-mysql/">PHP con MySQL: Insertar datos en MySQL</a>.</p><div class="syntax_hilite"><div id="php-71"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"conexion.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"funciones.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$criterio</span> = getParam<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"criterio"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">""</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$total</span> = <span style="color:#CC66CC;color:#800000;">0</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$criterio</span> != <span style="color:#FF0000;">""</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$slike</span> = sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$criterio</span>.<span style="color:#FF0000;">"%"</span>, <span style="color:#FF0000;">"text"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$query</span> = <span style="color:#FF0000;">"SELECT * FROM empresa WHERE nombre like "</span>.<span style="color:#0000FF;">$slike</span>.<span style="color:#FF0000;">" ORDER BY nombre ASC"</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$queEmp</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$query</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$total</span> = <a href="http://www.php.net/mysql_num_rows"><span style="color:#000066;">mysql_num_rows</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$queEmp</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Nótese que estamos utilizando una variable <em>$total</em> donde se almacena el total de resultados devueltos por la consulta. El total de resultados se obtiene con la función <em>mysql_num_rows</em>.</p><p>Finalmente mostramos los resultados al cliente, para ello verificamos que existan resultados en la consulta. Uniendo todos los elementos tendríamos:</p><div class="syntax_hilite"><div id="php-72"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#000000;">&lt;?php</span></div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"conexion.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"funciones.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$criterio</span> = getParam<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"criterio"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">""</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$total</span> = <span style="color:#CC66CC;color:#800000;">0</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$criterio</span> != <span style="color:#FF0000;">""</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$slike</span> = sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$criterio</span>.<span style="color:#FF0000;">"%"</span>, <span style="color:#FF0000;">"text"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$query</span> = <span style="color:#FF0000;">"SELECT * FROM empresa WHERE nombre like "</span>.<span style="color:#0000FF;">$slike</span>.<span style="color:#FF0000;">" ORDER BY nombre ASC"</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$queEmp</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$query</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$total</span> = <a href="http://www.php.net/mysql_num_rows"><span style="color:#000066;">mysql_num_rows</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$queEmp</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#006600;">&#125;</span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#000000;">?&gt;</span></div></li><li style="color:#26536A;"><div style="">&lt;html&gt;</div></li><li style="color:#3A6A8B;"><div style="">&lt;head&gt;</div></li><li style="color:#26536A;"><div style="">&lt;title&gt;Consultar datos en MySQL&lt;/title&gt;</div></li><li style="color:#3A6A8B;"><div style="">&lt;/head&gt;</div></li><li style="color:#26536A;"><div style="">&lt;body&gt;</div></li><li style="color:#3A6A8B;"><div style="">&lt;h3&gt;Buscador de&nbsp; Empresas&lt;/h3&gt;</div></li><li style="color:#26536A;"><div style="">&lt;form id=<span style="color:#FF0000;">"frbuscar"</span> method=<span style="color:#FF0000;">"get"</span> action=<span style="color:#FF0000;">""</span>&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &lt;input name=<span style="color:#FF0000;">"criterio"</span> id=<span style="color:#FF0000;">"criterio"</span> type=<span style="color:#FF0000;">"text"</span> /&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &lt;input type=<span style="color:#FF0000;">"submit"</span> id=<span style="color:#FF0000;">"btbuscar"</span> value=<span style="color:#FF0000;">"Buscar"</span> /&gt;</div></li><li style="color:#3A6A8B;"><div style="">&lt;/form&gt;</div></li><li style="color:#26536A;"><div style=""><span style="color:#000000;">&lt;?php</span> <span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$total</span>&gt; <span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span> <span style="color:#000000;">?&gt;</span></div></li><li style="color:#3A6A8B;"><div style="">&lt;em&gt;Total de Resultados: <span style="color:#000000;">&lt;?php</span> <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$total</span>; ?&gt;&lt;/em&gt;</div></li><li style="color:#26536A;"><div style="">&lt;table&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &lt;tr&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &lt;th&gt;Nombre&lt;/th&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &lt;th&gt;Dirección&lt;/th&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &lt;th&gt;Teléfono&lt;/th&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &lt;/tr&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; <span style="color:#000000;">&lt;?php</span> <span style="color:#616100;">while</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$rsEmp</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color:#000066;">mysql_fetch_assoc</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$quEmp</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span> <span style="color:#000000;">?&gt;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &lt;tr&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &lt;td&gt;&lt;?php <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$rsEmp</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">'nombre'</span><span style="color:#006600;">&#93;</span>; ?&gt;&lt;/td&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &lt;td&gt;&lt;?php <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$rsEmp</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">'direccion'</span><span style="color:#006600;">&#93;</span>; ?&gt;&lt;/td&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &lt;td&gt;&lt;?php <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$rsEmp</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">'telefono'</span><span style="color:#006600;">&#93;</span>; ?&gt;&lt;/td&gt;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &lt;/tr&gt;</div></li><li style="color:#26536A;"><div style="">&nbsp; <span style="color:#000000;">&lt;?php</span> <span style="color:#006600;">&#125;</span> <span style="color:#000000;">?&gt;</span></div></li><li style="color:#3A6A8B;"><div style="">&lt;/table&gt;</div></li><li style="color:#26536A;"><div style=""><span style="color:#000000;">&lt;?php</span> <span style="color:#006600;">&#125;</span> <span style="color:#000000;">?&gt;</span></div></li><li style="color:#3A6A8B;"><div style="">&lt;p&gt;&amp;nbsp;&lt;/p&gt;</div></li><li style="color:#26536A;"><div style="">&lt;/body&gt;</div></li><li style="color:#3A6A8B;"><div style="">&lt;/html&gt;</div></li></ol></div></div></div><p></p><p>Como pueden ver no es complicado hacer consultas a MySQL, solo debemos crear la cadena con la consulta SQL y luego enviarla utilizando la función <em>mysql_query</em>. Lo siguiente solo depende de como deseamos mostrar la información resultante de nuestra consulta.</p><p>Para finalizar les dejo los archivos fuente de los ejemplos desarrollados en este artículo para que puedan hacer sus pruebas, seguir experimentando y completar su aprendizaje.</p><p style="text-align:left;"><a href="http://blog.unijimpe.net/download/php-mysql-select.zip" title="PHP y MYSQL - Select"><img src="http://blog.unijimpe.net/btdown.png" border="0"></a></p><p style="font-size: 10px;border-top: 1px solid #666666;margin-top=12px;padding-top:6px;">&copy; 2006 - 2011 <a href="http://blog.unijimpe.net">unijimpe</a> - Utiliza este feed solo para uso personal, partes de este feed pueden ser utilizados mencionando al autor, no esta permitido publicar enteramente este feed para uso comercial sin permiso del autor.</p><br /> ]]></content:encoded> <wfw:commentRss>http://blog.unijimpe.net/php-con-mysql-consultar-datos-en-mysql/feed/</wfw:commentRss> <slash:comments>15</slash:comments> </item> <item><title>PHP con MySQL: Insertar datos en MySQL</title><link>http://blog.unijimpe.net/php-con-mysql-insertar-datos-en-mysql/</link> <comments>http://blog.unijimpe.net/php-con-mysql-insertar-datos-en-mysql/#comments</comments> <pubDate>Mon, 13 Dec 2010 04:16:30 +0000</pubDate> <dc:creator>unijimpe</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[base de datos]]></category> <category><![CDATA[insert]]></category> <category><![CDATA[sql]]></category> <category><![CDATA[validar]]></category><guid isPermaLink="false">http://blog.unijimpe.net/?p=1251</guid> <description><![CDATA[<p>Continuando con la serie de artículos dedicados a explicar como trabajar PHP con MySQL, ahora nos toca explicar como insertar datos o registros en nuestras tablas de MySQL.</p><p><strong>Requerimientos</strong></p><p>Para guardar datos en MySQL es necesario &#8230;</p>]]></description> <content:encoded><![CDATA[<p>Continuando con la serie de artículos dedicados a explicar como trabajar PHP con MySQL, ahora nos toca explicar como insertar datos o registros en nuestras tablas de MySQL.</p><p><strong>Requerimientos</strong></p><p>Para guardar datos en MySQL es necesario primero <a href="http://blog.unijimpe.net/php-con-mysql-crear-una-conexion-a-mysql/">crear una conexión con MySQL</a>, también necesitamos una base de datos de ejemplo el cual pueden crear con el siguiente <a href="http://samples.unijimpe.net/demo-empresa.sql">script SQL</a> y finalmente <a href="http://blog.unijimpe.net/php-con-mysql-introduccion-a-sql/">conocimientos básicos de SQL</a>.</p><h3>Conceptos Básicos</h3><p>PHP provee funciones nativas que permiten interactuar con MySQL, para el proceso de insertar registros utilizaremos las siguientes funciones.</p><ul><li><strong>mysql_query</strong> permite enviar cualquier consulta SQL (<em>SELECT</em>, <em>INSERT</em>, <em>UPDATE</em>, etc.) desde PHP.</li><li><strong>mysql_insert_id</strong> devuelve el identificador del último registro ingresado.</li></ul><p>Entonces si deseamos insertar una nueva empresa en nuestra tabla de ejemplo tendríamos:</p><div class="syntax_hilite"><div id="php-79"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// abrimos la conexión</span></div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"conexion.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// insertarmos el registro</span></div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"INSERT INTO empresa (nombre, direccion) VALUES ('Apple', '1 Infinite Loop')"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// mostramos el ID del registro</span></div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <a href="http://www.php.net/mysql_insert_id"><span style="color:#000066;">mysql_insert_id</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Como pueden ver la creación de datos es sencilla, solo necesitamos crear la consulta adecuada y la enviamos mediante <em>mysql_query</em>.</p><h3>Ejemplo Práctico</h3><p>Veamos un ejemplo práctico, donde mediante un formulario en HTML registraremos los datos de las empresas.</p><p><strong>Creando el Formulario</strong></p><p>Para nuestro ejemplo creamos un formulario con tres campos (nombre, dirección y teléfono) con dos botones uno para enviar los datos y otro para limpiar el formulario.</p><div class="syntax_hilite"><div id="html-80"><div class="html"><ol><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/form.html"><span style="color: #000000;">&lt;form</span></a> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">"post"</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">"agregar-empresa.php"</span><span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; Nombre&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"nombre"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"nombre"</span> /<span style="color: #000000;">&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/br.html"><span style="color: #000000;">&lt;br</span></a> /<span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; Direcci<span style="color: #ddbb00;">&amp;oacute;</span>n&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"direccion"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"direccion"</span> /<span style="color: #000000;">&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/br.html"><span style="color: #000000;">&lt;br</span></a> /<span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; Tel<span style="color: #ddbb00;">&amp;eacute;</span>fono&nbsp; &nbsp;<span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"telefono"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"telefono"</span> /<span style="color: #000000;">&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/br.html"><span style="color: #000000;">&lt;br</span></a> /<span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/button.html"><span style="color: #000000;">&lt;button</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"submit"</span><span style="color: #000000;">&gt;</span></a></span>Guardar<span style="color: #009900;"><span style="color: #000000;">&lt;/button&gt;</span></span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/button.html"><span style="color: #000000;">&lt;button</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"reset"</span><span style="color: #000000;">&gt;</span></a></span>Limpiar<span style="color: #009900;"><span style="color: #000000;">&lt;/button&gt;</span></span></div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><span style="color: #000000;">&lt;/form&gt;</span></span></div></li></ol></div></div></div><p></p><p><strong>Procesando los Datos</strong></p><p>El siguiente paso, es verificar si se están enviando datos desde un formulario para luego crear la consulta concatenando los datos enviados para finalmente guardar los datos con la función <em>mysql_query</em>.</p><div class="syntax_hilite"><div id="php-81"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// file: insert-demo2.php</span></div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"conexion.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$status</span> = <span style="color:#FF0000;">""</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span><a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"nombre"</span><span style="color:#006600;">&#93;</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$nombre</span> = <span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"nombre"</span><span style="color:#006600;">&#93;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$direccion</span> = <span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"direccion"</span><span style="color:#006600;">&#93;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$telefono</span> = <span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"telefono"</span><span style="color:#006600;">&#93;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Creamos la consulta&nbsp; </span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$sql</span> = <span style="color:#FF0000;">"INSERT INTO empresa (nombre, direccion, telefono) "</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$sql</span>.= <span style="color:#FF0000;">"VALUES ('"</span>.<span style="color:#0000FF;">$nombre</span>.<span style="color:#FF0000;">"', '"</span>.<span style="color:#0000FF;">$direccion</span>.<span style="color:#FF0000;">"', '"</span>.<span style="color:#0000FF;">$telefono</span>.<span style="color:#FF0000;">"')"</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// enviamos la consulta</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$sql</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$status</span> = <span style="color:#FF0000;">"ok"</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Uniendo el código obtenemos nuestro primer ejemplo funcionando para agregar registros desde un formulario.</p><p><strong>Filtrando los Datos</strong></p><p>El ejemplo anterior, funciona pero tiene un problema y es que el usuario podría haber ingresado datos inválidos (Por ejemplo texto donde solo se aceptan números) o también enviar código SQL para atacar a nuestra base de datos. Por ello antes de utilizar los datos enviados por el usuario es necesario antes normalizar estos datos.</p><p>Creamos una función llamada <strong>sqlValue</strong>, esta se encarga de convertir al tipo de dato correcto y agregarle los apostrofes necesarios para los textos y fechas.</p><div class="syntax_hilite"><div id="php-82"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// file: funciones.php</span></div></li><li style="color:#26536A;"><div style=""><span style="color:#000000;">function</span> sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$value</span>, <span style="color:#0000FF;">$type</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; <span style="color:#0000FF;">$value</span> = <a href="http://www.php.net/get_magic_quotes_gpc"><span style="color:#000066;">get_magic_quotes_gpc</span></a><span style="color:#006600;">&#40;</span><span style="color:#006600;">&#41;</span> ? <a href="http://www.php.net/stripslashes"><span style="color:#000066;">stripslashes</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$value</span><span style="color:#006600;">&#41;</span> : <span style="color:#0000FF;">$value</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; <span style="color:#0000FF;">$value</span> = <a href="http://www.php.net/function_exists"><span style="color:#000066;">function_exists</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"mysql_real_escape_string"</span><span style="color:#006600;">&#41;</span> ? <a href="http://www.php.net/mysql_real_escape_string"><span style="color:#000066;">mysql_real_escape_string</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$value</span><span style="color:#006600;">&#41;</span> : <a href="http://www.php.net/mysql_escape_string"><span style="color:#000066;">mysql_escape_string</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$value</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; <span style="color:#616100;">switch</span> <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$type</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#616100;">case</span> <span style="color:#FF0000;">"text"</span>:</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$value</span> = <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$value</span> != <span style="color:#FF0000;">""</span><span style="color:#006600;">&#41;</span> ? <span style="color:#FF0000;">"'"</span> . <span style="color:#0000FF;">$value</span> . <span style="color:#FF0000;">"'"</span> : <span style="color:#FF0000;">"NULL"</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; <span style="color:#616100;">break</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#616100;">case</span> <span style="color:#FF0000;">"int"</span>:</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$value</span> = <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$value</span> != <span style="color:#FF0000;">""</span><span style="color:#006600;">&#41;</span> ? <a href="http://www.php.net/intval"><span style="color:#000066;">intval</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$value</span><span style="color:#006600;">&#41;</span> : <span style="color:#FF0000;">"NULL"</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; <span style="color:#616100;">break</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#616100;">case</span> <span style="color:#FF0000;">"double"</span>:</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$value</span> = <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$value</span> != <span style="color:#FF0000;">""</span><span style="color:#006600;">&#41;</span> ? <span style="color:#FF0000;">"'"</span> . <a href="http://www.php.net/doubleval"><span style="color:#000066;">doubleval</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$value</span><span style="color:#006600;">&#41;</span> . <span style="color:#FF0000;">"'"</span> : <span style="color:#FF0000;">"NULL"</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; <span style="color:#616100;">break</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#616100;">case</span> <span style="color:#FF0000;">"date"</span>:</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$value</span> = <span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$value</span> != <span style="color:#FF0000;">""</span><span style="color:#006600;">&#41;</span> ? <span style="color:#FF0000;">"'"</span> . <span style="color:#0000FF;">$value</span> . <span style="color:#FF0000;">"'"</span> : <span style="color:#FF0000;">"NULL"</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; &nbsp; <span style="color:#616100;">break</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; <span style="color:#006600;">&#125;</span></div></li><li style="color:#3A6A8B;"><div style="">&nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$value</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Esta función recibe dos parámetros, el primero el dato que deseamos normalizar y el segundo el tipo de dato (<em>text</em>, <em>int</em>, <em>double</em>, <em>date</em>). Luego vamos a modificar nuestro primer script para verificar los datos antes de guardarlos.</p><div class="syntax_hilite"><div id="php-83"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#FF9933; font-style:italic;">// file: insert-demo3.php</span></div></li><li style="color:#26536A;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"conexion.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">require</span><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"funciones.php"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$status</span> = <span style="color:#FF0000;">""</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#616100;">if</span> <span style="color:#006600;">&#40;</span><a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"nombre"</span><span style="color:#006600;">&#93;</span><span style="color:#006600;">&#41;</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$nombre</span> = sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"nombre"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">"text"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$direccion</span> = sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"direccion"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">"text"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$telefono</span> = sqlValue<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600;">&#91;</span><span style="color:#FF0000;">"telefono"</span><span style="color:#006600;">&#93;</span>, <span style="color:#FF0000;">"int"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$sql</span> = <span style="color:#FF0000;">"INSERT INTO empresa (nombre, direccion, telefono) "</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$sql</span>.= <span style="color:#FF0000;">"VALUES ("</span>.<span style="color:#0000FF;">$nombre</span>.<span style="color:#FF0000;">", "</span>.<span style="color:#0000FF;">$direccion</span>.<span style="color:#FF0000;">", "</span>.<span style="color:#0000FF;">$telefono</span>.<span style="color:#FF0000;">")"</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$sql</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$status</span> = <span style="color:#FF0000;">"ok"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Unimos nuevamente el código y obtenemos una nueva forma de guardar datos en MySQL esta vez verificando los tipos de datos antes de guardarlos.</p><p>Haciendo pequeñas modificaciones podríamos guardar los datos de cualquier formulario, no importa la cantidad de datos o la complejidad de los mismos, todo este proceso se puede resumir en armar correctamente la consulta SQL para insertar los datos.</p><p><strong>Depurar los INSERTS</strong></p><p>Si tienen problemas en la creación de sus consultas por que no insertan o generan un error, lo mas sencillo es imprimir la consulta antes de enviarla a MySQL y detener la ejecución del script PHP.</p><div class="syntax_hilite"><div id="php-84"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$sql</span> = <span style="color:#FF0000;">"INSERT INTO empresa (nombre, direccion, telefono) "</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#0000FF;">$sql</span>.= <span style="color:#FF0000;">"VALUES ("</span>.<span style="color:#0000FF;">$nombre</span>.<span style="color:#FF0000;">", "</span>.<span style="color:#0000FF;">$direccion</span>.<span style="color:#FF0000;">", "</span>.<span style="color:#0000FF;">$telefono</span>.<span style="color:#FF0000;">")"</span>;</div></li><li style="color:#3A6A8B;"><div style=""><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$sql</span>;</div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a>;</div></li><li style="color:#3A6A8B;"><div style=""><a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$sql</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>La consulta devuelta podemos revisarla y detectar algún error de sintaxis, también podríamos ejecutarla directamente en algún cliente de MySQL que tengamos para verificar si hace la inserción correctamente.</p><p style="text-align:left;"><a href="http://blog.unijimpe.net/download/insert.zip" title="Insertar en MySQL"><img src="http://blog.unijimpe.net/btdown.png" border="0"></a></p><p><strong>Nota Final</strong></p><p>Para mejorar aún mas nuestro script para insertar datos, podríamos agregar validación de formularios. Ello lo podríamos hacer desde el lado del cliente mediante una librería javascript para validar formulario (Por ejemplo: <a href="http://blog.unijimpe.net/validar-formularios-con-mootools/">Validar Formularios con Mootools</a> o también <a href="http://blog.unijimpe.net/vanadium-validar-formularios-facilmente/">Vanadium: Validar formularios fácilmente</a>). La segunda forma es hacerlo con PHP verificando que los datos no estén vacíos, asignando valores por defecto si es que el campo esta vacío, entre otras verificaciones.</p><p style="font-size: 10px;border-top: 1px solid #666666;margin-top=12px;padding-top:6px;">&copy; 2006 - 2011 <a href="http://blog.unijimpe.net">unijimpe</a> - Utiliza este feed solo para uso personal, partes de este feed pueden ser utilizados mencionando al autor, no esta permitido publicar enteramente este feed para uso comercial sin permiso del autor.</p><br /> ]]></content:encoded> <wfw:commentRss>http://blog.unijimpe.net/php-con-mysql-insertar-datos-en-mysql/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>PHP con MySQL: Crear una conexión a MySQL</title><link>http://blog.unijimpe.net/php-con-mysql-crear-una-conexion-a-mysql/</link> <comments>http://blog.unijimpe.net/php-con-mysql-crear-una-conexion-a-mysql/#comments</comments> <pubDate>Mon, 29 Nov 2010 05:12:25 +0000</pubDate> <dc:creator>unijimpe</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[base de datos]]></category> <category><![CDATA[conexion]]></category><guid isPermaLink="false">http://blog.unijimpe.net/?p=1248</guid> <description><![CDATA[<p>La conexión de PHP con MySQL es el primer paso para crear aplicaciones web. Al tener una conexión podremos insertar, editar y consultar registros de una Base de Datos lo cual nos permitirá crear aplicaciones que &#8230;</p>]]></description> <content:encoded><![CDATA[<p>La conexión de PHP con MySQL es el primer paso para crear aplicaciones web. Al tener una conexión podremos insertar, editar y consultar registros de una Base de Datos lo cual nos permitirá crear aplicaciones que permitan a los visitantes a consultar información de acuerdo a sus necesidades.</p><p><strong>Crear una conexión con MySQL</strong></p><p>Para crear una conexión con MySQL hacemos uso de la función <em>mysql_connect</em> y luego definimos la base de datos que vamos a utilizar por medio de la función <em>mysql_select_db</em>.</p><div class="syntax_hilite"><div id="php-88"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#0000FF;">$conexion</span> = <a href="http://www.php.net/mysql_connect"><span style="color:#000066;">mysql_connect</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"localhost"</span>, <span style="color:#FF0000;">"usuario"</span>, <span style="color:#FF0000;">"clave"</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><a href="http://www.php.net/mysql_select_db"><span style="color:#000066;">mysql_select_db</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"demo"</span>, <span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>En la primera línea hemos creado la conexión a un servidor local y luego hemos seleccionado la base de datos <em>'demo'</em> la cual utilizaremos para consultar datos según nuestras necesidades.</p><p><strong>Explicando la Conexión</strong></p><p>En el ejemplo hemos utilizado la función <em>mysql_connect</em>, el cual recibe tres parámetros.</p><div class="syntax_hilite"><div id="code-89"><div class="code"><ol><li style="color:#3A6A8B;"><div style="">mysql_connect<span style="color:#006600;">&#40;</span><span style="color:#CC0000;">"server"</span>, <span style="color:#CC0000;">"username"</span>, <span style="color:#CC0000;">"password"</span><span style="color:#006600;">&#41;</span></div></li></ol></div></div></div><p></p><p>Donde se tienen:</p><ul><li><strong>server</strong>: es la dirección IP o el nombre del servidor de base de datos.</li><li><strong>username</strong>: nombre usuario para acceder a MySQL.</li><li><strong>password</strong>: clave de acceso a MySQL.</li></ul><p><strong>Cerrando la Conexión</strong></p><p>Una vez creada la conexión y hecho las consultas necesarias a nuestra base de datos, es necesario cerrar esa conexión de esta forma liberamos recursos del servidor y mejoramos el rendimiento de nuestra aplicación.</p><div class="syntax_hilite"><div id="php-90"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><a href="http://www.php.net/mysql_close"><span style="color:#000066;">mysql_close</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$conexion</span><span style="color:#006600;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Para cerrar la conexión hemos utilizado la función mysql_close que recibe como parámetro el identificador de la conexión.</p><p><strong>Recomendaciones</strong></p><ul><li>Se recomienda hacer la conexión en un archivo externo, para poder utilizarlo en diferentes páginas. De esta forma si cambiamos de servidor solo editamos el archivo de conexión con los nuevos datos de conexión.</li><li>Es recomendable cerrar siempre las conexiones, esto previene que la aplicación tenga errores por exceso de conexiones.</li></ul><p>Este post forma parte de una seria de artículos destinados a explicar el manejo de datos con MySQL. Poco a poco explicaremos como insertar registros, editar, eliminar y extraer datos de MySQL.</p><p style="font-size: 10px;border-top: 1px solid #666666;margin-top=12px;padding-top:6px;">&copy; 2006 - 2011 <a href="http://blog.unijimpe.net">unijimpe</a> - Utiliza este feed solo para uso personal, partes de este feed pueden ser utilizados mencionando al autor, no esta permitido publicar enteramente este feed para uso comercial sin permiso del autor.</p><br /> ]]></content:encoded> <wfw:commentRss>http://blog.unijimpe.net/php-con-mysql-crear-una-conexion-a-mysql/feed/</wfw:commentRss> <slash:comments>14</slash:comments> </item> <item><title>Técnicas para Ofuscar Email</title><link>http://blog.unijimpe.net/tecnicas-para-ofuscar-email/</link> <comments>http://blog.unijimpe.net/tecnicas-para-ofuscar-email/#comments</comments> <pubDate>Mon, 09 Aug 2010 07:13:38 +0000</pubDate> <dc:creator>unijimpe</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Seguridad]]></category> <category><![CDATA[codificar]]></category> <category><![CDATA[email]]></category> <category><![CDATA[ofuscar]]></category> <category><![CDATA[seguridad]]></category> <category><![CDATA[spam]]></category><guid isPermaLink="false">http://blog.unijimpe.net/?p=1209</guid> <description><![CDATA[<p>El SPAM es uno de los problemas por la que todo usuario a perdido mucho tiempo, siempre nos envían correos no solicitados con publicidad. Una de las técnicas utilizadas por los Spammers para obtener dirección email &#8230;</p>]]></description> <content:encoded><![CDATA[<p>El SPAM es uno de los problemas por la que todo usuario a perdido mucho tiempo, siempre nos envían correos no solicitados con publicidad. Una de las técnicas utilizadas por los Spammers para obtener dirección email es crear <em>robots</em> que van por todas las páginas leyendo y guardando las direcciones email publicadas. Basado en este método para obtener nuestras direcciones tenemos una regla para evitar el spam.</p><blockquote><p>Nunca publicar directamente direcciones <strong>email</strong> en una página web.</p></blockquote><p>Veamos algunas técnicas para mostrar direcciones de correo en nuestras páginas sin exponernos a un ataque de spam a estas cuentas de email.</p><p><a href="http://samples.unijimpe.net/ofuscar-email.php"><img src="http://blog.unijimpe.net/wp-content/uploads/2010/08/obuscate-email.jpg" alt="" title="obuscate-email" width="500" height="190" /></a></p><p><strong>Utilizando CSS: Método #1</strong></p><p>Una técnica sencilla pero que da buenos resultados es escribir la dirección email al revés y luego con estilos mostrarlo en la dirección correcta utilizando la propiedad <em>direction</em>, por ejemplo:</p><div class="syntax_hilite"><div id="html-97"><div class="html"><ol><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000;">&lt;span</span></a> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">"direction:rtl; unicode-bidi:bidi-override;"</span><span style="color: #000000;">&gt;</span></a></span>moc.oinimod@otcatnoc<span style="color: #009900;"><span style="color: #000000;">&lt;/span&gt;</span></span></div></li></ol></div></div></div><p></p><p>Entonces creamos una función que se encargue de utilizar este método para mostrar los emails en nuestra web:</p><div class="syntax_hilite"><div id="php-98"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#000000;">function</span> hideEmail<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$mail</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$mail</span> = <a href="http://www.php.net/strrev"><span style="color:#000066;">strrev</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$mail</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#FF0000;">"&lt;span style=<span style="color:#000099;">\"</span>direction:rtl; unicode-bidi:bidi-override;<span style="color:#000099;">\"</span>&gt;"</span>.<span style="color:#0000FF;">$mail</span>.<span style="color:#FF0000;">"&lt;/span&gt;"</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p><strong>Utilizando CSS: Método #2</strong></p><p>Otró método consiste en agregarle un texto oculto dentro de la dirección email, de esta forma la dirección se visualizará correctamente pero si un spammer copia el email esta contendrá un dirección inválida. Este texto se oculta con estilos utilizando la propiedad <em>display</em>.</p><div class="syntax_hilite"><div id="html-99"><div class="html"><ol><li style="color:#3A6A8B;"><div style="">contacto@<span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000;">&lt;span</span></a> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">"display:none"</span><span style="color: #000000;">&gt;</span></a></span>null<span style="color: #009900;"><span style="color: #000000;">&lt;/span&gt;</span></span>dominio.com</div></li></ol></div></div></div><p></p><p>Luego creamos la función en PHP para ofuscar los emails:</p><div class="syntax_hilite"><div id="php-100"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#000000;">function</span> hideEmail<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$mail</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$temp</span> = <a href="http://www.php.net/explode"><span style="color:#000066;">explode</span></a><span style="color:#006600;">&#40;</span><span style="color:#FF0000;">"@"</span>, <span style="color:#0000FF;">$mail</span><span style="color:#006600;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$temp</span><span style="color:#006600;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600;">&#93;</span>.<span style="color:#FF0000;">"@&lt;span style=<span style="color:#000099;">\"</span>display:none;<span style="color:#000099;">\"</span>&gt;null&lt;/span&gt;"</span>.<span style="color:#0000FF;">$temp</span><span style="color:#006600;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600;">&#93;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p><strong>Utilizando codificación ROT13</strong></p><p>Este método consiste en codificar la dirección email con el algoritmo ROT13 que consiste en trasladar 13 posiciones las letras del alfabeto, dejando los caracteres no-alfabéticos sin modificar. Para ello escribimos el email codificado y lo mostramos correctamente con una función Javascript <a href="http://phpjs.org/functions/str_rot13:528">str_rot13</a>.</p><div class="syntax_hilite"><div id="html-101"><div class="html"><ol><li style="color:#3A6A8B;"><div style=""><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text/javascript"</span><span style="color: #000000;">&gt;</span></a></span></div></li><li style="color:#26536A;"><div style="">document.write((&quot;pbagnpgb@qbzvavb.pbz&quot;).replace(/[a-z]/gi, function(s){</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; return String.fromCharCode(s.charCodeAt(0)+(s.toLowerCase()<span style="color: #009900;"><span style="color: #000000;">&lt;</span></a><span style="color: #ff0000;">'n'</span>?<span style="color: #cc66cc;color:#800000;">13</span>:-<span style="color: #cc66cc;color:#800000;">13</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="color:#3A6A8B;"><div style=""><span style="color: #000000;">&lt;/script&gt;</span></span></div></li></ol></div></div></div><p></p><p>Luego escribimos una función en PHP que se encargue de codificar la dirección email y que imprima el código javascript para decodificarlo:</p><div class="syntax_hilite"><div id="php-102"><div class="php"><ol><li style="color:#3A6A8B;"><div style=""><span style="color:#000000;">function</span> hideEmail<span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$mail</span><span style="color:#006600;">&#41;</span> <span style="color:#006600;">&#123;</span></div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$temp</span> = <span style="color:#FF0000;">"&lt;script type=<span style="color:#000099;">\"</span>text/javascript<span style="color:#000099;">\"</span>&gt;<span style="color:#000099;">\n</span>"</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$temp</span>.= <span style="color:#FF0000;">"document.write((<span style="color:#000099;">\"</span>"</span>.<a href="http://www.php.net/str_rot13"><span style="color:#000066;">str_rot13</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000FF;">$mail</span><span style="color:#006600;">&#41;</span>.<span style="color:#FF0000;">"<span style="color:#000099;">\"</span>).replace(/[a-z]/gi, function(s){ <span style="color:#000099;">\n</span>"</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$temp</span>.= <span style="color:#FF0000;">"&nbsp; &nbsp;return String.fromCharCode(s.charCodeAt(0)+(s.toLowerCase()&lt;'n'?13:-13));<span style="color:#000099;">\n</span>"</span>;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$temp</span>.= <span style="color:#FF0000;">"}));<span style="color:#000099;">\n</span>"</span>;</div></li><li style="color:#26536A;"><div style="">&nbsp; &nbsp; <span style="color:#0000FF;">$temp</span>.= <span style="color:#FF0000;">"&lt;/script&gt;"</span>;&nbsp; &nbsp;</div></li><li style="color:#3A6A8B;"><div style="">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$temp</span>;</div></li><li style="color:#26536A;"><div style=""><span style="color:#006600;">&#125;</span></div></li></ol></div></div></div><p></p><p>Puedes escoger cualquiera de estar técnicas y utilizarlo rápidamente para mostrar tu dirección de email sin temor a ataques de SPAM. Puedes descargar el código fuente con estas funciones asi como el ejemplo de uso para que puedan hacer sus pruebas.</p><p style="text-align:left;"><a href="http://blog.unijimpe.net/download/ofuscar-email.zip" title="Email  Obfuscate"><img src="http://blog.unijimpe.net/btdown.png" border="0"></a></p><p><strong>Mas Información</strong></p><ul><li><a href="http://techblog.tilllate.com/2008/07/20/ten-methods-to-obfuscate-e-mail-addresses-compared/">Nine ways to obfuscate e-mail addresses compared</a></li><li><a href="http://ch2.php.net/str_rot13">PHP: str_rot13 - Manual</a></li><li><a href="http://phpjs.org/functions/str_rot13:528">JavaScript str_rot13</a></li><li><a href="http://www.maurits.vdschee.nl/php_hide_email/">PHP hide_email()</a></li></ul><p style="font-size: 10px;border-top: 1px solid #666666;margin-top=12px;padding-top:6px;">&copy; 2006 - 2011 <a href="http://blog.unijimpe.net">unijimpe</a> - Utiliza este feed solo para uso personal, partes de este feed pueden ser utilizados mencionando al autor, no esta permitido publicar enteramente este feed para uso comercial sin permiso del autor.</p><br /> ]]></content:encoded> <wfw:commentRss>http://blog.unijimpe.net/tecnicas-para-ofuscar-email/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching using disk: basic

Served from: blog.unijimpe.net @ 2012-02-04 01:26:39 -->
