Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP mysqli insert

Tealk

Platinum Coder
Hi,

I don't know why but the following line doesn't work in the script.
Anybody have an idea why?

When I run the code manually it works and also the log shows everything correctly.
 
Can you check for each of the steps, prepare(), bind_param() and execute() that the result is not false? You need to know which step of the process is failing on. At each step, check that the result is not false, and if it is false output an error.

Also, when you say you run it manually and it works, is this the query without the prepared statements? I suspect either the prepare is failing or the data you think is integers may not be, but knowing which step it fails on would be helpful for debugging.
 
the data is correct, the whole thing is executed via webhook, so i don't get a return value
i write all steps with "fwrite" and as you can see here all data arrive correct
Code:
INSERT: INSERT INTO `services_status` (`id`, `service_id`, `status_id`) VALUES (NULL, '15', '58')
 
If you run that query against the DB does it insert correctly? If your ID is auto-incrementing I'm wondering if instead of including a null value for the ID, if instead you should just omit that field.
 
@Tealk I think you already solved your own problem, but for future Googlers here is an explanation -

the problem is in the database, this comes from having to fall back on existing databases

The error:
Cannot add or update a child row: a foreign key constraint fails

This is because whatever data this table is "related to" is missing data.
For example, I can have a table called "children" that stores a child ID, name, and a parent ID. My other table "parents" could have the parent ID and name. The "related table" would be the parents table's "id" column matching up to the children table's "parent_id" column. If you try to insert or update the children table with a parent ID that DOES NOT EXIST in parents, it will throw the foreign key constraint.

Basically what you are doing is trying to put data into TABLE2 and TABLE2 has a column that must match up to another column in TABLE1, but because TABLE1 does not have this column match, you can't insert into TABLE2.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom