2
<?php
    $conn = mysqli_connect('localhost', 'BinKill', 'password removed', 'tk_development');
    $ip = $_SERVER['REMOTE_ADDR'];
    $date = Date("d/mY, g:i:s");
    if(mysqli_connect_errno()){
        die("Connection failed: " . mysqli_connect_error());
    } else {
        $query = "INSERT INTO `logs` (ip,date) VALUES (`$ip`, `$date`)";
        $result = mysqli_query($conn, $query);
        if(!$result){
            die("Could not Execute Query: " . mysqli_error());
        }
        mysqli_close($conn);
    }

?>

Above is the code I'm using to try and log the IP of the user accessing the page and the date of which they accessed it. However when this is ran, all that is returned is: Could not Execute Query:

No error. Not sure what's going on exactly.

8
  • Try adding ini_set('display_errors',1); error_reporting(E_ALL); to the start of your PHP code and try, report back to me what you get. Commented Feb 11, 2015 at 17:58
  • Thanks for that little bit of code. Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/binkill/public_html/portfolio/index.php on line 13 Could not Execute Query: Commented Feb 11, 2015 at 17:58
  • Should be mysqli_error($conn) Commented Feb 11, 2015 at 17:59
  • Try this $query. I apologise for not using code formatting here, but it's not possible with that. Commented Feb 11, 2015 at 18:00
  • Ended up fixing the mysqli_error and it returned Could not Execute Query: Unknown column '66.97.29.2' in 'field list' I think I can figure it out from here. Will report back when done Commented Feb 11, 2015 at 18:00

2 Answers 2

1

Your error is the use of backticks(`) on the values. Only use backticks on the table and column names (only necessary for reserved keywords)

Change

$query = "INSERT INTO `logs` (ip,date) VALUES (`$ip`, `$date`)";

To

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

1 Comment

Moderator Note: I've removed the comments here. Comments should be used to discuss the topic of this answer, anything else should be taken to chat.
0

This is a case of a syntax error, but however your mysqli_* error code is incorrect as you never specified which connection to give an error for, it should be:

mysqli_error($conn)

instead of

mysqli_error()

You are also using backticks incorrectly, your correct code should be:

$query = "INSERT INTO `logs` (ip,date) VALUES ('$ip', '$date')";

which is why you were getting the error:

Could not Execute Query: Unknown column '66.97.29.2' in 'field list'

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.