Insert Query Error

Benn

Member
Nov 23, 2013
34
20
Hello,

After many attempts to solve, I cannot find the error with my Insert Query.

PHP Code:
PHP:
$stmm = $dbh->prepare("INSERT INTO hk_logs(staff,action,timestamp)
VALUES(:username, 'Logged in Successfully', '".time()."')");
$stmm->bindParam(':username', $_SESSION['username']);

I really hope I'm not being blind to a simple mistake.
All help is appreciated
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
What errors are you getting? Because I can see a few.

Sent from my SM-G928F using Tapatalk
 
Hello,

After many attempts to solve, I cannot find the error with my Insert Query.

PHP Code:
PHP:
$stmm = $dbh->prepare("INSERT INTO hk_logs(staff,action,timestamp)
VALUES(:username, 'Logged in Successfully', '".time()."')");
$stmm->bindParam(':username', $_SESSION['username']);

I really hope I'm not being blind to a simple mistake.
All help is appreciated
PHP:
$user = $_SESSION['username'];
$action = "Logged in successfully";
$time = time();

//Type of value getting inserted is depending on your database connection, whether or not it is a class, it is "$dbh->" or "PDO::" PARAM_STR
$stmt = $dbh->prepare("INSERT INTO `hk_logs` (`staff`,`action`,`timestamp`) VALUES (:u,:a,:t)");
$stmt->bindParam(':u', $user, $dbh->PARAM_STR);
$stmt->bindParam(':a', $action, $dbh->PARAM_STR);
$stmt->bindParam(':t', $time, $db->PARAM_STR);
$stmt->execute();
Depending on the type of insert, I always use array executing like so:
PHP:
$stmt = $dbh->prepare("INSERT INTO `hk_logs` (`staff`,`action`,`timestamp`) VALUES (:u,:a,:t)");
$data = [':u' => $user, ':a' => $action, ':t' => $time];
$stmt->execute($data);
 

Zaka

Programmer
Feb 9, 2012
471
121
Hello,

After many attempts to solve, I cannot find the error with my Insert Query.

PHP Code:
PHP:
$stmm = $dbh->prepare("INSERT INTO hk_logs(staff,action,timestamp)
VALUES(:username, 'Logged in Successfully', '".time()."')");
$stmm->bindParam(':username', $_SESSION['username']);

I really hope I'm not being blind to a simple mistake.
All help is appreciated
First of all I would suggest that you don't mix params with pure data. Now for the answer I think you get an error because you don't define the 3rd value in bindParam which is the data type. Personally I just use array inside of execute which is much easier. I would do it as follows.
PHP:
$stmm = $dbh->prepare("INSERT INTO hk_logs (staff, action, timestamp) VALUES (:username, :action, :timestamp));

$params = [
    "username" => $_SESSION['username'],
    "action" => "Logged in Successfully",
    "timestamp" => time()
];

$stmm->execute($params);

EDIT: It could also be the fact that you use time() directly. I don't know why, but sometimes I've gotten errors when I've used time() directly instead of saving it to a variable and then append the variable instead.
 

Brad

Well-Known Member
Jun 5, 2012
2,320
993
Here use this.

PHP:
 $stmt = $dbh->prepare("INSERT INTO hk_logs (staff,action,timestamp)  VALUES(:username, :action, :timestampp)");
                $timestamp2 = time();
                $stmt->bindParam(':username', $username);
                $stmt->bindParam(':action', $action);
                $stmt->bindParam(':timestampp', $timestamp2);
                $stmt->execute();
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
Here use this.

PHP:
 $stmt = $dbh->prepare("INSERT INTO hk_logs (staff,action,timestamp)  VALUES(:username, :action, :timestampp)");
                $timestamp2 = time();
                $stmt->bindParam(':username', $username);
                $stmt->bindParam(':action', $action);
                $stmt->bindParam(':timestampp', $timestamp2);
                $stmt->execute();
You forgot to add which type of param you're binding, otherwise its bindValue and not bindParam

Sent from my SM-G928F using Tapatalk
 

Users who are viewing this thread

Top