【问题标题】:Second form submit button seems to be skipped(?) PHP / HTML第二个表单提交按钮似乎被跳过(?)PHP / HTML
【发布时间】:2014-05-12 14:02:05
【问题描述】:

我需要这方面的帮助,我有一个 PHP 页面,它根据名字和姓氏搜索记录,如果 sql 找到数据,那么会出现第二种形式,它有很多文本框,用于更新 '搜索'信息。当我单击第二个表单的提交按钮时,它什么也不做,即使我有语法或我在条件 if(isset($_POST['submit'])) 条件下设置的任何错误,它们最终都会被忽略(不会出现错误消息),单击提交按钮后,它只是回到页面的原始状态,当它必须更新我搜索的刚刚编辑的记录时。这部分到底有什么错误?

class.php - 包含 sql 操作的 .php 文件

<? php

$months = array('January',
               'February', 
                  'March',
                  'April',
                    'May',
                   'June', 
                   'July',
                 'August', 
              'September', 
                'October',
               'November', 
               'December');

class EmployeeProfile {


    public
    function openConnection() {
        $conn = mysqli_connect("localhost", "root", "", "db_employee");

        if (mysqli_connect_errno()) {
            echo "Failed to connect to database server";
        }


        return $conn;

    }

    public
    function insert($query) {

        if (mysqli_query($this - > openConnection(), $query) == 1) {
            echo "Profile successfully registered!";
        } else {
            echo "Register failed";
        }

    }

    public
    function display($query) {

        $result = mysqli_query($this - > openConnection(), $query);
        echo "<br><br>";
        if ($result - > num_rows == 1) {
            while ($row = $result - > fetch_assoc()) {
                echo "<table>";
                echo "<tr><td><b>First Name</b>: ".$row["firstname"]."</td></tr>";
                echo "<tr><td><b>Middle Name</b>: ".$row["middlename"]."</td></tr>";
                echo "<tr><td><b>Last Name: </b>".$row["lastname"]."</td></tr>";
                echo "<tr><td><b>Date of Birth: </b>".$row["dateofbirth"]."</td></tr>";
                echo "<tr><td><b>Age: </b>".$row["age"]."</td></tr>";
                echo "<tr><td><b>School: </b>".$row["school"]."</td></tr>";
                echo "<tr><td><b>Highest Educational Attainment: </b>".$row["educ"]."</td></tr>";
                echo "<tr><td><b>Year Last Attended: </b>".$row["yearattended"]."</td></tr>";
                echo "<tr><td><b>Skills: </b>".$row["skills"]."</td></tr>";
                echo "<tr><td><b>Previous Company: </b>".$row["prevcompany"]."</td></tr>";
                echo "<tr><td><b>Position: </b>".$row["position"]."</td></tr>";
                echo "<tr><td><b>Date of Employment:</b> ".$row["dateofemployment"]."</td></tr>";
                echo "</table>";
            }
        } else

        {

            echo "Profile not found";
        }

    }

    public
    function edit($query) {

        $result = mysqli_query($this - > openConnection(), $query);

    }

}

?>

edit.php - 页面本身。

<html>
<title> Edit Profile</title>
<body>

<form method="post" action="?" name="searchform">
<center>
<table>
<tr><td>Enter first or last name</td><td><input type = "text" name="search"><td><td><input type = "submit" value="Search" name="search2"></td></tr>
</form>
</table>

<?php
include("class.php");
if(isset($_POST['search2'])):

$status = "hidden";
$query = "select * from employee WHERE firstname='".$_POST['search']."' OR lastname='".$_POST['search']."' ";
$emp = new EmployeeProfile();
$emp->openConnection();

$result = mysqli_query($emp->openConnection(), $query);


if($result->num_rows == 1):




?>

<form method="post" action="?" enctype="multipart/form-data" name="updateform">
<table>
<tr></tr>
<tr><td></td><td>Edit your profile:</td></tr>
<tr></tr>
<tr><td>*Enter first name:</td><td><input type="text" name="firstname"></td></tr>
<tr><td>Enter middle name:</td><td><input type="text" name="middlename"></td></tr>
<tr><td>*Enter last name:</td><td><input type="text" name="lastname"></td></tr>
<tr>
<td>*Date of Birth:</td><td><select name="month"><?php for($i = 0; $i < count($months); $i++) { echo "<option value=".$months[$i]." >".$months[$i]."</option>" ; }?></select></td>
<td><select  name="days"><?php for($i = 1; $i <= 31; $i++) { echo "<option value"."=".$i.">".$i."</option>";  } ?> </select></td>
<td><select  name="year"><?php for($i = 1950; $i <= 2014; $i++) { echo "<option value"."=".$i.">".$i."</option>";  } ?> </select></td>
</tr>
<tr><td>*Age:</td><td><input type="text" name="age"></td></tr>
<tr><td>*School:</td><td><input type="text" name="school"></td></tr>
<tr><td>*Highest Educational Attainment:</td><td><input type="text" name="educ"></td></tr>
<tr><td>*Year Last Attended:</td><td><input type="text" name="yearattended"></td></tr>
<tr><td>*Skill(s):</td><td><input type="text" name="skills"></td></tr>
<tr><td>Previous Company:</td><td><input type="text" name="prevcompany"></td></tr>
<tr><td>Position:</td><td><input type="text" name="position"></td></tr>
<tr><td>*Date of Employment:</td><td><select name="empmonth"><?php for($i = 0; $i < count($months); $i++) { echo "<option value=".$months[$i]." >".$months[$i]."</option>" ; }?></select></td>
<td><select  name="empyear"><?php for($i = 1950; $i <= 2014; $i++) { echo "<option value"."=".$i.">".$i."</option>";  } ?> </select></td>
</tr>
<tr><td></td><td><input type="submit" value="Register" name="submit"></td></tr>
<tr><td>* - Required</td></tr>
</form>

<?php


if(isset($_POST['submit'])):
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$dateofbirth = $_POST['month']. " ".$_POST['days']. ", ".$_POST['year'];
$age = $_POST['age'];
$school = $_POST['school'];
$educ = $_POST['educ'];
$yearattended = $_POST['yearattended'];
$skills = $_POST['skills'];
$prevcompany = $_POST['prevcompany'];
$position = $_POST['position'];
$dateofemployment = $_POST['empmonth']. " ".$_POST['empyear'];

$row = $result->fetch_assoc();
$usr = $row["firstname"];


$query2 = "UPDATE employee SET firstname='$firstname', middlename='$middlename', lastname='$lastname', dateofbirth='$dateofbirth', age='$age', school='$school',
educ='$educ', yearattended='$yearattended', skills='$skills', prevcompany='$prevcompany', position='$position', dateofemployment='$dateofemployment',
 WHERE firstname='$usr'";

 mysqli_query($emp->openConnection(), $query2);

endif;





else:
echo "Profile not found";
endif;
endif;


?>

</table>
</center>
</body>
</html>

我确实认为这条线及以后的线会被忽略。

这是上面显示的edit.php 文件的一部分。

<?php

if(isset($_POST['submit'])):
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$dateofbirth = $_POST['month']. " ".$_POST['days']. ", ".$_POST['year'];
$age = $_POST['age'];
$school = $_POST['school'];
$educ = $_POST['educ'];
$yearattended = $_POST['yearattended'];
$skills = $_POST['skills'];
$prevcompany = $_POST['prevcompany'];
$position = $_POST['position'];
$dateofemployment = $_POST['empmonth']. " ".$_POST['empyear'];

$row = $result->fetch_assoc();
$usr = $row["firstname"];


$query2 = "UPDATE employee SET firstname='$firstname', middlename='$middlename', lastname='$lastname', dateofbirth='$dateofbirth', age='$age', school='$school',
educ='$educ', yearattended='$yearattended', skills='$skills', prevcompany='$prevcompany', position='$position', dateofemployment='$dateofemployment',
 WHERE firstname='$usr'";

mysqli_query($emp->openConnection(), $query2);


endif;

【问题讨论】:

  • " 只是回到页面的原始状态" ---> 那一定是表单提交
  • 如果点击了第二个表单提交按钮,我希望它更新 sql 中的配置文件,但没有任何反应,仅此而已,但我向您保证第一个表单(搜索,只有一个文本框和一个提交按钮的表单)有效。
  • 这是故意action="?"吗?
  • 另外,删除dateofemployment='$dateofemployment', 末尾的逗号,这很可能是问题所在。如果您想将该操作用作同一页面,请执行action=""
  • @Fred-ii- 我只是按照我的好友告诉我的,因为他在 PHP 方面比我好,所以我就跟着它。我认为问题在于“if(isset($_POST['submit'])):” 并且超出的代码只是被忽略了,即使我故意放置了语法错误它什么也不做,甚至没有错误消息出现完全没有。

标签: php html


【解决方案1】:

一般来说,存在两种错误。

  1. HTML 标记顺序错误,范围广泛
  2. 语法错误。
    - &gt; 必须是 -&gt; 才能正确解析
    &lt;? php 必须是 &lt;?php 才能正确解析

两个文件中都存在语法错误。

注意:以下代码包含一些调试语句。

<html>
<head>
<?php
    echo "<p>In myquery.php header </p>";
    error_reporting(E_ALL);
    //echo "<p>" . var_dump($_POST); . "</p>";
    //echo "<p>" . var_dump($_GET); . "</p>";
?>

<?php
    include("class.php");
?>

    <title> Edit Profile</title>
</head>

<body>
<?php
    echo "<p>In myquery.php body</p>";
?>

    <form method="post" action="?" name="searchform">
        <table>
            <tr>
                <td>Enter first or last name</td>
                <td><input type = "text" name="search"><td>
                <td><input type = "submit" value="Search" name="search2"></td>
            </tr>
        </table>
    </form>


