1
I have a code that is for inserting/changing/deleting columns and rows from a database table, it worked very well when it was by Mysql, I tried to make some changes to Mysqli, at first it presents no errors, but it also does not perform the actions. I would ask for the help of the staff to give a check so that I can change and fix this problem, follow the code used. Thanks for your help...
<?php
session_start();
include_once("conexao com meu db");
// NOME DESSE DOCUMENTO
$pagina = "manipulation_data.php";
// NOME DA TABELA USADA
$tabela = "tabela";
// INSERIR COLUNA
if(isset($_GET['acao']) && @$_GET['acao']=="inserir_coluna") {
mysqli_query($conn, "ALTER TABLE '".$tabela."' ADD '".$_GET['coluna']."'
VARCHAR(10) NOT NULL COMMENT '".$_GET['comentario']."' AFTER '".$_GET['apos']."'");
echo "<script>location.href='".$pagina."'</script>";
exit;
}
// EXCLUIR COLUNA
if(isset($_GET['excluir_coluna']) && @$_GET['excluir_coluna']!="") {
mysqli_query($conn, "ALTER TABLE '".$tabela."' DROP '{$_GET['excluir_coluna']}'");
echo "<script>location.href='".$pagina."'</script>";
exit;
}
// EXCLUIR LINHA
if(isset($_GET['acao']) && @$_GET['acao']=="excluir") {
mysqli_query($conn, "DELETE FROM '".$tabela."' WHERE id='{$_GET['id']}'");
echo "<script>location.href='".$pagina."'</script>";
exit;
}
// INSERIR LINHA
if(isset($_POST['inserir']) && @$_POST['inserir']!="") {
$colunas = Array(); $valores = Array();
foreach($_POST['linhas'] AS $l1 => $l2) {
$colunas[] = $l1;
$valores[] = "'$l2'";
}
mysqli_query($conn, "INSERT INTO ".$tabela." (".implode(",", $colunas).")
VALUES(".implode(",", $valores).")");
echo "<script>location.href='".$pagina."'</script>";
exit;
}
// SALVAR OS DADOS
if(isset($_POST['salvar']) && @$_POST['salvar']=="salvar") {
$update = "";
$up = Array();
foreach($_POST['linhas'] AS $l1 => $l2) {
$up[]="$l1='$l2'";
}
$update.= implode(",", $up);
mysqli_query($conn, "UPDATE ".$tabela." SET ".$update." WHERE
id='".array_values($_POST['linhas'])[0]."'");
echo "<script>location.href='".$pagina."'</script>";
exit;
}
echo "<form action=\"".$pagina."\" method=\"post\"><table border=\"1\">
<tr>
<td>Editar</td>
<td>Excluir</td>";
$valorcolumn = "SELECT a.COLUMN_NAME, a.COLUMN_COMMENT FROM
information_schema.COLUMNS a WHERE a.TABLE_NAME = '".$tabela."'";
$query = mysqli_query($conn, $valorcolumn);
$array_colunas = Array();
while($dados = mysqli_fetch_array($query)) {
extract($dados);
$array_colunas[] = $COLUMN_NAME;
echo "<td>$COLUMN_COMMENT <a href=\"".$pagina."?
excluir_coluna=$COLUMN_NAME\">excluir coluna</td>";
}
echo "</tr>";
$valorcoluna = "SELECT COUNT(*) AS colunas FROM information_schema.COLUMNS a
WHERE a.TABLE_NAME = '".$tabela."'";
$colunas = mysqli_query($conn, $valorcoluna);
$valortable = "SELECT * FROM tabela";
$query = mysqli_query($conn, $valortable);
while($dados = $query->fetch_array()){
echo "<tr>
<td><a href=\"".$pagina."?acao=editar&id={$dados[0]}\">Editar</a></td>
<td><a href=\"".$pagina."?acao=excluir&id={$dados[0]}\">Excluir</a></td>";
while ($data = mysqli_fetch_array($colunas, MYSQLI_ASSOC)) {
$data['colunas'];}
for($i = 0; $i < $data; $i++) {
echo "<td>{$dados[$i]}</td>";
}
echo "</tr>";
}
echo "<tr>
<td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"inserir\"
value=\"Inserir Nova Linha\"></td>";
while ($data = mysqli_fetch_array($colunas, MYSQLI_ASSOC)) {
$data['colunas'];}
for($i = 0; $i < $data; $i++) {
echo "<td><input type=\"text\" name=\"linhas[{$array_colunas[$i]}]\"
value=\"\" placeholder=\"valores, coluna id = vazio\"></td>";
}
echo "</tr>";
echo "</table></form>";
?>
<a href="<?php echo $pagina; ?>?
acao=inserir_coluna&coluna=coluna3&comentario=Coluna três&apos=coluna2">INSERIR NOVA COLUNA</a>
<br /><br />
<?php
if(isset($_GET['acao']) && @$_GET['acao']=="editar") {
echo "<form action=\"".$pagina."\" method=\"post\"><table>
<tr>";
$query = mysqli_query($conn, "SELECT a.COLUMN_NAME, a.COLUMN_COMMENT FROM
information_schema.COLUMNS a WHERE a.TABLE_NAME = '".$tabela."'");
while($dados = $query->fetch_array()) {
extract($dados);
echo "<td>$COLUMN_COMMENT</td>";
}
echo "</tr>";
$query = mysqli_query($conn, "SELECT * FROM ".$tabela." WHERE
id='{$_GET['id']}'");
while ($dados = $query->fetch_array()) {
echo "<tr>";
while ($data = mysqli_fetch_array($colunas, MYSQLI_ASSOC)) {
$data['colunas'];}
for($i = 0; $i < $data; $i++) {
echo "<td><input type=\"text\" name=\"linhas[{$array_colunas[$i]}]\"
value=\"{$dados[$i]}\"></td>";
}
echo "</tr>";
}
echo "</table><input type=\"submit\" name=\"salvar\" value=\"salvar\">
</form>";
}
?>
Mysql code as old...
<?php
include("conexao com meu db");
// NOME DESSE DOCUMENTO
$pagina = "manipulation_data.php";
// NOME DA TABELA USADA
$tabela = "tabela";
// INSERIR COLUNA
if(isset($_GET['acao']) && @$_GET['acao']=="inserir_coluna") {
mysql_query("ALTER TABLE `".$tabela."` ADD `".$_GET['coluna']."` VARCHAR(10)
NOT NULL COMMENT '".$_GET['comentario']."' AFTER `".$_GET['apos']."`");
echo "<script>location.href='".$pagina."'</script>";
exit;
}
// EXCLUIR COLUNA
if(isset($_GET['excluir_coluna']) && @$_GET['excluir_coluna']!="") {
mysql_query("ALTER TABLE `".$tabela."` DROP `{$_GET['excluir_coluna']}`");
echo "<script>location.href='".$pagina."'</script>";
exit;
}
// EXCLUIR LINHA
if(isset($_GET['acao']) && @$_GET['acao']=="excluir") {
mysql_query("DELETE FROM `".$tabela."` WHERE id='{$_GET['id']}'");
echo "<script>location.href='".$pagina."'</script>";
exit;
}
// INSERIR LINHA
if(isset($_POST['inserir']) && @$_POST['inserir']!="") {
$colunas = Array(); $valores = Array();
foreach($_POST['linhas'] AS $l1 => $l2) {
$colunas[] = $l1;
$valores[] = "'$l2'";
}
mysql_query("INSERT INTO ".$tabela." (".implode(",", $colunas).")
VALUES(".implode(",", $valores).")");
echo "<script>location.href='".$pagina."'</script>";
exit;
}
// SALVAR OS DADOS
if(isset($_POST['salvar']) && @$_POST['salvar']=="salvar") {
$update = "";
$up = Array();
foreach($_POST['linhas'] AS $l1 => $l2) {
$up[]="$l1='$l2'";
}
$update.=implode(",", $up);
mysql_query("UPDATE ".$tabela." SET ".$update." WHERE
id='".array_values($_POST['linhas'])[0]."'");
echo "<script>location.href='".$pagina."'</script>";
exit;
}
echo "<form action=\"".$pagina."\" method=\"post\">
<table border=\"1\">
<tr>
<td>Editar</td>
<td>Excluir</td>";
$query=mysql_query("SELECT a.COLUMN_NAME, a.COLUMN_COMMENT
FROM information_schema.COLUMNS a
WHERE a.TABLE_NAME = '".$tabela."'");
$array_colunas = Array();
while($dados=mysql_fetch_array($query)) {
extract($dados);
$array_colunas[] = $COLUMN_NAME;
echo "<td>$COLUMN_COMMENT <a href=\"".$pagina."?
excluir_coluna=$COLUMN_NAME\">excluir coluna</td>";
}
echo "</tr>";
$colunas = mysql_query("SELECT COUNT(*) AS colunas FROM
information_schema.COLUMNS a WHERE a.TABLE_NAME = '".$tabela."'");
$query=mysql_query("SELECT * FROM ".$tabela."");
while($dados=mysql_fetch_array($query)) {
echo "<tr>
<td><a href=\"".$pagina."?acao=editar&id={$dados[0]}\">Editar</a></td>
<td><a href=\"".$pagina."?acao=excluir&id={$dados[0]}\">Excluir</a></td>";
for($i = 0; $i < mysql_result($colunas , 0, "colunas"); $i++) {
echo "<td>{$dados[$i]}</td>";
}
echo "</tr>";
}
echo "<tr>
<td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"inserir\"
value=\"Inserir Nova Linha\"></td>";
for($i = 0; $i < mysql_result($colunas , 0, "colunas"); $i++) {
echo "<td><input type=\"text\" name=\"linhas[{$array_colunas[$i]}]\"
value=\"\" placeholder=\"valores, coluna id = vazio\"></td>";
}
echo "</tr>";
echo "</table></form>";
?>
<a href="<?php echo $pagina; ?>?
acao=inserir_coluna&coluna=coluna3&comentario=Coluna
três&apos=coluna2">INSERIR NOVA COLUNA</a>
<br /><br />
<?php
if(isset($_GET['acao']) && @$_GET['acao']=="editar") {
echo "<form action=\"".$pagina."\" method=\"post\"><table>
<tr>";
$query=mysql_query("SELECT a.COLUMN_NAME, a.COLUMN_COMMENT
FROM information_schema.COLUMNS a
WHERE a.TABLE_NAME = '".$tabela."'");
while($dados=mysql_fetch_array($query)) {
extract($dados);
echo "<td>$COLUMN_COMMENT</td>";
}
echo "</tr>";
$query=mysql_query("SELECT * FROM ".$tabela." WHERE id='{$_GET['id']}'");
while($dados=mysql_fetch_array($query)) {
echo "<tr>";
for($i = 0; $i < mysql_result($colunas , 0, "colunas"); $i++) {
echo "<td><input type=\"text\" name=\"linhas[{$array_colunas[$i]}]\"
value=\"{$dados[$i]}\"></td>";
}
echo "</tr>";
}
echo "</table><input type=\"submit\" name=\"salvar\" value=\"salvar\">
</form>";
}
?>
Gleydson, post the connection script.
– rbz
Buddy I took the test and is connecting normally!!! Because I use other files with the same database and just gave an echo and connected quiet...
code
<?php $server = "localhost"; $user = "root"; $password = "test"; $dbname = "Meu_bank"; //Create connection $Conn = mysqli_connect($server, $user, $password, $dbname); if(!$Conn){ die("Connection failure: " . mysqli_connect_error(); }Else{ echo "Successfully connected"; } ?>– Gleydson Master
But where you tested is with
mysqli
also ? Because of my question: themysql
does not ask for the name of the bank, already themysqli
yes. There could be your problem. Also the way debug are different, it may be the fact that no error message appears.– rbz
Thanks for the help!! I use other mysqli scripts that is running well in an admin I have here!! The question I’m thinking there might be some error in the code I’m not able to identify, I’m using xampp with php 7 locally!! has yes the msqli, I have checked here!!
– Gleydson Master
It’s expensive... So just a more analytical debug... See point to point of your script...
– rbz
I will post my old code take a look please and see if the change I made from FOR to WHILE is correct!!!
code
WHERE a.TABLE_NAME = '". $table." '"); while($data=mysql_fetch_array($query)) { Extract($data); echo "<td>$COLUMN_COMMENT</td>"; } echo "</tr>"; $query=mysql_query("SELECT * FROM ". $table." WHERE id='{$_GET['id']}'"); while($dados=mysql_fetch_array($query)) { echo "<tr>"; for($i = 0; $i < mysql_result($columns , 0, "columns"); $i++) { echo "<td><input type="text" name"lines[{$array_columns[$i]}]value="{$data[$i]}"></td>"; } }code
– Gleydson Master
Gleydson, edit the question, put it there.
– rbz
OK I’ll edit the question and put it there...
– Gleydson Master
I edited the file put the old code in Mysql
– Gleydson Master