0

Basically am sending a user through a post request from postman to my PHP script attached to my site the script is this one

    <?php
    include_once 'config.php';
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Headers: Accept, Content-Type, X-Requested-With');
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');
    header('Access-Control-Max-Age: 600');
    $ts = gmdate("D, d M Y H:i:s") . " GMT";
    header("Expires: $ts");
    header("Last-Modified: $ts");
    header("Pragma: no-cache");
    header("Cache-Control: no-cache, must-revalidate");

    $contacto_existe = $mysqli->prepare("SELECT id FROM contactos WHERE nombre = ?");
    $contacto_existe->bind_param("s", $_POST['nombre_contacto']);
    $contacto_existe->execute();
    $contacto_existe->store_result();
    
        if($contacto_existe->num_rows === 0){
        $contacto_existe->close();
        $insertar_nuevo_contacto = $mysqli->prepare("INSERT INTO contactos (nombre, fecha_primera_interaccion, fecha_ultima_interaccion) VALUES (?, now(), now())");
        $insertar_nuevo_contacto->bind_param("s", $_POST['nombre_contacto']);
        $insertar_nuevo_contacto->execute();
        $id_contacto = $mysqli->insert_id;
        $insertar_nuevo_contacto->close();
    } else {
        $contacto_existe->bind_result($id_contacto);
        $contacto_existe->fetch();
        $contacto_existe->close();
    }
    
    
    $insert_mensaje = $mysqli->prepare("INSERT INTO mensajes (celular_id, contacto_id, fecha,mensaje ) VALUES (?, ?, now(),?)");
    // $insert_mensaje->bind_param("s", $_POST['id_celular'], $id_contacto,$_POST['fecha_inicial'],$_POST['mensaje']);
    $insert_mensaje->bind_param("s", $_POST['id_celular'], $id_contacto,$_POST['mensaje']);
    $insert_mensaje->execute();
    $insert_mensaje->fetch();
    $insert_mensaje->close();
    
    
   

?>

Where at the end am trying to insert into the msg table (mensajes) the post values and the id_contacto (id_contact) from the previous query result but it seems to be working and I don't know what could be the problem

Dharman
  • 30,962
  • 25
  • 85
  • 135
Erraco
  • 138
  • 1
  • 12

1 Answers1

1

I just found the error. I had to specify in the bind_param method the type of values for that query in this case it should be

$insert_mensaje->bind_param("iis", $_POST['id_celular'], $id_contacto,$_POST['mensaje']);

where iis = integer, integer, string

Dula
  • 1,276
  • 5
  • 14
  • 23
Erraco
  • 138
  • 1
  • 12