[英]How do I post a single value to PHP MyAdmin. This code doesn't seem to work. I am trying to get a value from a form into a database
<form action="signup.php" method="POST" id="newsletter">
<h4>Join Our Newsletter</h4>
<input id="email" type="text" value="Enter Email Address Here For Updates" onBlur="javascript:if(this.value==''){this.value=this.defaultValue;}" onFocus="javascript:if(this.value==this.defaultValue){this.value='';}">
<input type="submit" value="Sign up" class="btn2">
</form>
<?php
$dsn = "mysql:dbname=test_db";
$DBusername = "test";
$DBpassword = "test";
try {
$conn = new PDO($dsn, $DBusername, $DBpassword);
}
catch(PDOException $e) {
}
$email = $_POST["email"];
$sql = "INSERT INTO contacts (email) VALUES (:email)";
$pdoQuery = $conn->prepare($sql);
$pdoQuery->bindValue(":email", $email, PDO::PARAM_STR);
$pdoQuery->execute();
setcookie("success", "You have successfully signed up for the newsletter.", 0, "/");
header("Location: index.php");
?>
These are both on separate pages, and I can't seem to figure out why they won't work. 这些都在单独的页面上,我似乎无法弄清楚为什么它们不起作用。 Any help would be greatly appreciated! 任何帮助将不胜感激! Nothing posts to the database at all. 什么都没有发布到数据库。 The cookie is working though. Cookie正在运行。
Your connection is probably failing. 您的连接可能失败。 You don't have a host in your DSN 您的DSN中没有主机
$dsn = "mysql:host=localhost;dbname=test_db";
Not sure if it is localhost, but hope this shows what it should be. 不知道它是否是localhost,但是希望它能显示出它应该是什么。 It would also probably help that when you catch the exception, that you output something. 这也可能会帮助您在捕获异常时输出某些内容。
even... 甚至...
catch(PDOException $e) {
echo "error - ".$e->getMessage().PHP_EOL;
exit;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.