第二个表单提交按钮似乎被跳过(?)PHP / HTML

时间:2014-05-12 14:02:05

标签: php html

我需要这方面的帮助,我有一个PHP页面,根据它们的名字和姓氏搜索记录,如果sql找到数据,那么第二个表单出来,有很多文本框,来更新& #39;搜索'的信息。当我点击第二个表单的提交按钮时,它什么也没做,即使我有语法或任何错误我已经放了条件如果(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;

1 个答案:

答案 0 :(得分:0)

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

  1. HTML广告代码错误,广泛
  2. 语法错误。
    - >必须->才能正确解析 <? php必须<?php才能正确解析
  3. 两个文件中都存在语法错误。

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

    <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>