选择然后使用PHP将数据插入MySQL

时间:2015-07-03 15:35:11

标签: php mysql

我正在尝试检查今天是否有条目,如果没有,则添加一个,如果它已经存在则不然。 目前的代码是:

$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 个答案:

答案 0 :(得分:1)

如果您的SELECT语句按照您希望的方式直接从数据库运行,则以下内容应该有效。 (注意用.=替换=

$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();