PHP MySQL不从POST下拉列表中获取值

时间:2019-06-18 21:48:18

标签: php mysql xampp php-7

我正在尝试做一个应用程序,想要从数据库中获取一些数据并显示在一个下拉列表中,但是我想从该下拉列表中获取ID并显示我的名字。我试图做某事,但不想去,或者它使我插入0 ..

表格

 <form class="formValidate" id="formValidate1" method="post" action="routers/add-item.php" novalidate="novalidate" enctype="multipart/form-data">
            <div class="row">
              <div class="col s12 m4 l3">
                <h4 class="header">Adaugă produs nou</h4>
              </div>
              <div>
<table>
                    <thead>
                      <tr>
                        <th data-field="name">Imagine</th>
                        <th data-field="id">Nume produs</th>
                        <th data-field="name">Preţ/Bucată produs</th>
                        <th data-field="name">Detalii produs</th>
                        <th data-field="name">Categorie</th>

                      </tr>
                    </thead>

                    <tbody>
                <?php


          echo '<tr><td><input id="image" name="image" type="file" ></td>';

                    echo '<td><div class="input-field col s12"><label for="name">Nume produs</label>';
                    echo '<input id="name" name="name" type="text" data-error=".errorTxt01"><div class="errorTxt01"></div></td>';                   
                    echo '<td><div class="input-field col s12 "><label for="price" class="">Preţ</label>';
                    echo '<input id="price" name="price" type="text" data-error=".errorTxt02"><div class="errorTxt02"></div></td>';                   
          echo '<td><div class="input-field col s12 "><label for="price" class="">Detalii produs</label>';
                    echo '<input id="details" name="details" type="text" data-error=".errorTxt01"><div class="errorTxt01"></div></td>';                 

          echo '<td>'; 
          $query = "SELECT id,denumire FROM category";
          $result = mysqli_query($con,$query);
          ?>

          <select name="codes" value="<?php if (isset($_POST['id'])) echo $_POST['id']; ?>" >
          <?php
          while ($line = mysqli_fetch_array($result)) {
          ?>
          <option> <?php echo $line[1];?> </option>
          <?php
          }

          echo ' </select></td></tr>';
            ?>


                         </tbody>
</table>
              </div>
              <div class="input-field col s12">
                              <button class="btn cyan waves-effect waves-light right" type="submit" name="action" id="valid">Adaugă
                                <i class="mdi-content-send right"></i>
                              </button>
                            </div>
            </div>
            </form>     

这是插入内容。

<?php
include '../includes/connect.php';

$name = $_POST['name'];
$price = $_POST['price'];
$details = $_POST['details'];
$cod = $_POST['codes'];

$file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));

$sql = "INSERT INTO items (name, price,details,image,id_category) VALUES ('$name', $price,'$details','$file','$cod')";
$con->query($sql);
header("location: ../admin-page.php");
?>

从该代码无法解决的问题(我给出了错误),我尝试在控制台上查看错误,但徒劳无功。 一旦插入0,它将插入所有数据,但类别ID为0

0 个答案:

没有答案