Unable to insert new data into database but can fetch and edit data

Hi everyone! here is my username: epiz_25251062

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); } ?>

Here is my code to insert data:

Can you turn on show PHP errors in the Control Panel and let us know what the error message is?
Thanks

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.

Relevant PHP docs:

https://www.php.net/manual/en/mysqli.error.php

Could you teach me how to turn on PHP error in the Control Panel?

You can enable PHP error messages from the control panel:

  1. Login to your control panel.
  2. Go to Alter PHP Config.
  3. Select the domain name you are trying to debug and click Alter PHP Directives.
  4. Set “Display Errors” to “On” and click the Alter PHP Directives to save.
2 Likes

I already turned “on” at control panel. Where can I see the error message?

The page or file where your invoke the SQL statement

You can’t, because you’re not checking for it.

Please see and follow.

The display PHP error option only shows PHP errors, not database errors.

1 Like

Error No: 1366 - MySQL error Incorrect integer value: ‘’ for column ‘reportID’ at row 1
Query:
INSERT INTO report_2021 (reportID,year,month,ec120,mission,raisedby,penalty,defectWP,dateWP1,dateWP2,defectOOP,dateOOP1,dateOOP2,jobNoWP,jobNoOOP) VALUES (‘’,‘2021’,‘December’,‘M103-05’,‘FMC’,‘NONE’,‘YES’,‘’,‘’,‘’,‘’,‘’,‘’,‘’,‘’)
#0 {main}

this is PHP error that display. FYI, reportID in my database is auto-increment.

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’,’’,’’,’’,’’,’’,’’,’’,’’)
1 Like

Yes, I already done that, but when I insert a new data in database its duplicate the same data but different ID.

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.

2 Likes

My code work fine now. I appreciate your help so much :wink:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.