I am trying to get log in details from the user once they have logged in, into a server. I have attempted to use this:
$conn = new PDO("mysql:host=localhost;dbname=user_login",'root','');
global $dbh;
$stmt = $dbh->prepare("INSERT INTO login_details (browser, date_time, ip_address, username) VALUES (:browser, :date_time, :ip_address, :username)");
$stmt->bindParam(':browser', $browser);
$stmt->bindParam(':date_time', $date_time);
$stmt->bindParam(':ip_address', $ip_address);
$stmt->bindParam(':username', $username);
$browser = 'one';
$value = $_SERVER["HTTP_USER_AGENT"];
$stmt->execute();
$date_time = 'one';
$value = date('l jS \of F Y h:i:s A');
$stmt->execute();
$ip_address = 'one';
$value = $_SERVER['REMOTE_ADDR'];
$stmt->execute();
$username = 'one';
$value = $name;
$stmt->execute();
Sorry if I have got this completely wrong, I am completely new to this.
Thanks
In your script you are using $conn
as the object connecting to the database, so why are you using the global $dbh
. Just use $conn
in place of $dbh
.
As stated in another answer, you need to be consistent with your calls to your PDO object. Use $conn
instead of $dbh
, or vice versa.
Your current code also executes your SQL query mutliple times, specifically each time you call $stmt->execute()
. In most of the cases, you haven't even assigned values to your bound parameters yet.
Your $value
variables also missed the mark. You simply need to assign the desired value to the variables named in the bindParam
calls.
Try this
$conn = new PDO("mysql:host=localhost;dbname=user_login",'root','');
$stmt = $conn->prepare("INSERT INTO login_details (browser, date_time, ip_address, username) VALUES (:browser, :date_time, :ip_address, :username)");
$stmt->bindParam(':browser', $browser);
$stmt->bindParam(':date_time', $date_time);
$stmt->bindParam(':ip_address', $ip_address);
$stmt->bindParam(':username', $username);
$browser = $_SERVER["HTTP_USER_AGENT"];
$date_time = date('l jS \of F Y h:i:s A');
$ip_address = $_SERVER['REMOTE_ADDR'];
$username = $name;
$stmt->execute();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.