duanji2014 2017-12-13 10:20
浏览 53
已采纳

在表单提交中向表格插入数据的最佳方法是什么?

I have two tables wallet (table 1) and api (table 2).

Right now I have made the part where I insert the submitted data to my wallet table. This is the code for this specifically:

<?php
   include 'db.php'; 

   if ($_SERVER['REQUEST_METHOD'] == 'POST') {

      $coin = mysqli_real_escape_string($conn, $_REQUEST['coin']);
      $user_coins = mysqli_real_escape_string($conn, $_REQUEST['coins_quantity']);

      $sql = "INSERT INTO wallet (coin, user_coins) VALUES ('$coin', '$user_coins')";
      if ($conn->query($sql)){
          echo "Added successfully.";
      } else{
          echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
      }

   }

?>

Where this is my form the code above:

<form method="post">
   <div class="form-group row justify-content-center">
      <label class="col-sm-3 col-form-label">Coin</label>
      <div class="col-sm-6">
         <select class="form-control" name="coin">
             <?php

             $sql = "SELECT name, symbol FROM api";
             $result = $conn->query($sql);

             while ($row = $result->fetch_assoc()) {
                 unset($symbol, $name);
                 $symbol = $row['symbol'];
                 $name = $row['name'];
                 echo '<option value="'.$symbol.'">'.$name.'</option>';
              }
             ?>
         </select>
      </div>
   </div>
   <div class="form-group row justify-content-center">
      <label class="col-sm-3 col-form-label">Coins</label>
      <div class="col-sm-6">
         <input type="number" class="form-control" name="coins_quantity" placeholder="5">
      </div>
   </div>
   <div class="form-group row justify-content-center text-center">
      <div class="col-sm-12">
         <button type="submit" class="btn btn-primary">Add</button>
      </div>
   </div>
</form>

In my table wallet (table 1) I have created columns which are coin, user_coins and coin_price.

While in table api (table 2) I have name, price, symbol and more.

I want to insert data to coin_price in wallet from the price column in the api table, and I want the price from the selected '<option value="'.$symbol.'">'.$name.'</option>';.

How can I do this?

  • 写回答

2条回答 默认 最新

  • dongzhuo1958 2017-12-13 10:41
    关注

    I've found a way. You can:

    1.) Put the coin id on the value of the select then,
    2.) On your post method, find (where clause) the coin id and get the necessary data you want to insert them to another table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助