Error query from php, function bind_param ()

1

I need help with this:

   <?php
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
   include_once 'includes/funciones/funciones.php';
  session_start();
   usuario_autenticado();
  if(isset($_POST['submit'])) {
      $nombre = $_POST['nombre'];
      $fecha = $_POST['fecha'];
      $hora = $_POST['hora'];
      $id_cat = $_POST['categorias'];
      $id_invitado = $_POST['invitado'];

      require_once('includes/funciones/bd_conexion.php');

      if ($conn){
        $strSQL="SELECT cat_evento, COUNT(DISTINCT nombre_evento) FROM eventos INNER JOIN categoria_evento ON eventos.id_cat_evento=categoria_evento.id_categoria WHERE id_cat_evento = ?";
        $stmt = $conn->prepare($strSQL);

        if ($stmt){
            $stmt->bind_param('i', $id_cat); //Si $id_cat es numérico debes cambiar la "s" por una "i"
            $stmt->execute();
            $stmt->bind_result($categoria_evento, $total);
            $stmt->store_result();
            $stmt->fetch();
            (int) $total = $total;
            $total++;
            $clave = substr($categoria_evento, 0, 5) . "_" . $total;
            echo $clave;
        }else{
          echo "Consulta errónea ".$stmt->error;
        }

    }else{

      echo "La conexión es nula";
    }

  }else{

     echo "No hay datos en submit";
  }

 ?>


 <?php include_once 'includes/templates/header.php'; ?>


                <section class="seccion contenedor">
                    <h2>Agregar Evento</h2>
                    <p>Bienvenido <?php echo $_SESSION['usuario']; ?> </p>

                <?php include_once 'includes/templates/admin-nav.php'; ?>

                <form class="invitado" action="agregar_evento.php" method="post">

                    <div class="campo">
                        <label for="nombre">Nombre Evento:</label>
                        <input type="text" name="nombre" id="nombre" placeholder="Nombre" required>
                    </div>
                    <div class="campo">
                        <label for="fecha">Fecha Evento:</label>
                        <input type="date" name="fecha" id="fecha" required>
                    </div>
                    <div class="campo">
                        <label for="hora">Hora Evento:</label>
                        <input type="time" name="hora" id="hora" required>
                    </div>
                    <div class="campo">
                      <label for="categoria">Categoria: </label><br>
                    <?php
                      try {
                        require_once('includes/funciones/bd_conexion.php');
                        $sql = "SELECT * FROM 'categoria_evento'";
                        $res = $conn->query($sql);
                        while ($cat_eventos = $res->fetch_assoc()) {
                          echo '<input type="radio" name="categorias" value=' . $cat_eventos['id_categoria'] . '> ' . $cat_eventos['cat_evento'] . '<br/>';
                        }
                      } catch (Exception $e) {
                        echo "Error:" . $e->getMessage();
                      }
                      ?>
                    </div>

                    <div class="campo">
                      <label for="invitado">Invitado:</label>
                        <?php
                          try {
                            require_once('includes/funciones/bd_conexion.php');
                            $sql = "SELECT 'invitado_id', 'nombre_invitado', 'apellido_invitado' FROM 'invitados'";
                            $res_invitados = $conn->query($sql);
                            echo "<select name='invitado'>";
                            while ($invitados = $res_invitados->fetch_assoc()) { ?>
                              <option value="<?php echo $invitados['invitado_id'] ?> ">
                                <?php echo $invitados['nombre_invitado'] . " " . $invitados['apellido_invitado']; ?>
                              </option>
                          <?php }
                            echo "</select>";
                          } catch (Exception $e) {
                            echo "Error:" . $e->getMessage();
                          }
                        ?>
                    </div>

                  <div class="campo">
                    <input type="submit" name="submit" value="Agregar" class="button" >
                  </div>
                </form>


                <?php $conn->close(); ?>
                </section>


     <?php include_once 'includes/templates/footer.php'; ?>

