Do not save the data in the database

1

The database 5 tables: client, room, history, staff and user.

In the user table is the ID which also happens for the room as user_id, I make my normal form and it appears that the room has been created, but it does not show it or save it, from the database if I can insert data.

It does not throw me any error, it just does not save in the database, I think it's because of the ID relationship with user_id but I do not know what to do.

Edited: It is assumed that the ID of the user table given to me should be assigned to the one in the room.

User table:

CREATE TABLE 'usuario' (
  'id' int(11) NOT NULL,
  'usuario' varchar(20) NOT NULL,
  'clave' varchar(150) NOT NULL,
  'rango' varchar(20) NOT NULL,
  'cedula' int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Volcado de datos para la tabla 'usuario'
--

INSERT INTO 'usuario' ('id', 'usuario', 'clave', 'rango', 'cedula') VALUES
(1, 'eduardo', 'eduardo', '1', 262944),
(18, 'EduardoJ', '123456', '1', 262);

Room table:

CREATE TABLE 'habitacion' (
  'id' int(11) NOT NULL,
  'estado' varchar(200) NOT NULL,
  'n_habitacion' varchar(200) NOT NULL,
  'tipoh' varchar(200) NOT NULL,
  'id_usuario' int(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Volcado de datos para la tabla 'habitacion'
--

INSERT INTO 'habitacion' ('id', 'estado', 'n_habitacion', 'tipoh', 'id_usuario') VALUES
(10, 'Ocupado', '1', 'Completa', 18);

My code is this.

HTML:

 <form action="validarhabitacion.php" method="POST">
                <table  style="margin: 0 auto;">
                    <tr>
                    <td colspan="2"><CENTER><h2>Crear Habitación<h2></CENTER></td>
                    </tr>

                    <tr>
                        <td><h3>Estado de Habitación<h3></td></font>
                        <td><input name="estado" type="text" placeholder="" required></td>
                    </tr>
                    <tr>

                    <tr>
                        <td><h3>Número de Habitación<h3></td>
                        <td><input name="n_habitacion" type="text" placeholder="" required></td>
                    </tr>
                    <tr>
                        <td><h3>Tipo de Habitación<h3></td></font>
                        <td><input name="tipoh" type="text" placeholder=""required></td>
                    </tr>


    <td colspan="2">
    <center><input type="submit" value="Crear"></center>

    </td>
    </table>
    </form>

PHP file validatinghabitation.php:

<?php



$estado=$_POST['estado'];
$n_habitacion=$_POST['n_habitacion'];
$tipoh= $_POST['tipoh'];





    require("connect_db.php");




                mysqli_query($mysqli,"INSERT INTO habitacion VALUES('','$estado','$n_habitacion','$tipoh')");
                //echo 'Se ha registrado con exito';
                echo ' <script language="javascript">alert("Habitación Creada");</script> ';
           echo "<script>location.href='controlhabitacion.php'</script>";



?>

Connection to database:

<?php


        $mysqli = new MySQLi("localhost", "root","", "aeduardo");
        if ($mysqli -> connect_errno) {
            die( "Fallo la conexión a MySQL: (" . $mysqli -> mysqli_connect_errno()
                . ") " . $mysqli -> mysqli_connect_error());
        }
        else
        ?>
    
asked by Eduardo Ortiz 31.07.2018 в 02:13
source

1 answer

1

First, start by making a validation system in which you can discard the possible errors that may arise and then debug the code, I leave a stable connection that you can implement in your file connect_db.php NOTE: the field of the id to send it so empty must AUTO_INCREMENT

and the rest in validarhabitacion.php

$mysqli = mysqli_init();
    if (!$mysqli) {
        die('Falló mysqli_init');
    }

    //Process of connection to the database
    if (!$mysqli->real_connect("localhost", "root","", "aeduardo")) {
        die('Error de conexión (' . mysqli_connect_errno() . ') '
                . mysqli_connect_error());
    }
    //se valida que todos los datos lleguen y lleguen llenos
    if ( (isset($_POST['estado']) && !empty($_POST['estado']) ) &&
         (isset($_POST['n_habitacion']) && !empty($_POST['n_habitacion']) ) &&
         (isset($_POST['tipoh']) && !empty($_POST['tipoh']) )  ) {


//traer el id del usuario
         $sql = "SELECT id FROM usuario WHERE usuario ='nombre_elegifo'";
         $result = $mysqli->query($sql);
         $fila = mysqli_fetch_assoc($result);

      if($fila>0){
        $id_usuario= $fila['id'];
      }




//se pasan valores a las variables
        $estado=$_POST['estado'];
        $n_habitacion=$_POST['n_habitacion'];
        $tipoh= $_POST['tipoh'];
        //se crea el query
        $sql="INSERT INTO habitacion VALUES ('".$estado."','".$n_habitacion."','".$tipoh."','".$id_usuario."')";
        $result = $mysqli->query($sql);
        //si fue exitoso el devuelve un true
        if($result){
            echo "<script>alert('se inserto en la BD');</script>";
        }else{
            //reultado si no se hizo la insercion
            echo "<script>alert('problemas desde la BD');</script>";
        }
    }else{
        //descarte de los campos vacios
        echo "<script>alert('no llegaron los datos');</script>";
    }

NOTE2: DEBES PASAR EL ID DE USUARIO YA QUE EN LA DB ES NOT NULL

I hope you serve and mark it as valid Bro ReNiceCode

    
answered by 31.07.2018 в 02:36