如何向myphp admin发布一个值。这段代码似乎不起作用。我正在尝试从表单中获取一个值到数据库中

时间:2022-08-26 20:16:07
<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.

它们都在不同的页面上,我似乎不明白为什么它们不能工作。如有任何帮助,我们将不胜感激!根本不向数据库发送任何内容。饼干还在工作。

1 个解决方案

#1


2  

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. 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;
}

#1


2  

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. 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;
}