Error inserting data in mysql

1
<form name="datos" action="TP 371 Orangel.php" method="post">
      Nombre:&nbsp; &nbsp; <input type="text" size="15" pattern="[A-Za-z]{3,}" name='nombre' id='nombre' required><br><br>
      CI/RIF:&nbsp; &nbsp; &nbsp; <input type="text" size="11" name="ID" id="ID" required><br><br>
      Ciudad:&nbsp; &nbsp; &nbsp; <input type="text" pattern="[A-Za-z]{4,}" name="ciudad" id="ciudad" required><br><br>
      URB:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" size="30" name="URB" id="URB"><br><br>
      Calle:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="calle" id="calle" required><br><br>
      N°:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" size="3" name="numero" id="numero" min="1" max="999" required><br><br>
      Correo:&nbsp; &nbsp; &nbsp; &nbsp; <input type="email" size="30" name="correo" id="correo" placeholder="[email protected]" required multiple><br><br>
      Telefono:&nbsp; &nbsp; <input type="tel" size="12" name="telefono" id="telefono"><br><br>
      Tipo de mercancia que desea proteger:<input type="text" name="mercancia" id="mercancia"><br><br>
     ¿Incluye protecci&oacute;n ejecutiva(personal)?
    <select name="proteccion ejecutiva">
        <option value="Si">Si
        <option value="No">No
    </select><br><br>
    Describa el tipo de protecci&oacute;n que desea recibir:<br>
    <textarea name="descripcion del servicio" rows="8" cols="40"></textarea><br><br>
    <input type="submit" value="Enviar"><input type="reset" value="Borrar">

<?php
    include("conexion2.php");
    $nombre = $_POST["nombre"];
    $id = $_POST["ID"];
    $ciudad = $_POST["ciudad"];
    $urb = $_POST["URB"];
    $calle = $_POST["calle"];
    $num = $_POST["numero"];
    $correo = $_POST["correo"];
    $tel = $_POST["telefono"];
    $mer = $_POST["mercancia"];
    $pro = $_POST["proteccion_ejecutiva"];
    $des = $_POST["descripcion_del_servicio"];

    $query = "INSERT INTO cliente(Nombre,CI/RIF,Ciudad,URB,Calle,NumHab,Mail,Tel,Mercan,Protejec,Descrip) VALUES('$nombre','$id','$ciudad','$urb','$calle','$num','$correo','$tel','$mer','$pro','$des')";
    $resultado = $conexion->query($query);

    if($resultado){
        echo "Insercion exitosa";
    }else{
        echo "Insercion fallida";
    }
?>

// Connection arcivo "conecion2.php"

<?php
    $conexion = new mysqli("localhost","root","@orangel18","transvalsa");
    if($conexion){
        echo "Conexion exitosa";
        }else{
            echo "Conexion fallida";
        }
?>
    
asked by Orangel Gonzalez 08.11.2018 в 13:14
source

2 answers

6

I notice 2 weird things in the code. First:

$query = "INSERT INTO cliente(Nombre,CI/RIF,Ciudad,URB,Calle,NumHab,Mail,Tel,Mercan,Protejec,Descrip) VALUES('$nombre','$id','$ciudad','$urb','$calle','$num','$correo','$tel','$mer','$pro','$des')";

The name of the second field, CI / RIF, would create that you can not use the "/" in the name. The interpreter of the code I use marked it as a special character.

Second: I see that before inserting the data, you are not connecting to the base. You do it later. I recommend you do

$conexion = new mysqli("localhost","root","@orangel18","transvalsa");
if($conexion){
 echo "Conexion exitosa";

$query = "INSERT INTO cliente(Nombre,CI/RIF,Ciudad,URB,Calle,NumHab,Mail,Tel,Mercan,Protejec,Descrip) VALUES('$nombre','$id','$ciudad','$urb','$calle','$num','$correo','$tel','$mer','$pro','$des')";
$resultado = $conexion->query($query);

      if($resultado){
                echo "Insercion exitosa";
                    }else{
                    echo "Insercion fallida";
                    }

      }
      else{
        echo "Conexion fallida";
      }

Greetings.

    
answered by 08.11.2018 в 13:38
0

must know what error gives you to see what to help you, it would be best if you do something with try / catch, the other, the action file, avoid having spaces.

<?php
if (!empty($_POST)){

include("conexion2.php");
$nombre = $_POST["nombre"];
$id = $_POST["ID"];
$ciudad = $_POST["ciudad"];
$urb = $_POST["URB"];
$calle = $_POST["calle"];
$num = $_POST["numero"];
$correo = $_POST["correo"];
$tel = $_POST["telefono"];
$mer = $_POST["mercancia"];
$pro = $_POST["proteccion_ejecutiva"];
$des = $_POST["descripcion_del_servicio"];
$query = "INSERT INTO cliente(Nombre,CI/RIF,Ciudad,URB,Calle,NumHab,Mail,Tel,Mercan,Protejec,Descrip) VALUES('$nombre','$id','$ciudad','$urb','$calle','$num','$correo','$tel','$mer','$pro','$des')";

    try {
        $resultado = $conexion->query($query);
    }
    catch (Exception $e) {
        echo $e->getMessage();

    }

}
    
answered by 08.11.2018 в 13:45