I';我试图使用此表单捕获用户详细信息,并使用底部的PHP脚本将其加载到MySQL表中,但它';它不工作了 新闻稿 要定期接收更新,请订阅我们的邮件列表 通讯: 订阅
PHP脚本如下所示:I';我试图使用此表单捕获用户详细信息,并使用底部的PHP脚本将其加载到MySQL表中,但它';它不工作了 新闻稿 要定期接收更新,请订阅我们的邮件列表 通讯: 订阅,php,Php,PHP脚本如下所示: <div class="one_quarter"> <h6 class="heading">Newsletter</h6> <p class="nospace btmspace-15">To receive regular updates subscribe to our mailing list</p> <form acti
<div class="one_quarter">
<h6 class="heading">Newsletter</h6>
<p class="nospace btmspace-15">To receive regular updates subscribe to our mailing list</p>
<form action="php/newsletter.php" method="POST">
<fieldset>
<legend>Newsletter:</legend>
<input class="btmspace-15" type="text" placeholder="Name">
<input class="btmspace-15" type="text" placeholder="Email">
<textarea name="visitor" rows=4 cols=20></textarea>
<button class="btn" name="button" type="submit" value="submit">Subscribe</button>
</fieldset>
</form>
</div>
您的代码中有几个错误,最重要的是提交按钮的错误name=“button”
,而在PHP中,您正在检查是否设置($\u POST['submit'])
。第二件事是if(设置($\u POST['submit',$from))
。另外,正如有人提到的,使用SQL的准备语句,因为我更喜欢PDO,所以示例使用它
它的工作原理是:
您的输入需要name属性,即name=“email”和name=“name”,否则您的$\u帖子将不会有这些值。如果您执行var_dump($_POST),您已经看到了这一点。您应该意识到,如果您至少调试传入数据和呈现的SQL查询,请尊重他人并在StackOverflow编写问题之前调试代码,我们无法为您做基础工作。您还可以使用浏览器检查器检查POST请求中是否发送了有效的负载。你的情况不是这样的。我认为是插入了注释,但重定向不起作用?您是否已检查重定向的URL是否正确?可能是“/contacts/contact.html”而不是请求“/contact.html”(这使得它是/php/contact.html而不是/contacts/contact.html)?重定向url很好。实际上那部分是可选的。抱歉@biesior感到不受尊重。您应该真正使用参数化,而不是手动转义并将用户数据注入这样的查询中。我还怀疑您的过滤器输入()
-功能是否足以保护您免受SQL注入的影响。是否有任何特殊原因要否决此经过测试且有效的解决方案?
<?php
require 'dbconf.php';
$from = filter_input(INPUT_POST, 'email');
$first_name = filter_input(INPUT_POST, 'name');
$message = filter_input(INPUT_POST, 'visitor');
if (isset($_POST['submit'], $from)) {
$connection = mysqli_connect($servername, $username, $password);
if (!$connection) {
error_log("Failed to connect to MySQL: " . mysqli_error($connection));
die('Internal server error');
}
$db_select = mysqli_select_db($connection, 'webapp_comment');
if (!$db_select) {
error_log("Database selection failed: " . mysqli_error($connection));
die('Internal server error');
}
$sql = "INSERT INTO `webapp_comment.webapp_newsletter`(name,email,subscription_message) VALUES ('" . $first_name . "', '" . $from . "','" . $message . "')";
$result = mysqli_query($connection, $sql);
if ($result) {
echo '<script>alert("Your comment has been received . We will contact you shortly");</script>';
echo '<script>location.href = "../contact.html"</script>';
}
}
?>