代码之家  ›  专栏  ›  技术社区  ›  GAMEFORME riski NoeNoe

如何在php中从表中获取id

  •  -1
  • GAMEFORME riski NoeNoe  · 技术社区  · 7 年前
    table 'orders'(id,name,phone,price,adress,status)
    
    table orders_foods (id, food_id, order_id, price, quantity)
    

    我需要插入到Orders_Foods中,我需要在klik时自动生成Orders_ID 坦巴比萨南 (添加订单)

    home 当我将tambah pesan放入订单中时,显示modal

    modal view

    Evry Coloumn有按钮 坦巴比萨南 (添加订单)我如何从那个冒号中获取id以便插入到我的订单中?

    1 回复  |  直到 7 年前
        1
  •  0
  •   timunix    7 年前

    您可以得到这样的id(这是html或twig模板 表格条目.twig 以下内容:

    <html>
    <head>Get ID</head>
    <body>
      <table>
            <thead>
              <tr>
                  <td>column id</td>
                  <td>column name</td>
                  <td>column description</td>
                  <td>column note</td>
              </tr>
            </thead>
            {% for someAlias in dataExample %} <!-- dataExample is an array in the php file -->
            <tbody>
                <tr>
    <!-- *columnID*, *columnName*, etc. are 
    the real mysql columns, while **someAlias** 
    is just a random name for this for-loop. 
    **dataExample** is the array which contains 
    all the values of the corresponding columns -->
                    <td>{{ someAlias.columnID }}</td>
                    <td>{{ someAlias.columnName }}</td>
                    <td>{{ someAlias.columnDescription }}</td>
                    <td>{{ someAlias.columnNote }}</td>
                </tr>
            <td>
        <!-- The php script parses the mysql database 
    and saves the id and all other values in an array. 
    The php templating engine Twig uses this file as 
    its template (frontend). 
    If the Send-Button is pressed, the id will be 
    saved inside $_POST["SecretIdUsedForIfSendButtonIsPressed"]; 
    and can be used to add, edit or delete table entries (rows) 
    from there. -->
             <form method="POST" action="addTableEntries.php">
              <input type="hidden" name="SecretIdUsedForIfSendButtonIsPressed" value="{{ SecretIdParsedFromTheMySqlDatabase }}">
              <input type="submit" value="tambah pesanan">
            </form>
           </tbody>
            {% endfor %}
        </table>
    </body>
    </html>
    这是php脚本 表格条目.php 以下内容:
    <?php
    /* Lists all column entries inside your mysql table(s)
     * Author: timunix
     * Date: 10.07.2018
    */
    require_once 'vendor/autoload.php'; // you don't need to change the path, just leave it like that, twig will do the job for you
    require_once "utils/Database.class.php"; // include database configuration
    
    // init twig
    $loader = new Twig_Loader_Filesystem('template/'); /*no path adaptation needed, just leave it like that, twig will "know" where your twig/html templates are*/
    
    $twig = new Twig_Environment($loader, array(
        "debug" => "true",
    ));
    include "utils/injector.php";
    
    $twig->addExtension(new Twig_Extension_Debug());
    
    /* ------- */
    
    DatabaseLink::getInstance()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // fetch data from database *(simple way, not entirely safe, though!)*
    $stmt = DatabaseLink::getInstance()->query("SELECT * FROM tbl_columns WHERE columnID > 0");
    $columnData = $stmt->fetchAll();
    
    //template values
    $templateName = "tableEntries.twig";
    $data = array(
        "dataExample" => $columnData,
    );
    
    //display
    echo $twig->render($templateName, $data);
    

    如果你有什么问题,尽管问我。