如何从html表行获取ID?

时间:2018-11-06 08:49:17

标签: php html

我已经有一个只显示一个数据的表,单击一个按钮(编辑)后,我需要它来显示/显示模态数据库中的其他数据。 我的问题是(目前),我无法获取html表中每一行的ID。 (一旦我可以获取我认为可以提取其他数据的ID)

此处显示了显示该表的PHP代码:

<?php
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('ts_php');
$query = "SELECT * FROM job_posted";
$result = mysql_query($query);

echo "<table class='table'>
          <thead>
              <th>JOB</th>
              <th>STATUS</th>
              <th>APPLICATIONS</th>
              <th>EDIT</th>
              <th>DELETE</th>
          </thead>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
    echo "<tr>
              <th>" . $row['job_title'] . "</th>
              <td>" . $row['status'] . "</td>
              <td>" . $row['applications'] . "</td>
              <td><a data-toggle='modal' data-target='#myModal'>edit</a></td>
              <td><a href='#'>delete</a></td>                  
          </tr>";
}
echo "</table>";
mysql_close();
?>

4 个答案:

答案 0 :(得分:0)

我认为您是在谈论job_posted表的主键ID。在您的a标记中添加ID数据属性,然后单击它即可访问该ID。

<td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>

用您的ID密钥替换$row['id']

答案 1 :(得分:0)

例如,您的primary_key是id。您需要更改href对此模式是开放的

    <tr>
        <th>" . $row['job_title'] . "</th>
        <td>" . $row['status'] . "</td>
        <td>" . $row['applications'] . "</td>
        <td><a data-toggle='modal' data-id='".$row['id']."' data-target='#myModal'>edit</a></td>
        <td><a href='#'>delete</a></td>                  
    </tr>";

,我认为您也需要更改打开模式的方式。首先将类添加到您的href作为示例,我将使用.openeditmodal

所以您的href看起来像这样

<a class='openeditmodal' data-toggle='modal' data-id='".$row['id']."' data-target='#myModal'>edit</a>

然后添加此行以打开模式。

  $(".openeditmodal").click(function(){
    var job_posted_id = $(this).data('id');
    $("#myModal").modal("show");
});

答案 2 :(得分:0)

尝试此代码。

在循环内部

PKG="emacs"
dpkg-query -l $PKG > /dev/null || sudo apt install $PKG

在您的脚本中

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "<tr>
          <th>" . $row['job_title'] . "</th>
          <td>" . $row['status'] . "</td>
          <td>" . $row['applications'] . "</td>
          <td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>
          <td><a href='#'>delete</a></td>                  
      </tr>";
}

答案 3 :(得分:-1)

您可以在隐藏字段中获取ID。

'<input type="hidden" value =' . $row["id"] . ' >'

您需要创建主键,并在数据库的第一栏中自动递增,即您的$ row [“ id”]

<td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>

在下面检查您的代码。

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
    echo "<tr>
        <th>" . $row['job_title'] . "</th><td>" . $row['status'] . "</td><td>" . $row['applications'] . "</td>
                    <td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>
                    <td><a href='#'>delete</a></td>                  
                    </tr>";
    }