Insert does not work

Asked

Viewed 93 times

1

I am trying to create a login log of my page, and store this information in the table stat however I am not succeeding and is not returning any error. I put an IF to signal if the inclusion was made.

My code is like this:


php connection.

<?php
date_default_timezone_set('America/Sao_Paulo'); 
define('HOST', 'localhost');
define('DB', 'kurmabi');
define('USER', 'root');
define('PASS', '');         

$conn = new PDO('mysql:host=' . HOST . ';dbname=' . DB . ';', USER, PASS);
?>

index php.

include_once("libraries/conexao.php");
ini_set('display_errors', true); 
error_reporting(E_ALL); 

    $usuario = 18;

    $navegador       = $_SERVER['HTTP_USER_AGENT'];
    $sessao      = session_id();
    $meuip           = $_SERVER['SERVER_ADDR'];
    $origem      = $_SERVER['QUERY_STRING'];
    $idioma      = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    $servidor        = $_SERVER['SERVER_SIGNATURE'];
    $visitada        = $_SERVER['REQUEST_URI'];
    $pais            = "<span id='country'></span>";
    $estado      = "<span id='state'></span>";
    $cidade      = "<span id='city'></span>";
    $lat             = "<span id='latitude'></span>";
    $long            = "<span id='longitude'></span>";
    $ip          = "<span id='ipv4'></span>";
    $hora_req       = date('H:i:s');
    $registro       = date('Y-m-d H:i:s');


    $sql = "INSERT INTO `stat` (`id_stat`, `navegador`, `ip_visitante`, `sessao`, `pais`, `estado`, `cidade`, `latitude`, `longitude`, `ip_meuservidor`, `hora_req`, `origem`, `idioma`, `servidor`, `visitada`, `registro`) VALUES (NULL, '$navegador', '$ip', '$sessao', '$pais', '$estado', '$cidade', '$lat', '$long', '$meuip', '$hora_req', '$origem', '$idioma', '$servidor', '$visitada', CURRENT_TIMESTAMP)";

    $insert = $conn->prepare($sql);

    if($insert->execute()){
        echo "Yuhuuu!!!";
    } else {
        echo "Não foi";
    }
  • You need to get the SQL error, the error_reporting does not inform it. Tries to give a print_r($insert->errorInfo()); in the else to see the error that returns. -> https://stackoverflow.com/questions/8776344/how-to-view-query-error-in-pdo-php

  • Have you already tested this query directly in the database ? If so, what was the result you got ?

1 answer

0

The best way to use PDO is not like this, use the bindValue or bindParam.

I’ll leave an example using bindValue

$conexao = new PDO('mysql:host=localhost;dbname=stackoverflow', 'root', '');

$usuario = 1;
$navegador = $_SERVER['HTTP_USER_AGENT'];

$sql = "UPDATE users SET navegador=:navegador WHERE id=:id";
$stmt = $conexao->prepare($sql);
$stmt->bindValue(":navegador", $navegador, PDO::PARAM_STR);
$stmt->bindValue(":id", $usuario, PDO::PARAM_INT);
$data = $stmt->execute();
if ($data)
  echo "Banco de dados atualizado com sucesso!";
else
  echo "Houve um erro ao tentar atualizar o banco de dados!";

remembering the name on query should be the same in bindValue

If you need help see the link: Pdostatement::bindValue

  • I was able to do the insertion with your instructions, but it didn’t have the desired effect. Ended up going to the tables the records ":name" instead of the variable $name... :(

  • and because in the query I didn’t say which bank to run.

  • Artsher, thanks for the answer, but it still didn’t work. My code looks like this: $connection = new PDO("mysql:host=localhost;dbname=kurmabi", 'root', '); $sql = "INSERT INTO stat (id_stat, navegador, usuario, (etc....)

  • the variables are $browser = $_SERVER['HTTP_USER_AGENT']; $user = 18; Do the insertion but not with the correct values. In the "browser" field, instead of entering "Mozilla/5.0 (Windows NT.." is being inserted ":browser"

  • I edited the above code, I also tested if you are still having problems might be something else. table row data type.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.