简体   繁体   English

如何将单个值发布到PHP MyAdmin。 此代码似乎无效。 我正在尝试从表单获取值到数据库

[英]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.

相关问题 为什么这个 JS 和 PHP 代码不起作用? (我正在尝试将值从 JS 发送到 PHP) - Why doesn't this JS & PHP code work? (I am trying to send a value from JS to PHP) 如何从php中的表单获取文件名? $_FILES[“filename”][“name”] 似乎不起作用 - How do I get the filename from a form in php? $_FILES[“filename”][“name”] doesn't seem to work 从sql数据库中选择数据无效。 我如何解决它? - Selecting data from sql database doesn't work. how do I fix it? 我的登录 PHP 表单不起作用。 我相信我的 $hash = $row['password']; 是问题,但我不太确定 - My LogIn PHP form doesn't work. I believe that my $hash = $row['password']; is the problem, but am not really sure php试图获取$ _post [&#39;value。$ i的价值 - php trying to get value for $_post['value .$i 当我从获取到发布切换我的php联系表单时,它不起作用 - When I switch my php contact form from get to post it doesn't work 为什么我不能使用get_post方法php从发布表单中获取值 - why can't i get the value from post form, with get_post method php 我正在尝试上传文件,但它不起作用。 我能做什么? [解决] - I'm trying to upload files but it doesn't work. What can I do? [Resolved] 使用$ _POST变量的PHP if语句似乎不起作用。 为什么? - PHP if-statement using $_POST variable doesn't seem to work. Why? PHP - 如何从数据库中仅获取 1 个值? - PHP - How do I get only 1 value from database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM