在按钮单击时执行sql查询

时间:2013-11-26 07:33:17

标签: php mysql

我设计了一个表单,表格的值以表格格式显示。在表格中我有两列分别是按钮编辑和删除。我想对那些按钮执行更新和删除查询,其中id是我的第一列表。 这是代码

<?php
mysql_connect("localhost","root","")or die("Unable to create to connection");
mysql_select_db("sunil") or die(mysql_error());
if(isset($_REQUEST['submit']))
{
    $Id= $_POST["id"];
    $Name1=$_POST["t1"];
    $Name2=$_POST["t2"];
    $Sex=$_POST["sex"];
    $Country=$_POST["Country"];
    $Hobby=$_POST["check"];
    $Email=$_POST["t3"];
    $Pass=$_POST["p1"];
    $Repass=$_POST["p2"];
    $sql="INSERT INTO registration (Your_id,First_Name,Last_Name,Sex,Country,Hobbies,Email,Password,Repassword) VALUES('$Id','$Name1','$Name2','$Sex','$Country','$Hobby','$Email','$Pass','$Repass')";
    mysql_query($sql);
}
$result = mysql_query("SELECT * FROM registration");
print "<table border=1>";
while($row = mysql_fetch_array($result))
{
        print "<tr>";
        print "<td>".$row['Your_id']."</td>";
        print "<td>".$row['First_Name']."</td>";
        print "<td>".$row['Last_Name']."</td>";
        print "<td>".$row['Sex']."</td>";
        print "<td>".$row['Country']."</td>";
        print "<td>".$row['Hobbies']."</td>";
        print "<td>".$row['Email']."</td>";
        echo '<td><input type="submit" value="Edit" onclick="return update()"></td>'; 
// tell me code of update()
        echo '<td><input type="submit" value="Delete" onclick="DELETE FROM registration WHERE Your_id=id"></td>'; 
// tell me what to use in place of is in this line
        print "</tr>";
}
print "</table>";
?>

And here is snap shot
[1]: http://sunilpachlangia.blogspot.in/2013/11/blog-post.html

1 个答案:

答案 0 :(得分:0)

    print "<tr>";
    print "<td>".$row['Your_id']."</td>";
    print "<td>".$row['First_Name']."</td>";
    print "<td>".$row['Last_Name']."</td>";
    print "<td>".$row['Sex']."</td>";
    print "<td>".$row['Country']."</td>";
    print "<td>".$row['Hobbies']."</td>";
    print "<td>".$row['Email']."</td>";
    echo '<td><a href="req=update&id=UNIQUE_ID">edit</a>'; 
    echo '<td><a href="req=delete&id=UNIQUE_ID">delete</a>'; 
    print "</tr>";

现在在页面开始时检查请求:

if( isset($_GET['req']) ){
    switch($_GET['req']){
        case 'update':
           // Get the Unique ID
           // and perform your update query with update form
        break;
        case 'delete':
           // Get the Unique ID
           // and perform your delete query
        break;
    }
}

我告诉你如何实现这一目标的基本想法。