update 08/06/2018 22:59
This part is totally expendable and is of no use
$vem = 1;
$vem++;
$vem = 'campo'.$vem;
For the reasons listed below
It is quite obvious that as it stands, the variable $vem
will always be campo2
If you want string column names, there is no need to type any number in the field to be sent via post. Example, suppose that the number 8 has been typed and that we have the columns field1, field.2 and field.3 in the table, so the next column to be created is field.4 and the number sent via post was not used for nothing.
This variable $vem
from $_POST['nome'];
can be, for example, a prefix of the names of the columns to be created, allowing you to create columns with several prefixes and sequentially, such as field1, field.2 , etc ... or qqnome.1, qqnome.2, etc ... and such
It is not common to name columns with .
and this can cause errors if not handled correctly in the ALTER TABLE
statement. In this case it is imperative to wrap the column name with inverted quotes that are nothing more than the accent crase of your keyboard. More details on Schema Object Names
$sql = "ALTER TABLE $table ADD '$nomeProxColuna' varchar(255)";
Proposed code
with Mysqli
because mysql_*
has been deprecated and
Column names without .
$vem = $_POST['nome'];
if (isset($vem)){
$hostname="localhost";
$db_user="USUARIO";
$db_pw="SENHA";
$db_name = "nome_DB";
$con = mysqli_connect('localhost',$db_user,$db_pw,$db_name);
//nome da tabela
$table = 'artigos';
/**
* Obtem os nomes das colunas com prefixo = $vem (vindo do post)
**/
$sql = 'DESCRIBE '.$table;
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_assoc($result)) {
//cria array com nomes das colunas que contem a palavra $vem
$texto = $row['Field'];
if (strstr($texto, $vem)){;
$rows[] = $row['Field'];
}
}
/********** caso haja nomes de colunas com prefixo vindo do post
prepara o nome da proxima coluna *****************************/
if($rows){
/*****retorna maior valor do array dado pela parte numérica
já que a parte anterior à numérica é/são igual(is) *******/
$maior = max($rows);
/********próximo nome da coluna********/
//comprimento da variavel vindo do post
$len=strlen($vem);
//pega a parte numérica do nome dado pela variável $maior definida acima
$proxNum = substr($maior,$len)+1;
//cria o nome da próxima coluna
$nomeProxColuna = $vem.$proxNum;
}else{
//se não existem colunas com prefixo vindo do post cria a primeira
$nomeProxColuna=$vem."1";
}
// altera a estrutura da tabela acrescentando campos
$sql = "ALTER TABLE $table ADD '$nomeProxColuna' varchar(255)";
$incluirColuna = $con->query($sql);
if ($incluirColuna) {
echo "coluna criada com sucesso\n";
} else {
echo "deu zebra: " . $con->error . "\n";
}
}
If the column name prefixes will always be the same, just put it as the value of the $vem
variable. Example $vem="campo";
and the first few lines of code, for example, are
if (isset($_POST['criarColuna'])) {
$vem="campo";
and in the form only one input / button, example <input type="submit" name="criarColuna" value="Criar coluna">