2017-05-17 30 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

它不起作用 –

相關問題