【问题标题】:how to get 'id' from href inside database如何从数据库中的href获取'id'
【发布时间】:2017-12-24 04:32:56
【问题描述】:

Here is the table.

我有一个表名问题。在该表中,我有一个列名“dateclose”。每次我添加数据时,它都会在该列中自动创建一个数据。代码如下:

 $sam = '<a href="sams.php" id='.$_POST['incidentnum'].'>Close Issue</a>';

'incidentnum' 就像唯一键。如果我添加了新数据,这是数据库中的输出:

incidentnum | issue | datecreate | dateclose
------------|-------|------------|----------
210         |sample | 02-12-2017 |<a href="sams.php" id=210>Close Issue</a>

现在,我想回显“dateclose”以获取特定事件编号的 id 编号并将数据更改为

 date("Y-m-d")

【问题讨论】:

  • 不清楚你想要什么。
  • 在数据库中插入html代码不是一个好习惯
  • 我只想点击“关闭问题”链接,单元格将变为当前日期,这就是我使用 html 代码的原因

标签: php database xampp


【解决方案1】:

第一:在数据库中插入html代码不是一个好习惯

第二个:只需像下面这样传递查询参数。

<a href="sams.php?id=<?php echo $_POST['incidentnum'] ?>">Close Issue</a>

第 3 次: 如下所示访问它。

$incidentnum= $_GET['id'];

第四次:更新查询

 "UPDATE issue SET dateclose =CURDATE()  WHERE incidentnum='$incidentnum'";

5th:改变html表格渲染部分。在行创建时将dateclose 列设置为null

$html_table = '<table align="center" border="1" cellspacing="0" cellpadding="5" width="800" word-wrap="break-word" display="inline-block"> 
              <tr><th>Incident #</th><th>Issue</th>th>Date Closed</th></tr>'; 
foreach($result as $row){ 

    $html_table .= '<tr>
                        <td>' .$row["incidentnum"]. '</a></td>
                        <td>' .$row["issue"]. '</td> 
                        <td>';
   if(is_null($row["dateclose"])){  

      $html_table.="<a href='sams.php?id=<?php echo $row[\"incidentnum \"] ?>'>Close Issue</a>";

    }else{

       $html_table.=$row["dateclose"]; 
  }
                  $html_table. '</td>

                    </tr>';

     }

5th :尽量使用Prepared statement or pdo来避免sql injection

【讨论】:

  • 我只想点击“关闭问题”链接,单元格将变为当前日期,这就是我使用 html 代码的原因
  • 桌子? @JYoThI
  • 是的,你如何渲染html表格那部分代码@user8230483
  • $html_table = 'th>关闭日期'; foreach($result as $row){ $html_table .= '';
  • 看看我更新的答案。尤其是最后 td 的第 5 点 @user8230483
  • 事件# 问题
    ' .$row["incidentnum"]. ' ' .$row["issue"]. ' ' 。 $行[“日期关闭”]。 '
【解决方案2】:

从数据库中检索行后,您可以使用正则表达式获取 id 的内容。

例如:

$sam = '<a href="sams.php" id=213>Close Issue</a>';

preg_match('#(?<=id=).*?(?=>)#', $sam, $match);

var_dump($match);

输出:

array(1) { 
   [0] => string(3) "213" 
}

【讨论】:

    【解决方案3】:

    其实你的问题不是很清楚。 但我认为您的要求是每当用户点击&lt;a href="sams.php" id='.$_POST['incidentnum'].'&gt;Close Issue&lt;/a&gt;时保存关闭日期

    所以只要你想在数据库显示上插入 CURDATE() 就可以了。

    【讨论】:

    • 是的,这就是我真正想做的,这就是我使用 html 代码的原因
    • 所以您已经通过以下方式更新了您的代码:关闭问题然后在 sams.php 中编写更新查询以保存关闭日期。很简单。
    • 这是我在 sams.php 中的查询 $sql = "UPDATE issue SET dateclose ='$dateclose' WHERE eventnum='$incidentnum'";
    • sams.php 中的正确查询如下:$sql = "UPDATE issue SET dateclose =CURDATE() WHERE eventnum='$incidentnum'";
    【解决方案4】:

    为了获得id,您必须将其作为查询参数传递。

    你可以这样做:

    $sam = '<a href="sams.php?id='.$_POST['incidentnum'].'">Close Issue</a>';
    

    然后在服务器端,可以使用:

    $id = $_GET['id'];
    

    希望这会有所帮助。并且一定要处理不传id或者传错id的情况。

    【讨论】:

    • 我想要做的就是如果我点击“关闭问题”链接,单元格将变为当前日期,这就是我使用 html 代码的原因
    • @user8230483 我们从未说过不要使用 HTML。您必须使用它在浏览器中呈现链接,但将其存储在 DB 中并不是一个好主意。除此之外,您现在可以使用 id 并可以更新截止日期。
    • 这是我知道的唯一关闭日期的方法
    猜你喜欢
    • 2018-05-12
    • 2019-12-03
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多