繁体   English   中英

我的数据未存储在数据库中

[英]My data is not being stored in my database

我似乎无法弄清楚我的代码出了什么问题。 我试图让它从输入

<form id="contact-form" action="emails.php" method="post">
    <input placeholder="Please enter your email address" name="emailz" type="email" tabindex="2" required>
    <input type="submit" name="submit" id="contact-submit" value="Subscribe">
</form>

并将其保存到我的数据库中。

这是我的PHP文件:

$servername = "localhost";
$username = "poweilup";
$password = "bloop";
$dbname = "poweilup_emails";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$insert = "INSERT INTO emails(addressEmail) VALUES($_POST '$emailz')";
$conn->close();

这是插入的方式-

$servername = "localhost";
$username = "poweilup";
$password = "bloop";
$dbname = "poweilup_emails";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$stmt = $conn->prepare("INSERT INTO `emails`(`addressEmail`) VALUES(?)");
$stmt->bind_param('s', $email);

$email = $_POST('emailz');
$stmt->execute();

首先,您准备查询并为项目变量保留占位符。 绑定每个参数(变量) ,然后声明它们。 最后,您执行查询。

为了安全地使用MySQLi,最好使用Prepared Statements 这将防止您的用户插入SQL注入或可能会错误地插入可能对您的MySQL服务器造成问题的字符。

正如您在下面的脚本中看到的那样,我首先使用占位符“?”准备SQL查询。 用于item变量。 将参数(变量)绑定到此占位符后。

现在查询已正确设置,是时候执行它了。 关闭任何打开后的东西始终是一个好习惯,因为这将释放不再使用的内存。

<?php

/* DB Info */
$servername = "localhost";
$username = "poweilup";
$password = "bloop";
$dbname = "poweilup_emails";

/* MySQLi Object */
$conn = new mysqli($servername, $username, $password, $dbname);

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Prepare query */
if ($stmt = $conn->prepare("INSERT INTO emails (addressEmail) VALUES (?)")){

    /* Bind POST data */
    $stmt->bind_param("s", $_POST['emailz']);

    /* Run query */
    $stmt->execute();

    /* Close statement */
    $stmt->close();
}

/* Close connection */
$conn->close();

?>

首先检查用户是否没有错过任何东西:

if (isset($_POST['emailz']) { //code here.. };

code here...code here...部分中,将emailz的值存储在这样的变量中:

$emailz = $_POST['emailz'];

当然,对于更重要的数据(例如密码)来说,这可能是不安全的。 您应该使用hash()函数。

要在数据库中插入变量,请尝试以下操作:

$query = "INSERT INTO `user` (emailz) VALUES ('$emailz')"; $result = mysql_query($query);     //inserts the variable in the DB.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM