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 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题