MySQLi Query not submitting data to my database

2019-06-09 17:21发布

问题:

I'm having an issue with a query of MySQLi I'm trying to submit, but no errors are being reported, and absolutely nothing is happening. I've tried multiple methods for a fix, and nothing.

The code:

$IP = $_SERVER['REMOTE_ADDR'];
$ISP = GetISP($IP);
$Referrer = DetermineReferrer();
$Browser = GetBrowser();
$OS = GetOS();
$Time = Date("F/j/Y, g:i a");

If($IP){
    mysqli_query($Connection, "INSERT INTO tracker (ip, isp, referrer, browser, os, time)
                               VALUES ('mysqli_real_escape_string($IP)',    
                                       'mysqli_real_escape_string($ISP)',
                                       'mysqli_real_escape_string($Referrer)',
                                       'mysqli_real_escape_string($Browser)',
                                       'mysqli_real_escape_string($OS)',
                                       'mysqli_real_escape_string($Time)')");
}

mysqli_close($Connection);
?>

The If($IP) was actually a temporary test; that's where I'll be checking if an IP is already in the database, which if possible could you lead me on a track for that?

I appreciate all feedback, whether negative or positive, it can still be helpful.

Edit: I'm going to supply the connect script, although it shouldn't be the problem as an error would display

<?php
Define('Host', 'localhost');
Define('Username', 'username');
Define('Password', 'password');
Define('Database', 'database name');

$Connection = mysqli_connect(Host, Username, Password, Database);
?>

Out of curiosity, while researching I seen something about using SET instead of VALUES. Could that have anything to do with this?

回答1:

You need to use concatenation if you're going to use function calls to construct your string.

mysqli_query($Connection, "INSERT INTO tracker (ip, isp, referrer, browser, os, time)
                           VALUES ('".mysqli_real_escape_string($Connection,$IP)."',    
                                   '".mysqli_real_escape_string($Connection,$ISP)."',
                                   '".mysqli_real_escape_string($Connection,$Referrer)."',
                                   '".mysqli_real_escape_string($Connection,$Browser)."',
                                   '".mysqli_real_escape_string($Connection,$OS)."',
                                   '".mysqli_real_escape_string($Connection,$Time)."')");


回答2:

I fixed the problem (and thought this would be the best way to respond.)

So apparently, in my query I needed to do:

$Query = mysqli_query($Connection, "INSERT INTO `tracker` (`ip`, `isp`, `referrer`, `browser`, `os`, `time`) VALUES ('$IP', '$ISP', '$Referrer', '$Browser', '$OS', '$Time')");

Like that, and the code is submitting my IP, along with returning 1 on the page.

Thanks to all who helped, I appreciate it.



标签: php mysqli