this is the code without the insertion of your code

       <?php
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
      include_once 'includes/funciones/funciones.php';
      session_start();
      usuario_autenticado();
  if(isset($_POST['submit'])):
    $nombre = $_POST['nombre'];
    $fecha = $_POST['fecha'];
    $hora = $_POST['hora'];
    $id_cat = $_POST['categorias'];
    $id_invitado = $_POST['invitado'];

    try {
      require_once('includes/funciones/bd_conexion.php');
      $stmt = $conn->prepare(" SELECT cat_evento, COUNT(DISTINCT nombre_evento) FROM eventos INNER JOIN categoria_evento ON eventos.id_cat_evento=categoria_evento.id_categoria WHERE id_cat_evento = ?");
      $stmt->bind_param('d', $id_cat);
      $stmt->execute();
      $stmt->bind_result($categoria_evento, $total);
      $stmt->store_result();
      $stmt->fetch();
      (int) $total = $total;
      $total++;
      $clave = substr($categoria_evento, 0, 5) . "_" . $total;
      echo $clave;

      header('Location:agregar_evento.php?exitoso=1');
    } catch (Exception $e) {
      echo "Error:" . $e->getMessage();
    } /**/
  endif;
  ?>

  <?php include_once 'includes/templates/header.php'; ?>


                      <section class="seccion contenedor">
                          <h2>Agregar Evento</h2>
                          <p>Bienvenido <?php echo $_SESSION['usuario']; ?> </p>

                      <?php include_once 'includes/templates/admin-nav.php'; ?>

                      <form class="invitado" action="agregar_evento.php" method="post">

                          <div class="campo">
                              <label for="nombre">Nombre Evento:</label>
                              <input type="text" name="nombre" id="nombre" placeholder="Nombre" required>
                          </div>
                          <div class="campo">
                              <label for="fecha">Fecha Evento:</label>
                              <input type="date" name="fecha" id="fecha" required>
                          </div>
                          <div class="campo">
                              <label for="hora">Hora Evento:</label>
                              <input type="time" name="hora" id="hora" required>
                          </div>
                          <div class="campo">
                            <label for="categorias">Categoria: </label><br>
                          <?php
                            try {
                              require_once('includes/funciones/bd_conexion.php');
                              $sql = "SELECT * FROM 'categoria_evento'";
                              $res = $conn->query($sql);
                              while ($cat_eventos = $res->fetch_assoc()) {
                                echo '<input type="radio" name="categorias" value=' . $cat_eventos['id_categoria'] . '> ' . $cat_eventos['cat_evento'] . '<br/>';
                              }
                            } catch (Exception $e) {
                              echo "Error:" . $e->getMessage();
                            }
                            ?>
                          </div>

                          <div class="campo">
                            <label for="invitado">Invitado:</label>
                              <?php
                                try {
                                  require_once('includes/funciones/bd_conexion.php');
                                  $sql = "SELECT 'invitado_id', 'nombre_invitado', 'apellido_invitado' FROM 'invitados'";
                                  $res_invitados = $conn->query($sql);
                                  echo "<select name='invitado'>";
                                  while ($invitados = $res_invitados->fetch_assoc()) { ?>
                                    <option value="<?php echo $invitados['invitado_id'] ?> ">
                                      <?php echo $invitados['nombre_invitado'] . " " . $invitados['apellido_invitado']; ?>
                                    </option>
                                <?php }
                                  echo "</select>";
                                } catch (Exception $e) {
                                  echo "Error:" . $e->getMessage();
                                }
                              ?>
                          </div>

                        <div class="campo">
                          <input type="submit" name="submit" value="Agregar" class="button" >
                        </div>
                      </form>


                      <?php $conn->close(); ?>
                      </section>


      <?php include_once 'includes/templates/footer.php'; ?>

this is the error

  

Fatal error: Uncaught Error: Call to a member function bind_param () on   boolean in   E: \ Users \ Bitnami \ apache2 \ htdocs \ gdlwebcamp \ add_event.php: 19 Stack   trace: # 0 {main} thrown in   E: \ Users \ Bitnami \ apache2 \ htdocs \ gdlwebcamp \ add_event.php on line   19

image of phpMyadmin
! [see table]: link

    
asked by J. Medina 28.10.2017 в 17:10
source

1 answer

1

The error:

  

Fatal error: Uncaught Error: Call to a member function bind_param () on   boolean

indicates that the variable% co_of% whose $stmt method you try to call here:

$stmt->bind_param("s", $id_cat);

has a Boolean value. That means that when you prepared the bind_param something went wrong and its current status is not a statement, but it is $stmt .

Two things may be happening.

First

That the connection is null.

That is very easy to verify, besides, it is the recommended practice. Always evaluate the value of the connection before using it.

Second

There is an error in the query: Syntax error, error on behalf of a table, a misspelled column, etc. It is something to evaluate also in all code to know what happens.

Code implementing both solutions

You did not put the false ... because in the piece of shared code it does not make sense. I do not know if you use it for something else.

if(isset($_POST['submit'])) {
/*  echo "<pre>";
  var_dump($_POST);
  echo "</pre>"; */
  $nombre = $_POST['nombre'];
  $fecha = $_POST['fecha'];
  $hora = $_POST['hora'];
  $id_cat = $_POST['categorias'];
  $id_invitado = $_POST['invitado'];



    require_once('includes/funciones/bd_conexion.php');

    if ($con){
        $strSQL="SELECT cat_evento, COUNT(DISTINCT nombre_evento) FROM eventos INNER JOIN categoria_evento ON eventos.id_cat_evento=categoria_evento.id_categoria WHERE id_cat_evento = ?";
        $stmt = $conn->prepare($strSQL);

        if ($stmt){
            $stmt->bind_param("s", $id_cat); //Si $id_cat es numérico debes cambiar la "s" por una "i"
            $stmt->execute();
            $stmt->bind_result($categoria_evento, $total);
            $stmt->store_result();

        }else{

            echo "Consulta errónea ".$stmt->error;

    }

    }else{

        echo "La conexión es nula";
    }   

}else{

    echo "No hay datos en submit";
}   
  

NOTE: If this does not work, check that there is no problem with the connection to the database or the framework or   program that you are using to build your applications.

    
answered by 28.10.2017 / 17:35
source