php - 从 Materialise Form 添加到 MySQL

标签 php html mysql forms materialize

在有人说这是重复之前,我已经检查并尝试了 this 中的解决方案之前问过的问题。我相信我的问题是不同的,因为我没有单独的 php 文件 - 它是用 HTML 中的标签进行编码的(因此所有内容都在同一个文档中)。

这是我的 PHP(数据库信息留空):

<?php

session_start();
    $dbhost = '****';
    $dbuser = '****';
    $dbpass = '****';
    $dbname = '****';

$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);


$email=$_POST['email'];

$query="INSERT INTO tableName(email)
VALUES('$email')";

mysqli_free_result($result);

mysqli_close($connection);

?>

这是我的 Materialize/HTML 表单:

<form action="/thankyou.php" method="POST">
        <p class="input-header">Enter Your Email:</p>
        <input id="email" type="email" name= "email" class="validate" required>
        <br></br>
        <input class="waves-light btn indigo lighten-2" type="submit">
        <br></br>
        <br></br>
        <br></br>

</form>

对于为什么它不起作用有什么想法吗?我检查了我的 MAMP phpmyadmin 数据库,没有添加任何内容。如果您有任何建议,请告诉我!谢谢!

最佳答案

这将对您有所帮助:因为您尚未添加 mysqli_query 并且因此它没有添加数据。另外,我还在这里检查表单是否已提交,正如您提到的那样,它是单个文件。

<?php
// check if form is submitted
if(!empty($_POST['email'])) {
    $dbhost = '****';
    $dbuser = '****';
    $dbpass = '****';
    $dbname = '****';

    $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
    // this line prevents sql injection
    $email = mysqli_real_escape_string($connection, $_POST['email']);

    $query="INSERT INTO tableName(email) VALUES('$email')";
    // this statement runs your query and actually adds data to mysql
    if (mysqli_query($connection, $query)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($connection);
    }
    mysqli_close($connection);
}
?>

关于php - 从 Materialise Form 添加到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902994/

相关文章:

php - 在 PHP 中的类属性中存储闭包函数

html - bootstrap 3 无法更改背景图片

mysql c api : inserting numbers without converting them to a string

mysql - 如何在不创建索引的情况下检索 MySQL 中的列选择性元数据?

c# - 对关系的困惑

php - php页面出现未定义索引的警告,如何去除?

php - 通过属性值 PHP 对 XML 进行排序

php - Laravel/ Eloquent : how to hook into the deletion process?

javascript - 如何显示 slider 左侧的navigationMenu div

html - 使表格单元格中的链接填充整个行高