<?php

    echo "<p>about to check _post for search2</p>";

    if(isset($_POST['search2'])):
        echo "<p>found _post for search2</p>";   

        $status = "hidden";
        $query = "select * from employee WHERE firstname='".$_POST['search']."' OR lastname='".$_POST['search']."' ";
        echo "<p>about to open DB</p>";
        $emp = new EmployeeProfile();
        $emp->openConnection();
        echo "<p>about to place find query</p>";            
        $result = mysqli_query($emp->openConnection(), $query);

        echo "<p>about to check for successful query</p>";
        if($result->num_rows == 1):

            echo "<p>successful search query</p>";
?>

            <form method="post" action="?" enctype="multipart/form-data" name="updateform">
                <table>
                    <tr></tr>
                    <tr><td></td><td>Edit your profile:</td></tr>
                    <tr></tr>
                    <tr><td>*Enter first name:</td><td><input type="text" name="firstname"></td></tr>
                    <tr><td>Enter middle name:</td><td><input type="text" name="middlename"></td></tr>
                    <tr><td>*Enter last name:</td><td><input type="text" name="lastname"></td></tr>
                    <tr>
                    <td>*Date of Birth:</td><td><select name="month"><?php for($i = 0; $i < count($months); $i++) { echo "<option value=".$months[$i]." >".$months[$i]."</option>" ; }?></select></td>
                    <td><select  name="days"><?php for($i = 1; $i <= 31; $i++) { echo "<option value"."=".$i.">".$i."</option>";  } ?> </select></td>
                    <td><select  name="year"><?php for($i = 1950; $i <= 2014; $i++) { echo "<option value"."=".$i.">".$i."</option>";  } ?> </select></td>
                    </tr>
                    <tr><td>*Age:</td><td><input type="text" name="age"></td></tr>
                    <tr><td>*School:</td><td><input type="text" name="school"></td></tr>
                    <tr><td>*Highest Educational Attainment:</td><td><input type="text" name="educ"></td></tr>
                    <tr><td>*Year Last Attended:</td><td><input type="text" name="yearattended"></td></tr>
                    <tr><td>*Skill(s):</td><td><input type="text" name="skills"></td></tr>
                    <tr><td>Previous Company:</td><td><input type="text" name="prevcompany"></td></tr>
                    <tr><td>Position:</td><td><input type="text" name="position"></td></tr>
                    <tr><td>*Date of Employment:</td><td><select name="empmonth"><?php for($i = 0; $i < count($months); $i++) { echo "<option value=".$months[$i]." >".$months[$i]."</option>" ; }?></select></td>
                    <td><select  name="empyear"><?php for($i = 1950; $i <= 2014; $i++) { echo "<option value"."=".$i.">".$i."</option>";  } ?> </select></td>
                    </tr>
                    <tr><td></td><td><input type="submit" value="Register" name="submit"></td></tr>
                    <tr><td>* - Required</td></tr>
                </table>
            </form>

<?php
            echo "<p>about to check for submit second form</p>";

            if(isset($_POST['submit'])):

                echo "<p>found submit for second form</p>";

                $firstname = $_POST['firstname'];
                $middlename = $_POST['middlename'];
                $lastname = $_POST['lastname'];
                $dateofbirth = $_POST['month']. " ".$_POST['days']. ", ".$_POST['year'];
                $age = $_POST['age'];
                $school = $_POST['school'];
                $educ = $_POST['educ'];
                $yearattended = $_POST['yearattended'];
                $skills = $_POST['skills'];
                $prevcompany = $_POST['prevcompany'];
                $position = $_POST['position'];
                $dateofemployment = $_POST['empmonth']. " ".$_POST['empyear'];

                $row = $result->fetch_assoc();
                $usr = $row["firstname"];


                $query2 = 
                    "UPDATE employee 
                    SET firstname='$firstname', 
                        middlename='$middlename', 
                        lastname='$lastname', 
                        dateofbirth='$dateofbirth', 
                        age='$age', 
                        school='$school',
                        educ='$educ', 
                        yearattended='$yearattended', 
                        skills='$skills', 
                        prevcompany='$prevcompany', 
                        position='$position', 
                        dateofemployment='$dateofemployment',
                    WHERE firstname='$usr'";

                echo "<p>about to update DB</p>";

                mysqli_query($emp->openConnection(), $query2);
            endif;
        else:
            echo "<p>search query failed</p>";

            echo "Profile not found";
        endif;
    endif;
?>

</body>
</html>

【讨论】:

  • @wingedsubmariner 谢谢你,但我还是不能试试这个,因为我要去某个地方,一定会检查你的修订版!再次感谢你!!!你让我的代码可读!
  • @user3628778 答案实际上来自 user3629249,我只是清理了一些格式。别忘了给他点赞!
  • 我已经尝试执行改进的代码,但它仍然没有更新编辑的记录,这有点荒谬。
  • 我可能别无选择,只能在点击第二个提交表单后重定向到另一个页面。
猜你喜欢
  • 2018-08-25
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
相关资源
最近更新 更多