2017-05-17 13 views
-3

Ich habe zwei Dropdown-Listen in meiner Tabelle und ich möchte diese Werte in die Datenbank einfügen. Aber wenn ich auf "submit" drücke, passiert nichts.PHP Einfügen mehrerer Dropdown-Werte in der Datenbank

Das ist, was ich jetzt haben:

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

Dies ist der Teil für den Einsatz, das nicht funktioniert:

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

Wie kann ich das Problem lösen?

+1

wo ist Ihr Formular? –

+0

Sie wissen, dass Sie Data_list überschreiben, richtig? – Strawberry

+0

Diese Zeile sollte nicht 'unset ($ id, $ name);' die '$ id'-Variable nicht zu diesem Zeitpunkt Sie" unset " – Akintunde007

Antwort

-1

Versuchen Sie diese Lösung. Sie haben das Formular-Tag vor dem Auswahlfeld gestartet. In Reihenfolge für den Zugriff müssen Sie Drop-Down innerhalb Form-Tag hinzufügen.

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

es funktioniert nicht –

Verwandte Themen