获取MySQL数据库中的IP地址

时间:2016-02-13 12:42:24

标签: php mysql

我试图创建一个数据库,我可以存储人们的IP地址,因此相同的IP地址无法订阅两次。我已经知道如何让某人知道IP地址,但我不知道如何将其插入我的数据库。这是我的代码:      

 // Connect to MySQL
 $mysqli = new mysqli( '***', '***', '***', 'inschrijven' );

// Check our connection
if ( $mysqli->connect_error ) {
die( 'Kan niet verbinden met database. Probeer het later opnieuw. ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}

// Insert our data

$sql = "INSERT INTO inschrijven ( naam, email, aantal,ip) VALUES ( '{$mysqli->real_escape_string($_POST['naam'])}', '{$mysqli->real_escape_string($_POST['email'])}', '{$mysqli->real_escape_string($_POST['aantal'])}'";
     // validate agree unless you want to add 'checked' to one of the values

     $insert = $mysqli->query($sql);


 // Print response from MySQL
if ( $insert ) {
echo "U bent succesvol ingeschreven! U heeft bootnummer: {$mysqli->insert_id}. Op de naam: . U krijgt hiervan nog een bevesteging op uw mail, uw bootnummer kan nog veranderen. Hopelijk zien we u op de BotenBouwDag 2016!";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}


$mysqli->close();
}

?>


<HTML>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<title>
Welkom op de officiële site van de BotenBouwDag 2016
</title>
</head>
<body>

<form method="post" action="">
<input name="naam" id="naam" type="text">
<input name="email" id="email" type="email" autocomplete="off">
<input name="aantal" id="aantal" type="number">
<input type="radio" name="eten"> Ja 
<input type="radio" name="eten"> Nee
<input type="submit" id="submit" value="Verstuur Informatie">
</form>

<div id="ip"></div>
<div id="address"></div>
<script type="text/javascript">

$.get("http://ipinfo.io", function (response) {
$("#ip").html("IP: " + response.ip);



document.getElementById('city').value = response.city;
}, "jsonp");
</script>


</body>
</HTML>

1 个答案:

答案 0 :(得分:0)

如果我做对了。你应该把它添加到你的价值部分

VALUES ( '{$mysqli->real_escape_string($_POST['naam'])}', '{$mysqli->real_escape_string($_POST['email'])}', '{$mysqli->real_escape_string($_POST['aantal'])}','{$mysqli->real_escape_string($_SERVER['REMOTE_ADDR'])}'