update 08/06/2018 22:59
This part is totally expendable and serves no purpose
$vem = 1;
$vem++;
$vem = 'campo'.$vem;
for the reasons listed below
- it’s pretty obvious that the way it is, the variable
$vem
will always be campo2
- If you want sequence column names there is no reason to enter any number in the field to be sent via post. For example, suppose that the number 8 has been entered and we have the columns field.1, field.2 and field.3 in the table, so the next column to be created is field.4 and the number sent via post has not been used for anything.
This variable $vem
from $_POST['nome'];
can be, for example, a prefix of the names of the columns to be created, giving margin to create columns with multiple prefixes and sequentially, such as field.1, field.2, etc.. or qqnome.1, qqnome.2, etc...
It is not common to name columns with .
and this can cause errors if not treated correctly in the declaration ALTER TABLE
. In this case it is essential to wrap the column name with inverted quotes that are nothing more than the crase accent of your keyboard. More details on Schema Object Names
$sql = "ALTER TABLE $table ADD `$nomeProxColuna` varchar(255)";
Proposed code
with Mysqli
because mysql_*
was discontinued 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 column names prefixes will always be the same, just put it as variable value $vem
. Example $vem="campo";
and the first lines of the code, for example, are
if (isset($_POST['criarColuna'])) {
$vem="campo";
and in the form only an input/button, example <input type="submit" name="criarColuna" value="Criar coluna">
Try to get the names of the columns first. Take a look at this reply, in that and in that
– adventistaam
actually I just need to know if you have the column, the column name I’m changing when comes the POST, type a select and then a query
– Evandro Aguiar