-4
我有這個反饋表我做了,但我需要一些幫助。我想阻止人們發佈反饋,如果他們已經使用存儲在數據庫中的同一個ip進行反饋的話。下面是代碼:如何檢查一個IP是否已經存儲在mysql中?
<?php
if(isset($_POST['add'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$ip = $_SERVER['REMOTE_ADDR'];
$datetime = date('Y-m-d H:i');
$checkIp = mysql_query("SELECT ip from comments WHERE ip = '$ip'");
if (mysql_num_rows($checkIp) > 0) {
echo "Only 1 feedback per IP allowed!";
$IP = mysql_fetch_array($checkIp);
print_r($IP);
}
if($name){
if($email){
if($comment){
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
mysql_query("INSERT INTO comments (id, name, email, comment, ip, datetime) VALUES ('','$name','$email','$comment','$ip','$datetime')");
}
else
echo "The email address is invalid!<br><br>";
}
else
echo "You haven't entered any comment!<br><br>";
}
else
echo "You haven't entered an email address!<br><br>";
}
else
echo "You haven't entered your name!<br><br>";
}
我想有一個自己去,但沒有成功(你可以在我嘗試了一些功能上看到的),有人可以告訴我怎麼辦呢?
*不要*使用IP,它不是唯一的人類 –
此外,不要使用mysql_ *函數,因爲它們是官方棄用!,看看[預備聲明](http://php.net/manual/en/ mysqli.quickstart.prepared-statements.php) – Neat
如果你只需要一個,只需在表中的該字段設置一個「唯一」約束。 – developerwjk