php - 编辑表单无法在 PHP 中使用 mysql,将数据处理为 href 但不运行测试打印行

标签 php mysql post get

在尝试调试这段代码几个小时后,我发现我无法弄清楚为什么我的编辑表单永远不会更新。我不确定是因为我没有正确使用 GET 或 POST 方法,还是错误地使用了 mysql,或者是两者的组合。我什至不明白为什么一行打印“hi”;不会出现。如果我在点击编辑提交按钮时取出代码测试行,则会显示打印行,但我的数据库不会更新。所以我想我陷入了无法再进行打印行调试的困境,直到我弄清楚我做错了什么。这是我的代码..我在“print“hi”;”旁边发表了评论未显示的行。请记住,我很确定我尝试了 GET 和 POST 的每种组合,但它仍然没有显示......

<html lang="en">

<head>  
    <title>Employee</title>
</head>
<body>

<a href="employ.php">Clean</a> <br>

<form method="post" action="employ.php">
<input type="text" name="fname">First Name<br>
<input type="text" name="lname">Last Name<br>
<input type="text" name="email">email<br>
<input type="text" name="zip">zip code<br>

<input type="submit" name="add" value="Add"> <!-- button itself -->
</form>
<br>


<?php                   //server    login name    password  database
$link = mysqli_connect("server", "login", "password", "database") or die(mysqli_error());




if(isset($_POST['add'])) //this processes after user submits data.
{
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $zip = $_POST['zip'];

    $re = "/^[a-zA-Z]+(([\'\- ][a-zA-Z])?[a-zA-Z]*)*$/";
    $reEmail = "/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/";
    $reZip = "/^\d{5}$/";

    //if user passes re test
    if( preg_match($re, $fname) && preg_match($re, $lname) 
        && preg_match($reEmail, $email) && preg_match($reZip, $zip) )
    {   //display current table
        $querycheck = "select * from employees where fname='$fname' and email='$email'";
        $resultcheck = mysqli_query($link, $querycheck); //link query to database

        if(mysqli_num_rows($resultcheck) == 0)// test if query does "nothing"
        {//if not process the insert query
            $query = "insert into employees values('', '$fname', '$lname', '$email', '$zip')";
            mysqli_query($link, $query); //link query to database
            print "Employee Added"; // print confirmation
        }
        else
        {
            print "That record already exists!";
        }
    }
    else
    {
        print "You did not fill out the form correctly!";
    }

} ////////////////////////////////edit portion/////////////////////////////
if(isset($_GET['edit']))
{
    print "teseting edit<br><br>";
    ?>

    <form method="get" action="employ.php"> 
    <input type="text" name="fname" value = "<?php echo $_GET['fname']?>">First Name<br>
    <input type="text" name="lname" value = "<?php echo $_GET['lname']?>">Last Name<br>
    <input type="text" name="email" value = "<?php echo $_GET['email']?>">email<br>
    <input type="text" name="zip"   value = "<?php echo $_GET['zip']?>">zip code<br>

    <input type="hidden" name="employeeid" value = "<?php echo $_GET['employeeid']?>">

    <input type="submit" name="endedit" value="Edit"> <!-- button itself -->
    </form>

    <?php

    print "teseting end edit <br><br>";



    if(isset($_POST['endedit'])) //this processes after user submits edited data
    {                                //tried get and post
        print "hi";  // DOESNT APPEAR
        $fname = $_POST['fname'];
        $lname = $_POST['lname'];
        $email = $_POST['email'];
        $zip = $_POST['zip'];
        $employeeidtemp = $_POST['employeeid'];

        $re = "/^[a-zA-Z]+(([\'\- ][a-zA-Z])?[a-zA-Z]*)*$/";
        $reEmail = "/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/";
        $reZip = "/^\d{5}$/";

        //if user passes re test
        if( preg_match($re, $fname) && preg_match($re, $lname) 
        && preg_match($reEmail, $email) && preg_match($reZip, $zip) )
        {   //display current table
            //$querycheck = "select * from employees where employeeid='$employeeidtemp'";
            //$resultcheck = mysqli_query($link, $querycheck); //link query to database

        //  if(mysqli_num_rows($resultcheck) == 0)// test if query does "nothing"
        //  {
                $query = "UPDATE employees SET fname='$fname', lname='$lname', email='$email', zip='$zip' WHERE employeeid='$employeeidtemp'";
                mysqli_query($link, $query); //link query to database
                print "Employee Updated"; // print confirmation
        //  }
        //  else
        //      print "huh?";

        }
        else
        {
            print "You did not fill out the form correctly!";
        }

    }




}

if(isset($_GET['delete']))
{
    print "teseting delete<br><br>";
}



showemp();




function showemp()
{
    global $link;
    if(isset($_GET['choice']))
    {
        $choice = $_GET['choice'];
    }
    else
    {
        $choice = "lname";
    }

    $query = "select * from employees order by $choice";
    $result = mysqli_query($link, $query);


// print table (happens first before input)

    // first print row of links/headers that sort
    print "<table border='1'>

    <tr>
    <th>Edit</th>
    <th>Delete</th>
    <th><a href='employ.php?choice=fname'>FNAME</a></th> 
    <th><a href='employ.php?choice=lname'>LNAME</a></th>
    <th><a href='employ.php?choice=email'>EMAIL</a></th>
    <th><a href='employ.php?choice=zip'>ZIP</a></th>
    </tr>";

    //while the next row (set by query) exists?
    while($row = mysqli_fetch_row($result))
    {
        list($employeeid, $fname, $lname, $email, $zip) = $row;
        print "<tr>
        <td><a href='employ.php?edit=yes&employeeid=$employeeid&fname=$fname&lname=$lname&email=$email&zip=$zip'>Edit</a></td>
        <td><a href='employ.php?delete=yes&employeeid=$employeeid
                      onclick='return confirm(\"Are you sure\")'>Delete</a></td>
        <td>$fname</td>
        <td>$lname</td>
        <td>$email</td>
        <td>$zip</td>
        </tr>";
    }

    print "</table>";
}




?>
</body>
</html>

最佳答案

您有几个错误:

  1. 您不检查查询结果,至少使用 代码 mysqli_query($link, $query) 或 die(mysqli_error($link));

当我通过检查错误来踢你的代码时,我发现添加查询不起作用 - 我的整数字段不接受你的 employeeid 空字符串值。

  • 不要在表单中使用 GET。始终发布。如果您需要对 GET-url 进行 react ,请单独编写或使用 $_REQUEST var。
  • 在 INSERT 查询中始终写入字段。当您决定更改 mysql 表中的字段列表时,您会得到这段代码的奇怪行为。
  • 您的主要错误是您的 print 'hi' 条件位于条件 if(isset($_GET['edit'])) 内,当用户 sublim 表单时它不起作用。

  • 关于php - 编辑表单无法在 PHP 中使用 mysql,将数据处理为 href 但不运行测试打印行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26374895/

    相关文章:

    php - 有关 jQuery valid8 的帮助

    mysql - 使用前一行的值和分组更新查询

    jquery - 在返回 jquery ajax 帖子时获取特定的 html

    xml - 使用用户输入数据在 angularjs 中生成 xml 并将其发送到 Web 服务

    javascript - 使用AJAX形式更新数据库

    php - 注册流程+php mysql

    php - 将 mysql 查询存储到 php 变量后发出 undefined variable

    php - SQLSTATE[HY000] [1040] 连接太多

    python - iOS 不发送 POST 请求负载

    php - CMS 还是框架?