dqeznd1697 2015-07-03 15:35
浏览 13
已采纳

SELECT然后使用PHP将数据插入MySQL [关闭]

I am trying to check if there is a entry from today and if not then add one, if it already exists then not. Current code is:

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql .= "SELECT plusamount AS plustotal, minusamount AS minustotal FROM   Juuli WHERE reg_date >= CURRENT_DATE() ORDER BY id DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "It already exists!";
} else {
    echo "autowrite should run";
    $sql .= "INSERT INTO Juuli(minusamount) VALUES (0); ";
}
$conn->close();
?>
  • 写回答

1条回答 默认 最新

  • douyu9433 2015-07-03 15:45
    关注

    If your SELECT statement is working the way you want it to directly from the database, the following should work. (notice the replacement of .= with =)

    $sql = "SELECT plusamount AS plustotal, minusamount AS minustotal FROM Juuli WHERE reg_date >= CURRENT_DATE() ORDER BY id DESC";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        echo "It already exists!";
    } else {
        echo "autowrite should run";
        $sql2 = "INSERT INTO Juuli (minusamount) VALUES (0)";
        $result2 = $conn->query($sql2);
    }
    $conn->close();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式