My code were working fine on my localhost. But when I uploaded the codes into Infinityfree its not worked. When I insert new data it keeps saying “Error” seems like the data are not save into database. But, everything was fine on fetch and edit data. Can someone help me:)
My code for connection database:
<?php
$conn = new mysqli('sql113.epizy.com', 'epiz_25251062', 'xxxxxxx', 'epiz_25251062_drms');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
On the line where it says “Error!. Click OK”, could you please try logging/showing the value of $conn->error there? That should provide more detail as to why the query failed.
If you want to have an auto incrementing ID, you should leave reportID out of your insert statement and let MySQL generate it for you.
In your example, you specifically provide a reportID, with the value being an empty string. But a string is not an integer, so MySQL can’t write that value.
In other words, the query should look like this:
INSERT INTO report_2021 (year,month,ec120,mission,raisedby,penalty,defectWP,dateWP1,dateWP2,defectOOP,dateOOP1,dateOOP2,jobNoWP,jobNoOOP)
VALUES ('2021’,‘December’,‘M103-05’,‘FMC’,‘NONE’,‘YES’,’’,’’,’’,’’,’’,’’,’’,’’)
That’s how inserting data works. If you insert a row, it’s added to the database.
The way to prevent that is to add a UNIQUE constraint to one of the database columns (or a combination of them). That way, there cannot be multiple rows with the same (set of) value(s), and MySQL will return an error if you try that.
If you don’t want to get an error, you can run a query beforehand to check for the presence of the row, and perform other logic if that is the case. Or you can add an ON DUPLICATE KEY... segment to the query that can either ignore the insert or update the existing row.