Data SOMETIMES are not saved in MySQL database

0

< a href="https://i.stack.imgur.com/7ngv4.png"> I have an update system of data for a web page of news, consists of a form to put image, title, subtitle, text .. The form serves, works correctly, I know because when I use it, the rows of my database are updated and also I put an alert that indicates that the database was updated by doing SUBMIT, but suddenly the table goes crazy, and it is not updated, the row remains empty.

It must be said that all fields are with VARCHAR (1000) and the field TEXT and TEXT2 are with LongText.

the code would be the following:

<?php
//Declaracion de variables
$etiqueta = $_POST["etiqueta"];
$autor = $_POST["autor"];
$fecha = $_POST["fecha"];
$titulo = $_POST["titulo"];
$subtitulo = $_POST["subtitulo"];
$texto = $_POST["texto"];
$texto2 = $_POST["texto2"];
$imagenCortesia = $_POST["cortesia"];
$imagen_nombre = $_FILES['imagen']['name'];
$imagen_archivo = $_FILES['imagen']['tmp_name'];
$ruta = "imagenesNoticias";
$ruta = $ruta."/".$imagen_nombre;

//Crear Variables para conexion Noticias Principales
$host = "localhost";
$user = "********";
$pw = "*************";
$dataBase1 = "kautivai_DatosDeNoticias";

//========================= Consulta ============================//

//Consulta de campos llenos
if(isset($_POST['etiqueta']) && !empty($_POST['etiqueta']) &&
isset($_POST['autor']) && !empty($_POST['autor']) && 
isset($_POST['fecha']) && !empty($_POST['fecha']) &&
isset($_POST['titulo']) && !empty($_POST['titulo']) &&
isset($_POST['subtitulo']) && !empty($_POST['subtitulo']) &&
isset($_POST['texto']) && !empty($_POST['texto']) &&
isset($_POST['texto2']) && !empty($_POST['texto2']) &&
isset($_POST['cortesia']) && !empty($_POST['cortesia']) &&
isset($_FILES['imagen']['tmp_name']) && !empty($_FILES['imagen']['tmp_name'])){

    $conexion = mysqli_connect($host, $user, $pw) or die("Problemas al conectar con base de datos 'kautivai_DatosDeNoticias'");
    mysqli_select_db($conexion, $dataBase1) or die("Problemas al conectar con base de datos 'kautivai_DatosDeNoticias'");

    move_uploaded_file($imagen_archivo, $ruta);

    //Toma de datos y paso a base de datos
    mysqli_query($conexion, "INSERT INTO Noticia1(Etiqueta, Autor, Fecha, Titulo, Subtitulo, Texto, Texto2, Cortesia, RutaImagen) VALUES('$etiqueta', '$autor', '$fecha', '$titulo', '$subtitulo', '$texto', '$texto2', '$imagenCortesia', '$ruta')");

    echo "<script>
            alert('Los Datos han sido guardados, Base de datos actualizada!');
            window.history.go(-1);
        </script>";

} else {
    echo "Problemas al insertar los Datos en la base de datos 'kautivai_DatosDeNoticias'";
}

mysqli_close($conexion);

?>

Does anyone have any idea what is happening?

    
asked by Marvin Morales 09.11.2017 в 22:00
source

2 answers

0

Bearing in mind that I do not know what is the structure of your DB or the code, your form to validate data types, I leave you a code which you can adapt to your requirements, this is obviously checked.

HTML

<input type="submit" name="insertar" id="insertar" value="Guardar" 
class="btn btn-info"/>

PHP

<?php

