Inserting column in mysql database table and after doing the update in the same column created

Asked

Viewed 26 times

0

I’m developing a system:

1- The "USER" of the system registers a service for a particular client who contracted that service.
2- This registration is done via a form.php...
3- Send this data directly to a SCRIPT.PHP and automatically create a column in the client table referencing the service the client hired.
4- With this, each registered customer will have one or several services registered in the bank in its client table.

Come on! What’s going on... I can make an ALTER TABLE in the client table and I can create the column of the "contracted service"... But I cannot automatically insert the text related to the service in the column of the "contracted service" for each client... Follows the code..

// DADOS vindo do form
$ID_CLIENTE_ACB = trim($_POST['ID_CLIENTE_ACB']);
$servicos = $_POST['campoServico'];
$servicosAdd = implode($servicos);
// --------------------------------

$servicosAdd = str_replace(" ", "_", $servicosAdd);

//Tirando os acentos dos serviços para criar a COLUNA no BANCO MYSQL
function tirarAcentos($servicosAdd){

    return preg_replace(array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/"),explode(" ","a A e E i I o O u U n N"),$servicosAdd);
}

$servicosAdd = tirarAcentos($servicosAdd);
$servicosAddM = strtoupper($servicosAdd);

//ALTERAR TABELA BANCO DE DADOS MYSQL()
$alterTableSql = "ALTER TABLE cad_cadastro_cliente_acb ADD SERV_".$servicosAddM." VARCHAR(40)";
$usuario_conect = mysqli_query($connect, $alterTableSql);

//INSERIR SERVIÇO NA TABELA CRIADA
$UpdateSql = "UPDATE cad_cadastro_cliente_acb SET SERV_$servicosAddM= $servicosAdd WHERE ID_CLIENTE_ACB = $ID_CLIENTE_ACB";

$usuario_conect = mysqli_query($connect, $UpdateSql);

echo"<script language='javascript' type='text/javascript'>
alert('Serviço Cadastrado com sucesso!');
window.history.back();
</script>";
die();

mysqli_close ($usuario_conect);

Thank you and I need help !!! kkkk

  • script alter table in php code? this is very strange, the code should already run with the table with the final model, this code does not make sense. About the update, have you seen if the command in the variable $UpdateSql that is correct?

  • Dude, the services are inserted in the client table dynamically via web... So the update... Would you have any better suggestions ? Hugs and thank you

  • the point is not the update, this is ok, as I put in the comment, checked if the value of the variable is ok? now do alter table? This is not put on a page, outside that your script neither validates if the column no longer exists, which will clearly give error one hour. Without really understanding what you do, you can’t help ,but I can tell you for sure that this alter table not well, could make an Insert with the values in any table, it would make more sense

  • Why not create a table to store the service and link the service ID to the customer? If you have more than one service use relational table N:N, which would be a simpler CLIENTE_SERVICO table, not to mention that it can receive an SQL INJECTION

  • Wonderful... I thought this option "Relational table"... There is already this table of services... I will make this customer relation = services... Personal thank you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.