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 inserting 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\agregar_event.php:19 Stack trace: #0 {main} thrown in E:\Users\ Bitnami\apache2\htdocs\gdlwebcamp\add_event.php on line 19
phpMyadmin image
![query table]: https://photos.app.goo.gl/x15Yj7HcVLsKN1t13
The error:
indicates that the variable
$stmt
whose methodbind_param
you are trying to call here:has a boolean value. That means that when you prepared the
$stmt
something went wrong and its current state is not a statement, but rather it isfalse
.Two things may be happening.
First
That the connection is null.
That's very easy to verify, plus it's recommended practice. Always evaluate the value of the connection before using it.
Second
That there is an error in the query: syntax error, error in the name of a table, a badly written column, etc. It is something to evaluate also in all code to know what happens.
Code implementing both solutions
You didn't put the
try
... because in the shared code snippet it doesn't make sense. I don't know if you use it for anything else.