时间:2017-05-16 13:32:14

标签: php mysql

我有一个表,其中从数据库中选择了所有值,但有些单元格为空。如何将下拉列表放在该空单元格内。下拉列表中的值必须来自数据库

这是我的代码:

    <?php
include("css/style.php");

/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "Iamthebest1009", "dktp");

// Check connection
if ($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$dropdown_list = '';
$sql = "SELECT * FROM orden";
$result_list = mysqli_query($link, $sql);
 if (mysqli_num_rows($result_list) > 0) {
     $dropdown_list = '<select>';
     while ($row = mysqli_fetch_array($result_list)) {
         unset($id, $name);
         $id = $row['id'];
         $name = $row['id'];
         $dropdown_list .= '<option value="' . $id . '">' . $name . '</option>';

     }
      $dropdown_list .= '</select>';
 }

// Attempt select query execution
$sql = "SELECT * FROM Norm LEFT JOIN Cluster ON norm.cluster_id = cluster.id LEFT JOIN Orden ON norm.orden_id = orden.id ORDER BY norm_name";
if ($result = mysqli_query($link, $sql)) {
    if (mysqli_num_rows($result) > 0) {
        echo '<form method="POST">';
        echo "<table>";
        echo "<tr>";
        echo "<th>Norm id</th>";
        echo "<th>Norm</th>";
        echo "<th>Omschrijving</th>";
        echo "<th>Clusteren</th>";
        echo "<th>Ordenen</th>";
        echo "</tr>";

        while ($row = mysqli_fetch_array($result)) {
            if ($row['orden_name']) {
                $data_list = $row['id'];
            } else {
              $data_list = $dropdown_list;
            }
            echo "<tr>";
            echo "<td>" . $row['norm_id'] . "</td>";
            echo "<td>" . $row['norm_name'] . "</td>";
            echo "<td>" . $row['description'] . "</td>";
            echo "<td>" . $row['cluster_name'] . "</td>";
            echo "<td>" . $data_list . "</td>";
            echo "</tr>";

        }
        echo "</table>";

        echo '<input type="submit" </input><form>';
        // Free result set
        mysqli_free_result($result);
    } else {
        echo "No records matching your query were found.";
    }

} else {
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

if(isset($_POST['submit']))
{
  $sql = "INSERT INTO norm (orden_id) VALUES ('$data_list')";
  if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
// Close connection
mysqli_close($link);
?>

1 个答案:

答案 0 :(得分:0)

您可以查看此代码。当$ row [&#39; cluster_name&#39;]为空然后生成下拉列表并首先创建下拉列表然后检查您的数据是否退出但未经过测试

      <?php
include("css/style.php");

/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "Iamthebest1009", "dktp");

// Check connection
if ($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$dropdown_list = '';
$sql = "SELECT * FROM orden";
$result_list = mysqli_query($link, $sql);
 if (mysqli_num_rows($result_list) > 0) {
     $dropdown_list = '<select>';
     while ($row = mysqli_fetch_array($result_list)) {
         unset($id, $name);
         $id = $row['id'];
         $name = $row['orden_name'];
         $dropdown_list .= '<option value="' . $id . '">' . $name . '</option>';

     }
      $dropdown_list .= '</select>';
 }

// Attempt select query execution
$sql = "SELECT * FROM Norm LEFT JOIN Cluster ON norm.cluster_id = cluster.id LEFT JOIN Orden ON norm.orden_id = orden.id ORDER BY norm_name";
if ($result = mysqli_query($link, $sql)) {
    if (mysqli_num_rows($result) > 0) {

        echo "<table>";
        echo "<tr>";
        echo "<th>Norm id</th>";
        echo "<th>Norm</th>";
        echo "<th>Omschrijving</th>";
        echo "<th>Clusteren</th>";
        echo "<th>Ordenen</th>";
        echo "</tr>";

        while ($row = mysqli_fetch_array($result)) {
            if ($row['cluster_name']) {
                $data_list = $row['cluster_name'];
            } else {
              $data_list = $dropdown_list;
            }
            echo "<tr>";
            echo "<td>" . $row['norm_id'] . "</td>";
            echo "<td>" . $row['norm_name'] . "</td>";
            echo "<td>" . $row['description'] . "</td>";
            echo "<td>" . $data_list . "</td>";
            echo "<td>" . $row['orden_name'] . "</td>";
            echo "</tr>";

        }
        echo "</table>";
        // Free result set
        mysqli_free_result($result);
    } else {
        echo "No records matching your query were found.";
    }

} else {
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>