2017-05-17 29 views
-3

我有两个下拉列表在我的表内,我想插入这些值在数据库中。但是当我按提交没有发生。PHP在数据库中插入多个下拉值

这就是我现在所拥有的:

<?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

你知道你在覆盖data_list,对吧? – Strawberry

+0

这行不应该工作'unset($ id,$ name);'$ id'变量在那个时候不存在'你不想' – Akintunde007

回答

-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); 
?> 
+0

它不起作用 –

相关问题