Error wanting to save PHP form data to MySQL

0

I am saving some data from a php to mysql form but I do not know where I am wrong, but it does not save anything, this is the code that I have in my php

MY HTML

<form class="limpiar" method="POST">
  <div class="half izquierda limpiar">
    <input type="text" id="titulo" name="titulo" placeholder="Ingresa Titulo" >
    <input type="text" id="claves" name="claves" placeholder="Ingresar palabras claves" >
    <input type="text" id="siglas" name="siglas" placeholder="Ingresar siglas de institucion">
    <input type="text" id="fecha" name="fecha" placeholder="Ingresar año-mes-dia">
    <input type="text" id="numero" name="numero" placeholder="Ingresar numero">
    <input type="text" id="categoria" name="categoria" placeholder="Ingresar categoria">
    <input type="text" id="rutapdf" name="pdf" placeholder="Ingresar ruta de PDF ">

  </div>

  <div class="half derecha limpiar">
    <input type="text" id="autores" name="autores" placeholder="Ingresar autores">
    <input type="text" id="institucion" name="institucion" placeholder="Ingresar Institucion">
    <input type="text" id="ruta" name="rutaphp" placeholder="Ingresar ruta de php">
    <input type="text" id="volumen" name="volumen" placeholder="Ingresar volumen">
    <textarea name="descripcion" type="text" id="input-message" placeholder="Descripción"></textarea>
  </div>  
  <input type="submit" name="btn-guardar" id="input-submit">
</form>

and this is my code to keep it

MY PHP

<?php
include_once '../php/conexion.php';
if(isset($_POST['btn-guardar']))
{
 // variables for input data
 $titulo = $_POST['titulo'];
 $descripcion = $_POST['descripcion'];
 $autores = $_POST['autores'];
 $pclaves = $_POST['claves'];
 $siglas = $_POST['siglas'];
 //$fecha = $_POST['fecha'];
 $numero = $_POST['numero'];
 $categoria = $_POST['categoria'];
 $pdf = $_POST['pdf'];
 $institucion = $_POST['institucion'];
 $rutaphp = $_POST['rutaphp'];
 $volumen = $_POST['volumen'];

    $mysqli = new mysqli($hostbd,$usuariobd,$clave,$basededatos);
    $sql_query = "INSERT INTO articulos(Titulo,Descripcion,Autor,Institucion,Siglas_Institucion,Url,Keyword,Volumen,Numero,Categoria,Rutapdf) VALUES('$titulo','$descripcion','$autores','$institucion','$siglas','$rutaphp','$pclaves','$volumen','$numero','$categoria','$pdf')";

  if($resultado=$mysqli->query($sql_query))
 {
  ?>
  <script type="text/javascript">
  alert('Datos insertados');
  window.location.href='form.php';
  </script>
  <?php
 }
 else{?>
  <script type="text/javascript">
  alert('Error al guardar');
  window.location.href='form.php';
  </script>
  <?php
 }
}
?>

I know that it is an error because the alert ('Error when saving always appears to me') that I have above always appears to me and does not save anything to me

    
asked by DanielDaniel 05.10.2018 в 18:05
source

1 answer

0

First of all I would recommend that you do not mix the code html with the% code_of%.

Secondly you are putting php in all the fields of the table and I think that not all are of type Strings so the error can come from there.

To enter the variables you have to combine the varchar of the String with the variables separating them by comilla and concatenated them with points.

Then it would be something like this:

$sql_query = "INSERT INTO articulos(Titulo,Descripcion,Autor,Institucion,Siglas_Institucion,Url,Keyword,Volumen,Numero,Categoria,Rutapdf) VALUES('".$titulo."','".$descripcion."','".$autores."','".$institucion."','".$siglas."','".$rutaphp."','".$pclaves."','".$volumen."',".$numero.",'".$categoria."','".$pdf."')";

This solution may not be good at all because I do not know which of these data is numeric and which is varchar type, but keep in mind that the varchar have to go between quotes query and numeric without them ('".$variable."') .

    
answered by 06.10.2018 в 09:45