首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在php中从表中获取id

如何在php中从表中获取id
EN

Stack Overflow用户
提问于 2018-07-10 02:42:01
回答 1查看 337关注 0票数 -1
代码语言:javascript
复制
table 'orders'(id,name,phone,price,adress,status)

table orders_foods (id, food_id, order_id, price, quantity)

我需要插入到orders_foods,我需要order_id自动生成时,我klik 坦巴pesanan (添加订单)

当我klik tambah pesan时显示home for insert for orders_food

modal view

每个列都有按钮tambah pesanan (添加顺序)如何从列中获取id以插入到我的orders_foods中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-10 07:15:29

您可以像这样获得ID (这是html或twig模板tableEntries.twig

代码语言:javascript
复制
<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脚本tableEntries.php

代码语言:javascript
复制
<?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);

如果你有任何问题,尽管问我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51252121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档