Hello.
I have two tables. In the table "cadastre" I have the fields:
id - primary key - AUTO_INCREMENT ---- field name - VARCHAR 50 ---- field type - INT (11) - (field related to the "Type" table) ---- and field description - TEXT
And in the "Type" table I have the following fields:
** Primary id key field - AUTO_INCREMENT and field type - VARCHAR 20 - (INDEX) **
What happens is that these tables are part of a CRUD made in PHP PDO and when I relate the table "Type" to the table "Register" (FK) everything happens, however, I do not know if it is the PDO structure, the ajax that performs the data insertion does not work but if I delete the relationship between these tables it works again.
In case someone can tell me if this could be related to the fact that CRUD is developed in PHP PDO? Below is the PHP PDO code for inserting and updating CRUD:
<?php
include('db.php');
include('function.php');
if(isset($_POST["operation"]))
{
if($_POST["operation"] == "Add")
{
$image = '';
if($_FILES["user_image"]["name"] != '')
{
$image = upload_image();
}
$statement = $connection->prepare("
INSERT INTO users (tipo, nome, descricao, image)
VALUES (:tipo, :nome, :descricao, :image)
");
$result = $statement->execute(
array(
':tipo' => $_POST["tipo"],
':nome' => $_POST["nome"],
':descricao' => $_POST["descricao"],
':image' => $image
)
);
if(!empty($result))
{
echo 'Data Inserted';
}
}
if($_POST["operation"] == "Edit")
{
$image = '';
if($_FILES["user_image"]["name"] != '')
{
$image = upload_image();
}
else
{
$image = $_POST["hidden_user_image"];
}
$statement = $connection->prepare(
"UPDATE users
SET tipo = :tipo, nome = :nome, descricao = :descricao, image = :image
WHERE id = :id
"
);
$result = $statement->execute(
array(
':tipo_ps' => $_POST["tipo"],
':nome' => $_POST["nome"],
':descricao' => $_POST["descricao"],
':image' => $image,
':id' => $_POST["user_id"]
)
);
if(!empty($result))
{
echo 'Data Updated';
}
}
}
?>
Also put the code that receives the data via ajax and sends it to the insert.php file described above:
$(document).on('submit', '#user_form', function(event){
event.preventDefault();
var tipo = $('#tipo').val();
var nome = $('#nome').val();
var descricao = $('#descricao').val();
var extension = $('#user_image').val().split('.').pop().toLowerCase();
if(extension != '')
{
if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)
{
alert("Invalid Image File");
$('#user_image').val('');
return false;
}
}
if(nome != '' && descricao != '')
{
$.ajax({
url:"insert.php",
method:'POST',
data:new FormData(this),
contentType:false,
processData:false,
success:function(data)
{
alert(data);
$('#user_form')[0].reset();
$('#userModal').modal('hide');
dataTable.ajax.reload();
}
});
}
else
{
alert("Both Fields are Required");
}
});
Thanks for the attention :)