does anyone know how to create a insert table with my code? My code has a cicle to show the tables in the DB, and when the user chose the table to insert, I wanted to show the fieldname and the text field to insert. But I don't know how to do it, can someone help me with that? Thanks
Code to Chose table to insert :
if(!isset($_POST['submeterTabela']) && !isset($_POST['submeterDados'])) {
$form ="<form action=\"$self\" method=\"post\">";
$result = $dbo->query("SHOW TABLES");
$form.= "<select name='Tabela' class='select_box'>";
while ($row = $result->fetch(PDO::FETCH_NUM)) {
$form.= "<option value='$row[0]'>$row[0]</option>";
}
$form.= "</select>";
$form.="<br><input type=\"submit\" name=\"submeterTabela\" value=\"Submeter\"class='button'>";
echo($form);
}
// Data Insertion
if(isset($_POST['submeterTabela']) && !isset($_POST['submeterDados']))
{
$Tabela=$_POST['Tabela'];
$form ="<form action=\"$self\" method=\"post\">";
$form.="<input type=\"hidden\" name=\"Tabela\"class='tabela1'value=\"$Tabela\"> <br>";
$form.="Introduz Dados<br>";
$sql ="SHOW columns from ".$Tabela;
$result = $dbo->query($sql);
while ($row = $result->fetch(PDO::FETCH_NUM)) {
$sql1="select Campo from descritivoscampos where Tabela='".$Tabela."' and Campo='".$row[0]."';";
$Resultado = $dbo->query($sql1);
$linha = $Resultado->fetch(PDO::FETCH_NUM);
$form .= "$linha[0]";
$form.="<input type=\"text\" name=\"$row[0]\" size=\"5\"><br>";
}
$form.=" <br><input type=\"submit\" name=\"submeterDados\"class='botao'value=\"Submeter\">";
echo($form);
}
if(isset($_POST['submeterDados']))
{
$Tabela=$_POST['Tabela'];
$sql ="SHOW columns from ".$Tabela;
$result = $dbo->query($sql);
$sql1 = "insert into $Tabela(";
$aux=0;
while ($row = $result->fetch(PDO::FETCH_NUM)) {
if ($aux!=0) $sql1 .=", ";
if ($row[0]!='id')
{
$sql1 .="$row[0]";
$aux=1;
}
}
$sql1 .=") values (";
$result = $dbo->query($sql);
$aux=0;
while ($row = $result->fetch(PDO::FETCH_NUM)) {
if ($aux!=0) $sql1 .= ", ";
if ($row[0]!='id')
{
$aa=$_POST[$row[0]];
$sql1 .="'$aa'";
$aux=1;
}
}
$sql1 .=");";
$conn = mysqli_connect($host_name, $username, $password, $database)
or die("Could not connect.");
$result = mysqli_query($conn, $sql1)
or die("Could not execute SQL query");
if ($result) {
echo("Informação introduzida com Sucesso!!");
}
}
?>