我试图从数据库动态生成表单,以便我可以简单地登录和进行更改,以及跟踪投票。表单生成正常,但我不能为我的生活弄清楚如何从数组中获取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);
更具体地说,我想,在我的查询中获取$ topic [&#39; topic_id&#39;]的最佳方法是什么?我试图逃避它以及我能想到的一切。
答案 0 :(得分:0)
用大括号括起变量:
$query .=" '$filtered_business_name', '{$topics['topic_id']}', '$user_id'";
答案 1 :(得分:0)
请执行以下操作:
$topic = $topics['topic_id'];
$query .=" '$filtered_business_name', '$topic', '$user_id'";
答案 2 :(得分:0)
我一定是误用了它。我做了很多编辑和事情,我自己的东西没有意义,所以我扭转了一堆并尝试了key =&gt;值的东西......它就可以了。现在查询是:
if (isset($_POST['submitform'])) {
foreach($_POST as $key=>$value) {
$filtered_business_name = mysqli_real_escape_string($dbc, $value);
$query = "INSERT INTO votes (";
$query .=" business_name, topic_id, user_id";
$query .= ") VALUES (";
$query .=" '$filtered_business_name', '$key', '$user_id'";
$query .=")";
$votes = mysqli_query($dbc,$query);
confirm_query($votes);
}
}
感谢大家的帮助,希望这将有助于将来制作糟糕表格的人。