Mysql SELECT INSERT和UPDATE同时查询

时间:2015-04-03 05:27:13

标签: php database mysqli insert

您好我有一个名为“admin”的数据库,其中我有两个表 表1 Name =“register” 表2 Name =“noti”

在注册表中,我通过注册页面大约有超过10个用户条目 在Noti表中,此时为空(列名也是“noti”)

我想要表演这件事 首先,我想计算总数。 “注册”表中的记录 并检查,如果记录大于ZERO则运行INSERT查询,否则运行UPDATE查询

我想将这个计数值INSERT并更新为“noti”表

这是我的代码

<?php 
include('config.php');
$sql2 = "SELECT count(*) as count FROM register";
$result2 = mysqli_query($con, $sql2);
if($result2->num_rows>0)
{
while($rw1=$result2->fetch_array())
{
$value1 =  $rw1['count'];

$result = mysqli_query($con, "SELECT count(*) as count FROM register  ");

if(!empty($value1)) {
    mysqli_query($con, "UPDATE noti SET noti = '$value1' ");
}
else
{
    mysqli_query($con, "INSERT INTO noti(noti) VALUES ('$value1') ");
}
}
}
?>

1 个答案:

答案 0 :(得分:0)

使用此:

include('config.php');
$sql2 = "SELECT count(*) as count FROM register";
$result2 = mysqli_query($con, $sql2);
$count = $result->num_rows;
if($count != 0)
{
    mysqli_query($con, "UPDATE noti SET noti = '$count' ");
}
else
{
    mysqli_query($con, "INSERT INTO noti(noti) VALUES ('$value1') ");
}