1

我想在表单操作中调用 php 函数,并且我想将 id 作为参数传递。我正在做的是,在 html 表单中,数据库列值将显示在文本框中,如果我编辑这些值并单击“更新”按钮,数据库中的值应该被更新,并且“记录更新成功”消息应该显示在同一页面中。我尝试了下面的代码但没有工作。让我知道解决方案。提前致谢。

    <html>

    <head>

        <link rel="stylesheet" type="text/css" href="cms_style.css">        


    </head>

    <?php
        $ResumeID = $_GET['id']; 
        $con = mysql_connect("localhost", "root", "");
        mysql_select_db("engg",$con);
        $sql="SELECT * from data WHERE ResumeID=$ResumeID";
        $result = mysql_query($sql);
        $Row=mysql_fetch_row($result); 

        function updateRecord()
        {

            //If(!isset($_GET['id']))       
            //{
              $NameoftheCandidate=$_POST[NameoftheCandidate];
              $TelephoneNo=$_POST[TelephoneNo];
              $Email=$_POST[Email];

      $sql="UPDATE data SET NameoftheCandidate='$_POST[NameoftheCandidate]',         TelephoneNo='$_POST[TelephoneNo]', Email='$_POST[Email]' WHERE ResumeID=$ResumeID ";

            if(mysql_query($sql))
                echo "<p>Record updated Successfully</p>";
            else
                echo "<p>Record update failed</p>";



            while ($Row=mysql_fetch_array($result))     {
                echo ("<td>$Row[ResumeID]</td>");

                echo ("<td>$Row[NameoftheCandidate]</td>");
                echo ("<td>$Row[TelephoneNo]</td>");
                echo ("<td>$Row[Email]</td>");
            } // end of while

        } // end of update function


    ?>

    <body>
        <h2 align="center">Update the Record</h2>

        <form align="center" action="updateRecord()" method="post">
            <table align="center">
                <input type="hidden" name="resumeid" value="<? echo    "$Row[1]"?>">
            <? echo "<tr> <td> Resume ID </td>   <td>$Row[1]</td> </tr>" ?>                      

        <div align="center">
        <tr>       
        <td> Name of the Candidate</td>
    <td><input type="text" name="NameoftheCandidate" 
size="25" value="<? echo "$Row[0]"?     >"></td>
        </tr>

        <tr>
     <td>TelephoneNo</td>
    <td><input type="text" name="TelephoneNo" size="25" value="<? echo "$Row[1]"?>"></td>
     </tr>
     <tr>       
      <td>Email</td>
        <td><input type="text" name="Email" size="25" value="<? echo "$Row[3]"?>">
        </td>
    </tr>
     <tr>
    <td></td>
    <td align="center"><input type="submit" name="submitvalue" value="UPDATE" ></td>
                    </tr>
                </div>                              
            </table>
        </form>

      </body>
       </html>
4

3 回答 3

2

试试这个方法

HTML

<form align="center" action="yourpage.php?func_name=updateRecord" method="post">

PHP

$form_action_func = $_POST['func_name'];

if (function_exists($form_action_func)) {
  updateRecord();
}
于 2013-03-18T06:32:48.083 回答
2

编写表单 action="" 然后编写您的 php 代码,如下注:使用表单方法作为 get

<?php
if(isset($_GET['id']))
{
       call your function here
}
?>

在函数中使用 $_GET['fieldname'] 访问所有值

于 2013-03-18T06:41:10.160 回答
0

简单的方法使您的“提交”和“更新”操作在同一页面上执行然后

if(isset($_POST['update']))
{
 //perform update task
 update($var1,var2,$etc); // pass variables to function
 header('Location: http://www.example.com/');// link to your form
}
else if(isset($_POST['submit']))
{
//perform update task
submit($var1,$var2,$etc);// pass variables to function
header('Location: http://www.example.com/'); // link to next page after submit successfully 
}
else
{
// display form
}
于 2013-03-18T12:00:58.180 回答