PHP在数据库中插入多个下拉列表值

时间:2017-05-17 07:11:33

标签: 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['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['orden_name']) {
                $data_list = $row['orden_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>" . $row['cluster_name'] . "</td>";
            echo "<td>" . $data_list . "</td>";
            echo "</tr>";

        }
        echo "</table>";

        echo ' <form method="POST"><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);
?>

这是插件不起作用的部分:

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);
}
}

我该如何解决?

1 个答案:

答案 0 :(得分:-1)

  

试试这个解决方案。您已在选择框之前启动了表单标记。在   要访问您,您需要在表单标记中添加下拉列表。

<?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 '<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['orden_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>" . $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);
?>