将 var 名称解析为字符串文字以将字符串的一部分分配给新的 var

parsing var name as string literal to assign part of string to new var

我正在将数据库 table id 编号附加到动态呈现的按钮,如下所示:

echo '</div><!-- close formRowDiv -->';
echo '<div class="buttonDiv">';
echo '<input type="submit" id="buttonUpdate_' . $row[ 'id' ] . '" name="buttonUpdate_' . $row[ 'id' ] . '" class="button" value="Update Trouble Log Entry">';
echo '</div><!-- closes updateDiv -->';

因此,当我单击时,我最终会得到 $_POST[ 'buttonUpdate_1' ],例如作为引用数据库 table 中的 ID #1。现在我想从 $_POST 变量名中提取 1

我假设我需要使用 preg_replace 但想不出如何在模式中逐字使用 var 名称来提取附加的 id #。我一直在谷歌搜索,似乎也找不到我需要的东西。那么如何在 preg_replace 中将 var 名称解析为文字字符串以提取其中的一部分并将其分配给新的 var?

以数组样式命名输入字段。我不知道它的正确名称。无论如何,它看起来像这样:

echo '<input type="text" name="myinput[' . $row[ 'id' ] . ']">';

如果您 post 数据,您将获得一个包含输入字段数据的数组。所以如果你写

foreach ($_POST['myinput'] as $id => $input) {
    echo $id;
    echo $input;
}

它将return逐行输入字段的数据。

虽然不是你问的预替换解决方案,但如果我没看错你的问题,你希望能够对 table 中带有附加 ID 号的行做一些事情。

如果是这样,您可以先查询 table 以获得最高值 id,并将其用作 for 循环中的计数器。然后使用 for 循环,您可以循环遍历 table 中的所有行,直到 $_POST[ 'buttonDisplay_1' ] 变量中的附加 ID 为 found/matched.

一旦匹配,你就可以抓取返回查询行的ID号,然后对抓取到的ID号所在的行做任何你想做的事情。也许是这样的:

$sql_query_to_find_high_id = "SELECT * FROM table ORDER BY id DESC LIMIT 1";
  $result=mysqli_query(dbcon(), $sqlQHighId);
  while ($row=mysqli_fetch_array($result)){high_id_number=$row['id'];}

在table中查找最高分配的id。然后循环使用那个高 ID 号作为你的计数器,你可以:

for($i=0; $i<$high_id_number; $i++){ // cycle thru all rows in table to find a match for ID appended to $_POST[ 'buttonDisplay_1' ]
    if(isset($_POST["buttonUpdate_$i"])){
      $id=$i;
      $sql_update_statement="UPDATE table SET column=value WHERE id='$id'";
}