如果列数小于2则插入记录,否则不插入mysql

时间:2016-12-14 11:20:57

标签: php mysql

只想添加记录,如果今天的日期列数小于2,如果count超过2,则不应插入db.It会在两条记录后继续添加。

$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(*) as totalupload FROM `daily_uploads` WHERE DATE_FORMAT(`date`, '%Y-%m-%d') = CURDATE()";


$row = mysqli_fetch_assoc($sql); 
$sum = $row['totalupload'];

if ($sum < 2 ) { 
     $sql = "INSERT INTO `daily_uploads` (img, geoplugin_city, geoplugin_regionName, amount)
   VALUES ('$img', '$city', '$region','$amount')";
     if ($conn->query($sql)) {
     echo ('success');
} else {
     echo ('error');
}

} else {

  echo"already exist";

2 个答案:

答案 0 :(得分:1)

在这样的计数查询后进行连接,

public class Config {
    public static final String JSON_ARRAY        = "result";
}

答案 1 :(得分:0)

    Try this one hope it will help you.

    $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(*) as totalupload FROM `daily_uploads` WHERE   DATE_FORMAT(`date`, '%Y-%m-%d') = CURDATE()";
    $qry= mysql_query($sql);
   $row = mysql_fetch_assoc($qry);
   $count = $row['totalupload'];
    if ($count < 2 ) { 
         $sql = "INSERT INTO `daily_uploads` (img, geoplugin_city, geoplugin_regionName, amount)
       VALUES ('$img', '$city', '$region','$amount')";
         if ($conn->query($sql)) {
         echo ('success');
    } else {
         echo ('error');
    }

    } else {

      echo"already exist";