error with mysqli when connecting with a bd

-2

Good trying to have some records added to the bd but I can not get them to be saved I do not know what I should get these errors and warnings

  

mysqli_query () expects at most 3 parameters, 4 given in C: \ xampp \ htdocs \ boostrap \ css \ wados.php on line 17

     

Notice: Undefined variable: mysqli_select_db in C: \ xampp \ htdocs \ boostrap \ css \ wados.php on line 18

     

Fatal error: Uncaught Error: Function name must be a string in C: \ xampp \ htdocs \ boostrap \ css \ wados.php: 18 Stack trace: # 0 {main} thrown in C: \ xampp \ htdocs \ boostrap \ css \ wados.php on line 18

This is my registration form:

 <!DOCTYPE html>
<html lang="es">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="login.css" 

<head>
<body>
<h1>Formulario de registro</h1>
<form action="wados.php" method="post"
class="form-register">
</head>
  </body>
    <div class="container">
      <form class="form-horizontal" action="registrodeusuarios2.php" method="post">
      <div class="form-group">
          <label for="id" class="col-sm-2  col-xs-4 col-xs-offset4 col-sm-offset-2  control-label">id</label>
          <div class="col-sm-10">
            <input type="id" class="form-control" name="id" id="id" placeholder="id" required>
          </div>

          <div class="form-group">
          <label for="nombre" class="col-sm-2  col-xs-4 col-xs-offset4 col-sm-offset-2  control-label">nombre</label>
          <div class="col-sm-10">
            <input type="nombre" class="form-control" name="nombre" id="nombre" placeholder="nombre" required>
          </div>
          <div class="form-group">
          <label for="apellidos" class="col-sm-2  col-xs-4 col-xs-offset4 col-sm-offset-2  control-label">Apellidos</label>
          <div class="col-sm-10">
            <input type="apellidos" class="form-control" name="apellidos" id="apellidos" placeholder="apellidos" required>
          </div>
          <div class="form-group">
          <label for="correo" class="col-sm-2 col-xs-4 col-xs-offset4 col-sm-offset-2  control-label">correo</label>
          <div class="col-sm-10">
            <input type="correo" class="form-control" name="correo" id="correo" placeholder="correo" required>
          </div>
          <div class="form-group">
          <label for="usuario" class="col-sm-2  col-xs-4 col-xs-offset4 col-sm-offset-2  control-label">Telefono</label>
          <div class="col-sm-10">
            <input type="telefono" class="form-control" name="telefono" id="telefono" placeholder="telefono" required>
          </div>
        <div class="form-group">
          <label for="direccion" class="col-sm-2 col-xs-4 col-xs-offset4 col-sm-offset-2  control-label">Direccion</label>
          <div class="col-sm-10">
            <input type="direccion" class="form-control" name="direccion" id="direccion" placeholder="direccion" required>
          </div>
          <label for="usuario" class="col-sm-2 col-xs-4 col-xs-offset4 col-sm-offset-2  control-label">Usuario</label>
          <div class="col-sm-10">
            <input type="usuario" class="form-control" name="usuario" id="usuario" placeholder="usuario" required>
          </div>
        </div>
        <div class="form-group">
          <label for="usuario" class="col-sm-2  col-xs-4 col-xs-offset4 col-sm-offset-2 control-label">Password</label>
          <div class="col-sm-10">
           <input type="password" class="form-control" name="password" id="password" placeholder="Password" required>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">REGISTRARSE</button>
          </div>
        </div>
      </form>
    </div>
  </body>
</html>


con este hago la conexion:



    <?php
     $host_db = "localhost";

     $usuario= "root";

     $pass = "";

     $bd="trabajofinal";
     $tabla="usuarios";

     $form_pass = $_POST['password'];
     $hash = password_hash($form_pass, PASSWORD_BCRYPT);
     $conexion = new mysqli($host_db, $usuario, $pass, $bd);
     if ($conexion->connect_error) {

     die("La conexion falló: " . $conexion->connect_error);

    }
     $buscarUsuario = "SELECT * FROM $usuarios

     WHERE  usuario = '$_POST[username]'";
     $result = $conexion->query($buscarUsuario);
     $count = mysqli_num_rows($result);
     if ($count == 1) {
     echo "<br />". "El Nombre de Usuario ya a sido tomado." . "<br />";
     echo "<a href='registrodeusuarios.php'>Por favor escoga otro Nombre</a>";

     }
     else{
     $query = "INSERT INTO usuarios (usuario, password)

               VALUES ('$_POST[username]', '$hash')";
     if ($conexion->query($query) === TRUE) {


     echo "<br />" . "<h2>" . "Usuario Creado Exitosamente!" . "</h2>";
     echo "<h4>" . "Bienvenido: " . $_POST['username'] . "</h4>" . "\n\n";
     echo "<h5>" . "Hacer Login: " . "<a href='logincorregido.php'>Login</a>" . "</h5>";

     }

     else {

     echo "Error al crear el usuario." . $query . "<br>" . $conexion->error;

       }

     }
     mysqli_close($conexion);

    ?>

con esto hago los post pero no sucede nada no me guarda a la bd

    <?php
$host_db = "localhost";
$usuario= "root";
$pass = "";
$bd="trabajofinal";
$tabla="usuarios";
$id=$_POST["id"];
$nombre=$_POST["nombre"];
$apellidos=$_POST["apellidos"];
$correo=$_POST["correo"];
$telefono=$_POST["telefono"];
$direccion=$_POST["direccion"];
$usuario=$_POST["usuario"];
$password=$_POST["password"];


$conexion = mysqli_query($host_db, $usuario, $pass, $bd);
$mysqli_select_db("trabajofinal");



$insertar="INSERT INTO usuarios(id,nombre,apellidos,telefono,correo,usuario,password) VALUES('$id','$nombre','$apellidos','$correo','$telefono','$direccion','$usuario','$password')";

mysqli_query($conexion);




if(!$resultado){
echo 'Error al registrarse';

}else{


echo 'Usuario registrado exitosamente';

}

mysqli_close($conexion);



?>

This is the database script

  -- phpMyAdmin SQL Dump
-- version 4.7.9
-- https://www.phpmyadmin.net/
--
-- Servidor: 127.0.0.1
-- Tiempo de generación: 25-04-2018 a las 21:23:22
-- Versión del servidor: 10.1.31-MariaDB
-- Versión de PHP: 7.2.3

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Base de datos: 'trabajofinal'
--

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla 'almancen'
--

CREATE TABLE 'almancen' (
  'idarticulo' varchar(45) NOT NULL,
  'cantidad' int(45) NOT NULL,
  'fechaderegistro' varchar(45) NOT NULL,
  'hrdesalida' varchar(45) NOT NULL,
  'foliofactura' varchar(45) NOT NULL,
  'proveedor' varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla 'empresas'
--

CREATE TABLE 'empresas' (
  'id' int(45) NOT NULL,
  'nombre' varchar(45) NOT NULL,
  'direccion' varchar(45) NOT NULL,
  'tipo' varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla 'provedores'
--

CREATE TABLE 'provedores' (
  'id' int(45) NOT NULL,
  'nombre' varchar(45) NOT NULL,
  'direccion' varchar(45) NOT NULL,
  'telefono' varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla 'servicios'
--

CREATE TABLE 'servicios' (
  'tipo' varchar(45) NOT NULL,
  'duracion' varchar(45) NOT NULL,
  'precio' int(45) NOT NULL,
  'Descripcion' varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla 'usuarios'
--

CREATE TABLE 'usuarios' (
  'id' int(45) NOT NULL,
  'nombre' varchar(45) NOT NULL,
  'apellidos' varchar(45) NOT NULL,
  'correo' varchar(45) NOT NULL,
  'telefono' int(45) NOT NULL,
  'direccion' varchar(45) NOT NULL,
  'usuario' varchar(45) NOT NULL,
  'password' varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

INSERT INTO 'usuarios' ('id', 'nombre', 'apellidos', 'correo', 'telefono', 'direccion', 'usuario', 'password') VALUES
(40444, 'nose', 'wdwdwd', 'dwdwdwd', 12121, 'sqwdwdw', 'nose', '1234'),
(40451, '', '', '', 0, '', '', '$2y$10$iA/JTHMLr7cmwhF7uiiajehtB6ntLju1i3nsGf');

--
-- Índices para tablas volcadas
--

--
-- Indices de la tabla 'almancen'
--
ALTER TABLE 'almancen'
  ADD PRIMARY KEY ('idarticulo');

--
-- Indices de la tabla 'provedores'
--
ALTER TABLE 'provedores'
  ADD PRIMARY KEY ('id');

--
-- Indices de la tabla 'servicios'
--
ALTER TABLE 'servicios'
  ADD PRIMARY KEY ('tipo');

--
-- Indices de la tabla 'usuarios'
--

This error is what I do not understand why it should be:

  

Fatal error: Uncaught Error: Function name must be a string in

    
asked by wdwd 25.04.2018 в 21:28
source

1 answer

2

Let's see, in addition to the other errors that may be in the code, I would say that the biggest is here:

$conexion = mysqli_query($host_db, $usuario, $pass, $bd);

$mysqli_select_db("trabajofinal");

$insertar="INSERT INTO usuarios(id,nombre,apellidos,telefono,correo,usuario,password) VALUES('$id','$nombre','$apellidos','$correo','$telefono','$direccion','$usuario','$password')";

mysqli_query($conexion);

The connection is not made with mysqli_query, but with:

$conexion = mysqli_connect($host_db, $usuario, $pass, $bd);

That you also have it well above in the code. If you make the new connection with the $ db variable, you do not need to do a mysqli_select_db right after, just change:

$bd = 'trabajofinal'

Before the call to connect.

And finally, you probably want to do:

mysqli_query($conexion, $insertar);

Not with $ connection.

I would also advise you to change it to

$result = mysqli_query($conexion, $insertar);

if(!$result){ 
echo "Error " . $conexion->error;
}

And so you control if an error occurred during execution.

Anyway I advise you to check the code, especially the operation of the functions you use and if the syntax is correct.

Edit: I've noticed that you have a mix of mysqli-oriented and procedural-oriented calls. I have corrected my code. I also advise you to document the difference between the two.

    
answered by 25.04.2018 / 23:25
source