if(isset($_POST['insertar']))
{
  $items1 = ($_POST['FECHA']);
  $items2 = ($_POST['CUENTA']);
  $items3 = ($_POST['OT']);
  $items4 = ($_POST['CIUDAD']);
  $items5 = ($_POST['TIPO_ACTIVIDAD']);
  $items6 = ($_POST['SUBTIPO']);
  $items7 = ($_POST['ITEM_RE']);
  $items8 = ($_POST['OBSERVACIONES']);
  $items9 = ($_POST['CANTIDAD']);
  $items10 = ($_POST['OT_RECUPERADA']);
  $items11 = ($_POST['VALOR_RE_MO']);
  $items12 = ($_POST['USUARIO']);

  //Separar valores de array
 while(true){

// Retrieve the values of the fixes         $ item1 = current ($ items1);         $ item2 = current ($ items2);         $ item3 = current ($ items3);         $ item4 = current ($ items4);         $ item5 = current ($ items5);         $ item6 = current ($ items6);         $ item7 = current ($ items7);         $ item8 = current ($ items8);         $ item9 = current ($ items9);         $ item10 = current ($ items10);         $ item11 = current ($ items11);         $ item12 = current ($ items12);

    //Asignarlos a variables
    $fecha=(( $item1 !== false) ? $item1 : ", &nbsp;");
    $cuenta=(( $item2 !== false) ? $item2 : ", &nbsp;");
    $ot=(( $item3 !== false) ? $item3 : ", &nbsp;");
    $ciudad=(( $item4 !== false) ? $item4 : ", &nbsp;");
    $tipo_actividad=(( $item5 !== false) ? $item5 : ", &nbsp;");
    $subtipo=(( $item6 !== false) ? $item6 : ", &nbsp;");
    $item_re=(( $item7 !== false) ? $item7 : ", &nbsp;");
    $observaciones=(( $item8 !== false) ? $item8 : ", &nbsp;");
    $cantidad=(( $item9 !== false) ? $item9 : ", &nbsp;");
    $ot_recuperada=(( $item10 !== false) ? $item10 : ", &nbsp;");
    $valor_re_mo=(( $item11 !== false) ? $item11 : ", &nbsp;");
    $usuario=(( $item12 !== false) ? $item12 : ", &nbsp;");

    // Concantenar los valores en orden para su futura insercion

 $valores='("'.$fecha.'","'.$cuenta.'","'.$ot.'",
 "'.$ciudad.'","'.$tipo_actividad.'","'.$subtipo.'",
 "'.$item_re.'","'.$observaciones.'","'.$cantidad.'","'.$ot_recuperada.'",
 "'.$valor_re_mo.'","'.$usuario.'"),';

    // Ya que termina con coma cada fila se resta con la funcion SUBSTR la 
       ultima fila
    $valoresQ = substr($valores, 0, -1);

    $sql = "INSERT INTO 'facturacion_hfc'('FECHA', 'CUENTA', 'OT', 'CIUDAD', 
'TIPO_ACTIVIDAD', 'SUBTIPO', 'ITEM_RE', 'OBSERVACIONES', 'CANTIDAD', 
'OT_RECUPERADA', 'VALOR_RE_MO', 'USUARIO') VALUES $valoresQ";

    $sqlRes = mysql_query($sql, $conexion) or mysql_error();

    // Up! Next Value
    $item1 = next($items1);
    $item2 = next($items2);
    $item3 = next($items3);
    $item4 = next($items4);
    $item5 = next($items5);
    $item6 = next($items6);
    $item7 = next($items7);
    $item8 = next($items8);
    $item9 = next($items9);
    $item10 = next($items10);
    $item11 = next($items11);
    $item12 = next($items12);

// Check terminator
    if($item1 === false && $item2 === false && $item3 === false && $item4 
=== false && $item5 === false && $item6 === false && $item7 === false && 
$item8 === false && $item9 === false && $item10 === false && $item10 === 
false && $item11 === false && $item12 === false) break;

  }

}

    if(isset($_POST['insertar'])){
  echo "
  <scripttype='text/javascript'>location.href='ingre_hfc_exi.php'</script>";
}

?>
    
answered by 09.11.2017 в 22:29
0

The problem you suffer is related to a bad escape from the SQL query:

mysqli_query($conexion, "INSERT INTO Noticia1(Etiqueta, Autor, Fecha, Titulo, Subtitulo, Texto, Texto2, Cortesia, RutaImagen) VALUES('$etiqueta', '$autor', '$fecha', '$titulo', '$subtitulo', '$texto', '$texto2', '$imagenCortesia', '$ruta')");

Use prepared PDO or mysqli queries.

    
answered by 12.11.2017 в 01:28