成功查询后未显示成功消息,但表单数据仍然发送到DB

时间:2017-12-16 19:16:48

标签: php html mysql pdo

我正在制作一个代码,允许用户输入他们的电子邮件和另一个用户的电子邮件,将他们作为“朋友”添加到“朋友”表中

到目前为止,我的代码在将表单数据发布到DB /表“friends”方面起作用,但是我想要显示的消息根本没有出现。

我的HTML表单:

<form class="form-signin" action="FriendLookup.php" method = "POST" enctype="multipart/form-data">
        <h2 class="form-signin-heading">Add a Friend</h2>
        </br>
        <label for="inputEmail" class="sr-only">Your Email</label>
        <input type="text" id="inputEmail1" name = "self_email" class="form-control" placeholder="Friend's Username" >
        </br>
        <label class="sr-only">Your Friend's Email</label>
        <input type="text" id="inputEmail2" name = "friend_email" class="form-control" placeholder="Your Username" >
        </br>
        <button class="btn btn-lg btn-primary btn-block" name = "submit" type="submit">Search</button>
      </form>

PHP脚本:

<?php
include_once('support.php');
//connect_database.php contains your connection/creation of a PDO to connect to your MYSQL db on bmgt406.rhsmith.umd.edu/phpmyadmin
include_once('connect_database.php');
ini_set("display_errors","1");
error_reporting(E_ALL);

// Initialize $title and $body.
$title = "Add User";
$body = "<fieldset><legend> $title </legend>";


$name_of_table = "friends";



// Check if the table exists in the db.
if (tableExists($db, $name_of_table)) {

    $inputemail1 = $_POST['self_email'];
    $inputemail2 = $_POST['friend_email'];

        // Prepare a SQL query and bind all 6 variables.
        $sqlQuery = "INSERT INTO $name_of_table ( self_email, friend_email)
        VALUES ( :self_email, :friend_email)";



        $statement1 = $db->prepare($sqlQuery);

        $statement1->bindValue(':self_email', $inputemail1, PDO::PARAM_STR);
        $statement1->bindValue(':friend_email', $inputemail2, PDO::PARAM_STR);


        // Execute the SQL query using $statement1->execute(); and assign the value
        // that is returned  to $result.
        $result = $statement1->execute();

        if(!$result) {
                // Query fails.
                $body .= "Inserting entry for friend failed.";
        } else {
                // Query is successful.
                   $body .= "Success";
        }


        // Closing query connection
                $statement1->closeCursor();

           }



$body .= "</fieldset>";
echo generatePage($title,$body);
?>

非常感谢任何帮助。我是新手程序员。

1 个答案:

答案 0 :(得分:-1)

我的函数generatePage错了。我在函数中添加了HTML,现在它可以工作了!