1
<?php

$ip = $_SERVER['REMOTE_ADDR'];
$date = date("d/m/y : H:i:s", time());

$dbc = mysqli_connect('localhost', 'root', 'derp', 'derpdb')
  or die("Database connection fried.");

$query = "INSERT INTO ipstore (tstamp, ip), " .
  "VALUES ('$date', '$ip')";

mysqli_query($dbc, $query);

mysqli_close($dbc);

?>

Can anyone tell me what's wrong with this code? It's meant to store the users IP/date they requested the page in the database. I've tried replacing localhost with 127.0.0.1, no luck. It doesn't bring a message, so it must be connected, however when it comes to querying it just doesn't do it. And it doesn't give a warning. I've checked the DB, nothings there.

Also don't worry, nothing sensitive is there ;)

Thanks

2 Answers 2

1
$query = "INSERT INTO ipstore (tstamp, ip), " . "VALUES ('$date', '$ip')";

You are not supposed to use a comma after specifying columns - try

$query = "INSERT INTO ipstore (tstamp, ip) VALUES ('$date', '$ip')";
Sign up to request clarification or add additional context in comments.

5 Comments

Now I have this: Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /var/www/siet/index.php on line 11
You have not changed anything but the $query-variable, correct? mysqli_query($dbc, $query); is still the correct way to execute the query, despite a wrongful answer given earlier.
Ah, I changed the mysqli_query();. Thanks, it now adds things to the DB, however the IP address is completely wrong...
Stored IP is 173.245.53.217
I recommend that you create a new question if you have a new issue - in my opinion this questions should be considered as solved.
0

try it this way

    $query = mysql_query("INSERT INTO ipstore (tstamp,ip) VALUES ('$date', '$ip')") or die(mysql_error()); if($query) {echo 'Success'; esle { echo 'Failed'; }

And you will get success for sure

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.