2016-04-29 38 views
2

我正在编写一个应用程序,用户可以在其中输入数据库名称,我应该使用PHP将所有内容写入表中。我可以在知道数据库名称下面的代码。在PHP中编写数据库的属性

$result = mysqli_query($con,"SELECT * FROM course"); 

echo "<table border='1'> 
<tr> 
<th>blablabla</th> 
<th>blabla</th> 
<th>blablabla</th> 
<th>bla</th> 
</tr>"; 

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['blablabla'] . "</td>"; 
    echo "<td>" . $row['blabla'] . "</td>"; 
    echo "<td>" . $row['blablabla'] . "</td>"; 
    echo "<td>" . $row['bla'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 

在这个例子中,我可以告诉它,因为我知道表的名称当然,它有4 attributes.But我希望能够显示结果,无论这个名字的用户entered.So如果用户想要查看教师的内容应该有两列,而不是4.我可以如何完成这一点。我用html获取表名。

Table:<input type="text" name="table"> 

编辑:丹尼斯的回答和GrumpyCroutons的回答都是correct.You也可以问我,如果你不明白在他们的解决方案的东西。

+0

难道你的意思是像phpMyAdmin或Adminer? –

+0

有点儿。有一个页面是用html创建的,它可以从客户端获得输入。我有一个mysql数据库,里面有所有的表格,我试图用PHP – Curriculaa

+1

来显示它,如果你不想要在这里使用适当的措施来保护你的数据库。关于这个只是一个快速的旁注。 –

回答

2

快速写下来,评论它(这样你可以很容易地知道发生了什么,你看到),并为你测试它。

<form method="GET"> 
     <input type="text" name="table"> 
    </form> 

    <?php 


    //can be done elsewhere, I used this for testing. vv 
    $config = array(
     'SQL-Host' => '', 
     'SQL-User' => '', 
     'SQL-Pass' => '', 
     'SQL-Database' => '' 
    ); 
    $con = mysqli_connect($config['SQL-Host'], $config['SQL-User'], $config['SQL-Pass'], $config['SQL-Database']) or die("Error " . mysqli_error($con)); 
    //can be done elsewhere, I used this for testing. ^^ 


    if(!isSet($_GET['table'])) { //check if table choser form was submitted. 
     //In my case, do nothing, but you could display a message saying something like no db chosen etc. 
    } else { 

    $table = mysqli_real_escape_string($con, $_GET['table']); //escape it because it's an input, helps prevent sqlinjection. 

    $sql  = "SELECT * FROM " . $table; // SELECT * returns a list of ALL column data 
    $sql2 = "SHOW COLUMNS FROM " . $table; // SHOW COLUMNS FROM returns a list of columns 

    $result = mysqli_query($con, $sql); 
    $Headers = mysqli_query($con, $sql2); 

    //you could do more checks here to see if anything was returned, and display an error if not or whatever. 

    echo "<table border='1'>"; 
    echo "<tr>"; //all in one row 

    $headersList = array(); //create an empty array 

    while($row = mysqli_fetch_array($Headers)) { //loop through table columns 
     echo "<td>" . $row['Field'] . "</td>"; // list columns in TD's or TH's. 
     array_push($headersList, $row['Field']); //Fill array with fields 
    } //$row = mysqli_fetch_array($Headers) 

    echo "</tr>"; 

    $amt = count($headersList); // How many headers are there? 

    while($row = mysqli_fetch_array($result)) { 
     echo "<tr>"; //each row gets its own tr 
     for($x = 1; $x <= $amt; $x++) { //nested for loop, based on the $amt variable above, so you don't leave any columns out - should have been <= and not <, my bad 
      echo "<td>" . $row[$headersList[$x]] . "</td>"; //Fill td's or th's with column data 
     } //$x = 1; $x < $amt; $x++ 
      echo "</tr>"; 
    } //$row = mysqli_fetch_array($result) 

    echo "</table>"; 
    } 
    ?> 
+0

我测试了你的答案。它正确写入所有数据,但它显示最后一个属性的值为空。 – Curriculaa

+0

噢,我的坏我知道为什么,我开始在1阵列。请再试一次请 – GrumpyCrouton

+0

我想出来,以及你不需要编辑它。你的答案也是正确的,给了我一个窗口,看到不同的东西。感谢帮助。我不知道我是否可以同时接受你和丹尼斯的回答。 – Curriculaa

1
$tablename = $_POST['table']; 
$result = mysqli_query($con,"SELECT * FROM $tablename"); 

$first = true; 

while($row = mysqli_fetch_assoc($result)) 
{ 
    if ($first) 
    { 
    $columns = array_keys($row); 
    echo "<table border='1'> 
     <tr>"; 
    foreach ($columns as $c) 
    { 
     echo "<th>$c</th>"; 
    } 

    echo "</tr>"; 
    $first = false; 
    } 
    echo "<tr>"; 
    foreach ($row as $v) 
    { 
    echo "<td>$v</td>"; 
    } 
    echo "</tr>"; 
} 
echo "</table>"; 
+0

我检查了代码。这是我想要的,但它每次写入两次属性。 – Curriculaa

+0

当然,只是想到从第一行开始,您应该使用行键来获取表格列。如果从while循环中删除它,只需取第一行,获取表列并不要忘记写第一行值。 –

+0

请再试一次,我编辑我的答案mysqli_fetch_assoc –

-1
<?php 
    $table_name = do_not_inject($_REQUEST['table_name']); 
    $result = mysqli_query($con,'SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME='. $table_name); 
?> 
<table> 
    <?php 
    $columns = array(); 
    while ($row = mysql_fetch_assoc($result)){ 
     $columns[]=$row['COLUMN_NAME']; 
     ?> 
     <tr><th><?php echo $row['COLUMN_NAME']; ?></th></tr> 
     <?php 
    } 

    $result = mysqli_query($con,'SELECT * FROM course'. $table_name); 
    while($row = mysqli_fetch_assoc($result)){ 
     echo '<tr>'; 
     foreach ($columns as $column){ 
     ?> 
      <td><?php echo $row[$column]; ?></td> 
     <?php 
     } 
     echo '</tr>'; 
    } 
    ?> 
</table> 
+0

请解释! –

+0

您的代码混乱,缺少文档,无法正常工作。 – GrumpyCrouton