Ver Mensaje Individual
  #2 (permalink)  
Antiguo 26/08/2013, 08:16
bathorz
 
Fecha de Ingreso: agosto-2013
Mensajes: 150
Antigüedad: 11 años, 6 meses
Puntos: 29
Respuesta: Error en INSERT, se insertan regristros en blanco

Si aún te sirve esto funciona:

Tabla para el test:
Código SQL:
Ver original
  1. CREATE TABLE IF NOT EXISTS `rh_puestos` (
  2.   `id` INT(8) NOT NULL AUTO_INCREMENT,
  3.   `numero` INT(10) DEFAULT NULL,
  4.   `puestos` INT(5) DEFAULT NULL,
  5.   `job` VARCHAR(10) DEFAULT NULL,
  6.   `code` INT(5) DEFAULT NULL,
  7.   `global_tittle` VARCHAR(20) DEFAULT NULL,      
  8.   `fecha` DATE DEFAULT NULL,
  9.   `archivo` VARCHAR(20) DEFAULT NULL,
  10.   `file` VARCHAR(20) DEFAULT NULL,  
  11.   PRIMARY KEY (`id`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci AUTO_INCREMENT=1;

Código PHP:
Ver original
  1. <?php
  2. function conectadb() {
  3.    $servidor='127.0.0.1';
  4.    $usuario='user';
  5.    $clave='pass';
  6.    $base='test';
  7.  
  8.    $db=@mysql_connect($servidor,$usuario,$clave) or die(mysqlError());
  9.    @mysql_select_db($base,$db)or die(mysqlError());
  10.  
  11.    return $db;
  12. }
  13.  
  14. function mysqlError() {
  15.    echo '<br><span class="error">ERROR: '.mysql_errno().' -> '.mysql_error().'</span><p>';
  16.    if (func_num_args()) {
  17.       echo 'En Consulta ->' . func_get_arg(0);
  18.    }
  19. }
  20. ?>
  21. <link
  22.     href="../css/tablas.css" rel="stylesheet" type="text/css">
  23. <form
  24.     enctype="multipart/form-data"
  25.     action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
  26.     <!--  -->
  27.     Espa&ntilde;ol<input type="file" name="archivo" id="archivo" />
  28.     <!--  -->
  29.     Ingl&eacute;s<input type="file" name="file" id="archivo" />
  30.     <!--  -->
  31.     Number<input type='text' id='' name='number' size='5' value='' />
  32.     <!--  -->
  33.     Puesto<input type='text' id='' name='puesto' size='5' value='' />
  34.     <!--  -->
  35.     Job<input type='text' id='' name='job' size='5' value='' />
  36.     <!--  -->
  37.     Code<input type='text' id='' name='code' size='5' value='' />
  38.     <!--  -->
  39.     Tittle<input type='text' id='' name='tittle' size='5' value='' />
  40.     <!--  -->
  41.     Fecha<input type='date' name='fecha' size='8' maxlength='10'
  42.         value='<?= date("Y-m-d") ?>' />
  43.     <!--  -->
  44.     <input type="submit" name="add" value="Add">
  45. </form>
  46. <!--  -->
  47. <table width="71%" height="91" border="1" align="center"
  48.     style="position: absolute; left: 74px; top: 304px; width: 931px; border: thin; border-color: #CCC;">
  49.     <tr class="footable">
  50.         <td width="83" align="center"><p>Numero</p></td>
  51.         <td width="129" align="center"><p>Puesto</p></td>
  52.         <td width="137" align="center"><p>Job</p></td>
  53.         <td width="101" align="center"><p>Code</p></td>
  54.         <td width="233" align="center"><p>Global Tittle</p></td>
  55.         <td width="208" align="center"><p>Fecha de Aprobacion-Aprovval Date</p>
  56.         </td>
  57.     </tr>
  58.     <?php
  59.     $link= conectadb();
  60.  
  61.     //SE INSERTA UN NUEVO REGISTRO A LA TABLA///
  62.     if (isset($_POST["add"])) {
  63.  
  64.        if(is_uploaded_file($_FILES['archivo']['tmp_name'])) {
  65.           // me verifica haya sido cargado el archivo
  66.           $ruta_destino = "../Recursoshumanos/puestos/";
  67.           $namefinal= trim ($_FILES['archivo']['name']); //linea nueva devuelve la cadena sin espacios al principio o al final
  68.           $namefinal= ereg_replace (" ", "", $namefinal);// linea nueva devuelve la cadena sin espacios entre palabtas
  69.           $uploadfile= $ruta_destino.$namefinal;
  70.           if(move_uploaded_file($_FILES['archivo']['tmp_name'], $uploadfile)) { // se coloca en su lugar final
  71.           }
  72.        }
  73.  
  74.        if(is_uploaded_file($_FILES['file']['tmp_name'])){
  75.           //me verifica haya sido cargado el archivo
  76.           $ruta_destino2 = "../Recursoshumanos/puestos/";
  77.           $namefinal2= trim ($_FILES['file']['name']); //linea nueva devuelve la cadena sin espacios al principio o al final
  78.           $namefinal2= ereg_replace (" ", "", $namefinal2);// linea nueva devuelve la cadena sin espacios entre palabtas
  79.           $uploadfile2= $ruta_destino . $namefinal2;
  80.           if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile2)){ // se coloca en su lugar final
  81.  
  82.           }
  83.        }
  84.  
  85.        /** **/
  86.        //conectamos a la base de datos para almacenar los datos y la ruta del archivo
  87.        $numero=$_POST["number"];
  88.        $puesto=$_POST["puesto"];
  89.        $job=$_POST["job"];
  90.        $code=$_POST["code"];
  91.        $tittle=$_POST["tittle"];
  92.        $fecha=$_POST["fecha"];
  93.        $sql = "INSERT INTO rh_puestos (numero,puestos,job,code,global_tittle,fecha,archivo,file) VALUES ('$numero','$puesto', '$job', '$code', '$tittle', '$fecha','$namefinal','$namefinal2')";
  94.        $result = mysql_query($sql,$link);
  95.     }
  96.  
  97.     /** **/
  98.     //SE REALIZA UNA BUSQUEDA///
  99.     $search=$_POST["buscar"];
  100.     $result= mysql_query("SELECT * FROM rh_puestos WHERE numero like '$search%' OR puestos like '$search%' or job like '$search%' or code like'$search%' or global_tittle like '$search%' or fecha like '$search%'",$link);
  101.  
  102.     //imprime arreglo
  103.     while ($row = mysql_fetch_array($result)) {
  104.        $i=$row["archivo"];
  105.        $e=$row["file"];
  106.  
  107.        echo "<tr>";
  108.        echo "<td>".$row["numero"]."</td>";
  109.  
  110.        if($i==""){ //si no se agrega puesto en espanol no se agrega un link
  111.           echo"<td>".$row["puestos"]."</td>";
  112.        }
  113.        else {
  114.           echo "<td><a href='puestos/".$i."'</a>".$row["puestos"]."</td>";
  115.        }
  116.  
  117.        if($e==""){ // si no se agregan jobs no se agrega link
  118.           echo "<td>".$row["job"]."</td>";
  119.        } else {
  120.           echo "<td><a href='puestos/".$e."'</a>".$row["job"]."</td>";
  121.        }
  122.  
  123.        echo "<td>".$row["code"]."</td>";
  124.        echo "<td>".$row["global_tittle"]."</td>";
  125.        echo "<td>".$row["fecha"]."</td>";
  126.  
  127.        echo "</tr>";
  128.     }
  129.  
  130.     $result->free;
  131.     mysql_close($link);//se termina la consulta
  132.  
  133.     ?>
  134. </table>