成功查询后未显示成功消息,但表单数据仍然发送到DB
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了成功查询后未显示成功消息,但表单数据仍然发送到DB相关的知识,希望对你有一定的参考价值。
我正在制作一个代码,允许用户输入他们的电子邮件和另一个用户的电子邮件,将他们作为“朋友”添加到表“朋友”中
到目前为止,我的代码在将表单数据发布到DB /表“朋友”方面起作用,但是我想要显示的消息根本没有出现。
我的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);
?>
任何帮助是极大的赞赏。我是新手程序员。
答案
我的函数generatePage错了。我在函数中添加了HTML,现在它可以工作了!
以上是关于成功查询后未显示成功消息,但表单数据仍然发送到DB的主要内容,如果未能解决你的问题,请参考以下文章