SELECT and then INSERT data into MySQL using PHP

Silver Ringvee

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();
?>
dstudeba

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP insert data using mysql

From Dev

PHP insert data using mysql

From Dev

Using ajax and php to insert data to MySQL

From Dev

insert data from JSON into mysql using php

From Dev

Unable to insert data using mysql query in php

From Dev

Insert XML Data to MySQL Table Using PHP

From Dev

How to insert data to mysql using php

From Dev

insert data into mysql database using php

From Dev

Unable to insert data in a MySQL table using PHP

From Dev

Insert data from Javascript to MySQL using PHP

From Dev

Insert JSON data into MySQL using PHP

From Dev

Unable to insert data on mysql using php

From Dev

Select, Insert MySQL and PHP

From Dev

Insert data php mysql

From Dev

MySQL Insert INTO using SELECT from another table with additional data

From Dev

Error when inserting data in mysql using php insert.php

From Dev

Insert DATA into 2 tables using one Query using PHP and Mysql

From Dev

MySQL INSERT INTO using SELECT with TIMESTAMP

From Dev

MySQL INSERT record using SELECT

From Dev

INSERT value using SELECT in mysql

From Dev

INSERT value using SELECT in mysql

From Dev

Get and insert Data into MySQL database using PHP with xampp on localhost

From Dev

Can't insert all data in MySQL using PHP

From Dev

Need help on insert comma separated data in mysql database using PHP

From Dev

Not being able to Insert data into MYSQL tables using PHP

From Dev

can't insert data in a mysql database using php

From Dev

Insert data from multiple array form inputs using php and mysql

From Dev

Retrieve data from mysql database and insert it in a table using php

From Dev

Unable to Insert Data into MySQL Database using (PHP, AJAX, JQ)

Related Related

HotTag

Archive