php - 以动态生成的形式使用数组中的 id

标签 php mysql forms

我正在尝试从数据库动态生成表单,以便我可以简单地登录并进行更改,以及跟踪投票。表单生成得很好,但是,我一生都无法弄清楚如何从数组中获取 id 并与数据一起发布。唯一成功的尝试将最后一个设置为其所有 topic_id,因为我尝试在 while 语句中定义它。

  <?php  $cat_set = fetch_categories();

  if (!isset($business_name)) {
  $business_name = ''; }

    while($categories = mysqli_fetch_assoc($cat_set)) {
                echo '<div class="category">'. "<h3>" . "Best of the Best"      
   .' ' . ucfirst($categories["cat_name"]) . ' ' . "</h3>";

                $topic_set = get_topics_for_cat($categories["cat_id"]);
                    while($topics = mysqli_fetch_assoc($topic_set)) {
                        $topic_name = $topics["topic_name"];
                    echo '<div class="field">' . '<label for=' . 
  $topics['topic_id'] . '">' . ucfirst($topic_name) . ":" . '</label>';
                        echo '<input type="text"' . 'id="' . 
  $topics['topic_id'] . '"' . 'name="'
                        . $topics["topic_id"] . '"' . 'value="' . 
  $business_name . '"></div>';
                    }
                    echo "</div>";
    }
    ?>

<input type="submit" value="Submit" id="submit" name="submitform"></form>
</div>
<pre>
<?php
$user_id = 3000;
if (isset($_POST['submitform'])) {
    print_r($_POST);

foreach ($_POST as $business_name) {
    $filtered_business_name = mysqli_real_escape_string($dbc,  
$business_name);
    $query = "INSERT INTO votes (";
    $query .=" business_name, topic_id, user_id";
    $query .= ") VALUES (";
    $query .=" '$filtered_business_name', '$topics['topic_id']',   
'$user_id'";
    $query .=")";

    $votes = mysqli_query($dbc,$query);

更具体地说,我想将 $topics['topic_id'] 纳入我的查询的最佳方式是什么?我试图逃避它以及我能想到的一切。

最佳答案

将变量括在大括号中:

$query .="'$filtered_business_name', '{$topics['topic_id']}', '$user_id'";

关于php - 以动态生成的形式使用数组中的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41943870/

相关文章:

mysql - 计算 2 个表的相等字段,并与第 3 个表进行内部连接

php - Zend 框架在哪里保存默认证书存储?

mysql - 用于 SSH 连接的 Spring/Hibernate MySQL 驱动程序设置

php - 尝试从 mysql 数据库检索用户个人资料图片。

mysql - 使用 MySQL 选择查询显示结果不需要的行

python - 如何获取 django 的密码文本框?

c# - 计时器不响

javascript - 无法在 heroku 上使用 Node.js 执行多个名称字段 POST

PHP:重构为面向对象——如何对连接建模

php - 触发由父 View 引起的任何事件时退出 Tour - Intro.js