Ver Mensaje Individual
  #3 (permalink)  
Antiguo 24/05/2011, 10:40
Tymofek
 
Fecha de Ingreso: marzo-2011
Mensajes: 6
Antigüedad: 13 años, 1 mes
Puntos: 0
Respuesta: principiante: boton submit, registro duplicado

pense ke era algo simple, pero bien aqui el codigo!


Código PHP:
Ver original
  1. <?php require_once('Connections/comentarios.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. $editFormAction = $_SERVER['PHP_SELF'];
  35. if (isset($_SERVER['QUERY_STRING'])) {
  36.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  37. }
  38.  
  39. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  40.   $insertSQL = sprintf("INSERT INTO comentarios (autor, comentario) VALUES (%s, %s)",
  41.                        GetSQLValueString($_POST['autor'], "text"),
  42.                        GetSQLValueString($_POST['comentario'], "text"));
  43.  
  44.   mysql_select_db($database_comentarios, $comentarios);
  45.   $Result1 = mysql_query($insertSQL, $comentarios) or die(mysql_error());
  46.  
  47.   $insertGoTo = "coemntarios.php";
  48.   if (isset($_SERVER['QUERY_STRING'])) {
  49.     $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
  50.     $insertGoTo .= $_SERVER['QUERY_STRING'];
  51.   }
  52.   header(sprintf("Location: %s", $insertGoTo));
  53. }
  54.  
  55. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  56.   $insertSQL = sprintf("INSERT INTO comentarios (fecha, autor, comentario) VALUES (%s, %s, %s)",
  57.                        GetSQLValueString($_POST['fecha'], "date"),
  58.                        GetSQLValueString($_POST['autor'], "text"),
  59.                        GetSQLValueString($_POST['comentario'], "text"));
  60.  
  61.   mysql_select_db($database_comentarios, $comentarios);
  62.   $Result1 = mysql_query($insertSQL, $comentarios) or die(mysql_error());
  63. }
  64.  
  65. $maxRows_comentarios = 10;
  66. $pageNum_comentarios = 0;
  67. if (isset($_GET['pageNum_comentarios'])) {
  68.   $pageNum_comentarios = $_GET['pageNum_comentarios'];
  69. }
  70. $startRow_comentarios = $pageNum_comentarios * $maxRows_comentarios;
  71.  
  72. mysql_select_db($database_comentarios, $comentarios);
  73. $query_comentarios = "SELECT * FROM comentarios ORDER BY ID_comentario DESC";
  74. $query_limit_comentarios = sprintf("%s LIMIT %d, %d", $query_comentarios, $startRow_comentarios, $maxRows_comentarios);
  75. $comentarios = mysql_query($query_limit_comentarios, $comentarios) or die(mysql_error());
  76. $row_comentarios = mysql_fetch_assoc($comentarios);
  77.  
  78. if (isset($_GET['totalRows_comentarios'])) {
  79.   $totalRows_comentarios = $_GET['totalRows_comentarios'];
  80. } else {
  81.   $all_comentarios = mysql_query($query_comentarios);
  82.   $totalRows_comentarios = mysql_num_rows($all_comentarios);
  83. }
  84. $totalPages_comentarios = ceil($totalRows_comentarios/$maxRows_comentarios)-1;
  85. ?>
  86. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  87. <html xmlns="http://www.w3.org/1999/xhtml">
  88. <head>
  89. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  90. <title>Documento sin título</title>
  91. </head>
  92.  
  93. <body>
  94. <div>
  95.   <p>&nbsp;</p>
  96.   <div>
  97.     <div>
  98.       <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  99.         <table align="center">
  100.           <tr valign="baseline">
  101.             <td nowrap="nowrap" align="right">Autor:</td>
  102.             <td><input type="text" name="autor" value="" size="32" /></td>
  103.           </tr>
  104.           <tr valign="baseline">
  105.             <td nowrap="nowrap" align="right">Comentario:</td>
  106.             <td><textarea name="comentario" cols="32" rows="5"></textarea></td>
  107.           </tr>
  108.           <tr valign="baseline">
  109.             <td nowrap="nowrap" align="right">&nbsp;</td>
  110.             <td><input type="submit" value="Insertar registro" /></td>
  111.           </tr>
  112.         </table>
  113.         <input type="hidden" name="MM_insert" value="form1" />
  114.       </form>
  115.       <p>&nbsp;</p>
  116.     </div>
  117.   </div>
  118.   <p>&nbsp;</p>
  119.   <div>
  120.     <table width="290" border="5" align="center" cellspacing="20">
  121.      <?php do { ?>
  122.       <tr>
  123.         <td width="176" height="29"><?php echo $row_comentarios['autor']; ?> dijo...</td>
  124.       </tr>
  125.        <tr>
  126.           <td height="95" align="left" valign="top"><?php echo $row_comentarios['comentario']; ?></td>
  127.         </tr>
  128.         <tr>
  129.           <td><?php echo $row_comentarios['fecha']; ?></td>
  130.         </tr>
  131.         <?php } while ($row_comentarios = mysql_fetch_assoc($comentarios)); ?>
  132.     </table>
  133.   </div>
  134.   <p>&nbsp;</p>
  135.   <p>&nbsp;</p>
  136.   <p>&nbsp;</p>
  137.   <p>&nbsp;</p>
  138. </div>
  139. </body>
  140. </html>
  141. <?php
  142. mysql_free_result($comentarios);
  143. ?>