如果单击链接,则执行PHP

时间:2015-05-15 03:42:55

标签: php html database

我一直在互联网上搜索,当然还有Stackoverflow,以获取有关如何在点击链接时执行PHP命令的答案。以下是我的一些基本代码,它们试图“更新”。或者'删除'表格中的数据。

if(isset($_GET['Delete'])){

                $sql = "DELETE FROM addresses WHERE id ='$_POST[id]'";
                mysql_query($sql,$conn);
                header("Location: form.php");
            };

    if (isset($_GET['update'])){
                $sql = "UPDATE addresses SET firstname='$_POST[firstname]', lastname='$_POST[lastname]', age='$_POST[age]' WHERE id='$_POST[id]'";
                mysql_query($sql,$conn);
                header("Location: form.php");
            };




?>
<?php
while ($row = mysql_fetch_array($retreve, MYSQL_ASSOC)) {

    echo "<form action=form.php method=post>";
    echo "<tr>";
    echo "<td><input type=text name=firstname value={$row['firstname']}> </td>";
    echo "<td><input type=text name=lastname value={$row['lastname']}> </td>";
    echo "<td><input type=text name=age value={$row['age']}> </td>";
    echo "<td><input type=hidden name=id value={$row['id']}> </td>";
    //links insead of buttons
    echo "<td><a href = # id='update'> Update</a></td>";
    echo "<td><a href = # id='delete'> Delete</a> </td>";

} 

我点击了链接&#34;更新&#34;和&#34;删除&#34;。我想做什么才能让PHP执行。

注意:数据库连接未显示但已连接。

2 个答案:

答案 0 :(得分:0)

echo "<td><a href = 'form.php?type=delete&id=99' id='delete'> Delete</a> </td>";
在您的特定情况下,

{$row['id']}为99 然后:

if($_GET['type']=='delete'){

                    $sql = "DELETE FROM addresses WHERE id ='$_GET[id]'";
                    mysql_query($sql,$conn);
                    header("Location: form.php");
}elseif ($_GET['type']=='update'){

//

}

答案 1 :(得分:-1)

如果您使用的是相同的表单,请检查您的表单方法。您的表单方法是发布您的条件包含$_GET['Delete']将其更改为$_POST['Delete']