Unable to Insert new data in database

epiz_21093665

I can access the old data but I am unable to add new data to the database.

The same code is working on localhost.
**There is no error message from the database/PHP, but because I am using “query check” before submitting the data code displays the error message → “Something went wrong, Please contact system administrator”(error message - if something went wrong during query execution).
** I can add data manually from PHPMyAdmin

So… is there an error message?

The message “something went wrong” doesn’t really help. “Please contact system administrator” with no details a system administrator can do something with isn’t great advice.

On top of what Admin said, sharing your code and the URL may also be helpful.

If (for any reason) there is a problem during the query execution, then the code returns an error message “Something went wrong, Please contact system administrator” → A custom variable I created.

And I think I got it, It has something to do with default values in Table(In my case at least).
**Try adding default values to empty fields (in your database)
Admin can you check with other developers (I saw some similar posts yesterday)

But, why now??

We can’t help you if you don’t tell us anything. We need your code, an actual error message, and perhaps a URL as well. Oh, it would also be useful if you tell us what your trying to do.

It’s already mentioned in the post–>There is no error message from the database/PHP(not a visible one at least)

I used the in-built error function to check for any hidden error (because of the above), and got an error “no default value set for the field (with field name)”

Here is the code–>

$insert_query = "INSERT INTO `data` (`var1`, `var2`,`var3`, `var4`) VALUES ('$var1', '$var2', '$var3', '$var4')";  
          
          if(mysqli_query($tcon,$insert_query)){
                                     $msg = "Data Uploaded Successfully"; 
                                        }
          }
             
              else{
                  $error = "Something went wrong, Please contact system administrator";
              }

It’s resolved now
I just want to know why this is happening all of a sudden

That’s because you’re not checking for error messages. If mysqli_query returns false, then you can use mysqli_error to retrieve the error message. That message will provide a lot more information about why the query fails.

Which function and where do you use it? I don’t see it in the code.

We don’t even know what’s happening yet. We need the actual database query error for that. Only then can we maybe say something about why your query fails.

1 Like

I am using mysqli error(). I removed it after adding default values in database.
And the error was Field ‘field_name’ doesn’t have a default value

So what is the error message returned by mysqli_error right now?

Because if the default value issue was the only issue and has been completely fixed, your query would just work. Since it doesn’t, either your fix didn’t work or there is another issue.

Yeah, It is working now and there is no error message (query is working without any problem) :grin:

I recreated the issue (just for error message sake)
error message

MySQL error() printed the underlined section of the image

Thank you, that error message provides a lot more information.

But it seems quite clear what the issue is, right? Your database table has a field val9 which does not have a default value, and you’re trying to enter a record it in which doesn’t have that value. Can you please verify that either the column has a default set or your query does try to write that column?

3 Likes

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