无法从表格内的按钮获取行ID

时间:2016-02-10 10:45:45

标签: javascript php html mysql

嘿,我有以下代码:

SessionNotCreatedException

现在,如果我想编辑或删除任何数据并触发查询,那么我该怎么做才能执行该任务。 我尝试使用onslick事件和调用函数使用Js,但我不知道如何获取id。

<tbody>
  <?php 
  $connection = mysql_connect("localhost", "root", "") or die("Could Not Connect to DB: ".mysql_error());
  $db = mysql_select_db("DBNAME", $connection) or die("Could Not Connect to DB: ".mysql_error());
  $result = mysql_query("SELECT * FROM product");
  while($row = mysql_fetch_row($result))
  {                          
   echo "<tr class='odd gradeX'>
      <td>{$row[0]}</td>
      <td>{$row[1]}</td>
      <td>{$row[2]}</td>
      <td>{$row[3]} x {$row[4]} x {$row[5]}</td>
      <td>{$row[6]}</td>
      <td>{$row[7]}</td>
      <td>{$row[8]} Rs.</td>
      <td><a href='' onclick=''>Edit</a></td>
      <td><a href='' onclick=''>Delete</a></td>
  </tr>";
  }
  ?>
</tbody>

3 个答案:

答案 0 :(得分:1)

您可以像这样更改您的代码:

<tbody>
        <?php 
            $connection = mysql_connect("localhost", "root", "") or die("Could Not Connect to DB: ".mysql_error());
            $db = mysql_select_db("DBNAME", $connection) or die("Could Not Connect to DB: ".mysql_error());
            $result = mysql_query("SELECT * FROM product");
            while($row = mysql_fetch_row($result))
            {                          
                echo "<tr class='odd gradeX'>
                <td>{$row[0]}</td>
                <td>{$row[1]}</td>
                <td>{$row[2]}</td>
                <td>{$row[3]} x {$row[4]} x {$row[5]}</td>
                <td>{$row[6]}</td>
                <td>{$row[7]}</td>
                <td>{$row[8]} Rs.</td>
                <td><a href='edit.php?id={$row[0]}'>Edit</a></td>
                <td><a href='delete.php?id={$row[0]}'>Delete</a></td>
                </tr>";
            }
        ?>
    </tbody>

或通过javascript as:

<tbody>
    <?php 
        $connection = mysql_connect("localhost", "root", "") or die("Could Not Connect to DB: ".mysql_error());
        $db = mysql_select_db("DBNAME", $connection) or die("Could Not Connect to DB: ".mysql_error());
        $result = mysql_query("SELECT * FROM product");
        while($row = mysql_fetch_row($result))
        {                          
            echo "<tr class='odd gradeX'>
            <td>{$row[0]}</td>
            <td>{$row[1]}</td>
            <td>{$row[2]}</td>
            <td>{$row[3]} x {$row[4]} x {$row[5]}</td>
            <td>{$row[6]}</td>
            <td>{$row[7]}</td>
            <td>{$row[8]} Rs.</td>
            <td><a href='#' OnClick='FunctionEdit({$row[0]})'>Edit</a></td>
            <td><a href='#' OnClick='FunctionDelete({$row[0]})' >Delete</a></td>
            </tr>";
        }
    ?>
</tbody>

答案 1 :(得分:0)

<tbody>
  <?php 
  $connection = mysql_connect("localhost", "root", "") or die("Could Not Connect to DB: ".mysql_error());
  $db = mysql_select_db("DBNAME", $connection) or die("Could Not Connect to DB: ".mysql_error());
  $result = mysql_query("SELECT * FROM product");
  while($row = mysql_fetch_row($result))
  {?>                       
     <tr class='odd gradeX'>
        <td><?=$row[0]?></td>
        <td><?=$row[1]?></td>
        <td><?=$row[2]?></td>
        <td><?php $t = $row[3] x $row[4] x $row[5];?><?=$t?></td>
        <td><?=$row[6]?></td>
        <td><?=$row[7]?></td>
        <td><?=$row[8]?> Rs.</td>
        <td><a href='' onclick='functionEdit'>Edit</a></td>
        <td><a href='' onclick='functionDelete'>Delete</a></td>
    </tr>
        <?php }?>
</tbody>

答案 2 :(得分:0)

将此添加到您的代码

        <td><a href='edit.php?id={$row[0]}'>Edit</a></td>
        <td><a href='delete.php?id={$row[0]}'>Delete</a></td>