有人知道如何用我的代码创建一个插入表吗? 我的代码有一个显示数据库中表的提示,当用户选择要插入的表时,我想显示要插入的字段名和文本字段。但是我不知道该怎么做,有人可以帮我吗? 谢谢
要插入表的代码:
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);
}
//数据插入
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!!");
}
}
?>