Click here to Skip to main content
14,970,304 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
header("Content-Type: application/json;charset=utf-8");

if (!isset($_SESSION['id_usuario'])) {
    echo json_encode(["status" => false, "msg" => "Debes iniciar sesión para publicar un episodio"]);

require "../../img_upload_script.php";
require "../../audio_upload_script.php";

// Agarramos el json de la solicitud recibida
$json = $_POST["json"];

if (empty($json)) {
    echo json_encode(["status" => false, "msg" => "No se han proporcionado los datos necesarios"]);

// Convertimos el json recibido a un objeto PHP
$data = json_decode($json);

$link = new mysqli('localhost', 'root', '', 'madradio', 3306);

// preparamos y adjuntamos los parámetros
$stmt = $link->prepare("INSERT INTO publicaciones (titulo, descripcion, etiquetas, id_categoria, id_genero, id_usuario, url_imagen, url_audio, fecha) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssiiisss", $data->titulo, $data->descripcion, $data->etiquetas, $data->id_categoria, $data->id_genero, $_SESSION["id_usuario"], $target_file_image, $target_file_audio, date('Y-m-d'));

// ejecutamos

if ($stmt->affected_rows <= 0) {
    echo json_encode(["status" => false, "msg" => "No se ha creado la publicación"]);


echo json_encode(["status" => true, "msg" => "Publicación creada con éxito"]);

What I have tried:

Hello good evening,
I have a problem with the code I posted and have been sitting for hours to find the solution but I am not able to find the bug, everything in the code looks good to me I've revised every comma and the problem is in the bind_param section. Any help is much appreciated
Updated 4-Jun-21 16:58pm

1 solution

Bind parameters are expected to be variables so you cannot use return values from functions directly.

For example date('Y-m-d') is a function call so you should first assign it to a variable and then use that variable as the bind parameter.

Just a side note. It looks that you're storing date in a character format to the database. Why not store it as a date and then format it when fetching the data. This would make it much easier to operate on dates whenever needed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900