How to Insert an indeterminate amount of users into an SQL Database?

Asked

Viewed 57 times

0

Hello guys I am beginner in PHP and SQL, I need to make a kind of form that the person informs how many people want to register, anyone have any idea? I can only do this with a fixed amount of variables

1 answer

-1


If you want when the person fills it create a new table with specific fields you do:

<?php

$mysqli = new mysqli("host", "user", "password", "db");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

// aqui você passa como a pessoa quer, no formulário ela precisará dizer se os dados são do texto, numeros, etc. Aí você separa cada dado, os tipos você envia num array para o post 'tipos', e o nome vc envia para os 'dados'. Na hora do envio você faz as variáveis a baixo um array ou então manda os dados com um separador: Ex  INT(5), VARCHAR(50),...  nesse ex o separador é a ','

$tipos = $_POST['tipos'];
$dados = $_POST['dados'];

//Dentro no '/ /' fica o separador que irá quebrar, no caso está o espaço em 
branco.
$tiposArray = preg_split('/ /', $tipos, -1, PREG_SPLIT_OFFSET_CAPTURE);
$dadosArray = preg_split('/ /', $dados, -1, PREG_SPLIT_OFFSET_CAPTURE);

//Definindo a query
$textoDaQuery;

for (int i = 0; i < count($tiposArray); i++) {
     $textoDaQuery += $dadosArray[i] + $tiposArray[i] + ",";
}

// então você faz:
// Ex de do $textoDaQuery:  nome VARCHAR(50), nascimento DATE
$query = "CREATE TABLE $nomedatabela($textoDaQuery)";

$mysqli->query($query);

So you create the table for the client with the fields and types it defined

  • I think that’s what you wanted to do, if not, responds here that I edit the answer

  • I did it! Very obg Woton Sampaio, I will post here the way I did.

  • looking for the way to put the code here

  • @L.Diego glad you could, if the answer was helpful, mark as accepted, so others who have the msm problem you can solve your problems also

Browser other questions tagged

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