简体   繁体   English

如果列计数值为2,则将记录插入mysql表

[英]inserting record into mysql table if column count value is 2

Below is code for count record for today's date and inserting into database if value is not more than two per day.now problem is I want to insert into database if count for today is below 2. 下面是今天日期计数记录的代码,如果每天的值不超过两个,则插入数据库中。现在的问题是,如果今天计数小于2,我想插入数据库中。

$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$img = $_POST['img'];
$amount = 5;

$sql = "SELECT COUNT(*) FROM `daily_uploads` WHERE DATE_FORMAT(`date`, '%Y-%m-%d') = CURDATE()";

$result = $conn->query($sql);

if ($result->num_rows > 2) {

    echo"already exist";
    echo "Error: " . $sql . "<br>" . $conn->error; 


} else {


    $sql = "INSERT INTO `daily_uploads` (img, geoplugin_city, geoplugin_regionName, amount)
   VALUES ('$img', '$city', '$region','$amount')";
         // echo "success";
}

You have forgotten to execute the insert $sql string, you can do it this way: 您忘记了执行插入$ sql字符串,可以通过以下方式进行操作:

if ($conn->query($sql)) {
     echo ('success');
} else {
     echo ('error');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM