sql调用-这是正确的吗?

0vvn1miw  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(296)

我最近搬到了另一个地方,我遇到了一个问题。当我以网站的形式输入数据时,数据库不会更新。然而,当我的托管服务的运营商站在他们这边做的时候,这个表单似乎对他们有用。为什么会这样?
下面是php中的数据库调用

<?php

error_reporting(E_ALL); ini_set('display_errors', 1);

$name = $_POST['name1'];
$surname = $_POST['surname1'];
$email = $_POST['email1'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

    define('DB_NAME', 'name');
    define('DB_USER', 'user');
    define('DB_PASSWORD', 'password');
    define('DB_HOST','localhost');

    $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    $name = mysqli_real_escape_string($conn, $name);
    $surname = mysqli_real_escape_string($conn,$surname);
    $email = mysqli_real_escape_string($conn,strtolower($email));

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "INSERT INTO interested (Name, Surname, Email) VALUES ('$name', '$surname', '$email')";

    if ($conn->query($sql) === TRUE) {
        echo "Thank you!";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

} else {
    echo "<span>* invalid email *" + $email +"</span>";
}
?>
1tuwyuhd

1tuwyuhd1#

我忘了提到,你也必须激活错误报告的mysql级别了。e、 g.设置 $mysqliDriver->report_mode ,如下所示。
因此,在更改db凭证之后,按原样测试此代码。如果有效,那么问题就存在于代码中。如果不是,那它就在别的地方。

<?php
// Db configs.
define('HOST', 'localhost');
define('PORT', 3306);
define('DATABASE', 'tests');
define('USERNAME', 'root');
define('PASSWORD', 'root');

/*
 * Enable internal report functions. This enables the exception handling,
 * e.g. mysqli will not throw PHP warnings anymore, but mysqli exceptions
 * (mysqli_sql_exception).
 *
 * MYSQLI_REPORT_ERROR: Report errors from mysqli function calls.
 * MYSQLI_REPORT_STRICT: Throw a mysqli_sql_exception for errors instead of warnings.
 *
 * @link http://php.net/manual/en/class.mysqli-driver.php
 * @link http://php.net/manual/en/mysqli-driver.report-mode.php
 * @link http://php.net/manual/en/mysqli.constants.php
 */
$mysqliDriver = new mysqli_driver();
$mysqliDriver->report_mode = (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// Create a new db connection.
$connection = new mysqli(HOST, USERNAME, PASSWORD, DATABASE, PORT);

// Signalize if a new record could be inserted, or not.
$recordInserted = FALSE;

// Operations upon form submission.
if (isset($_POST['submit'])) {
    // Read the posted values.
    $name = $_POST['name1'] ?? '';
    $surname = $_POST['surname1'] ?? '';
    $email = $_POST['email1'] ?? '';

    // Validate the name.
    if (empty($name)) {
        $errors[] = 'Please provide the name.';
    } /* Other validations here using elseif statements */

    // Validate the surname.
    if (empty($surname)) {
        $errors[] = 'Please provide the surname.';
    } /* Other validations here using elseif statements */

    // Validate the email.
    if (empty($email)) {
        $errors[] = 'Please provide an email address.';
    } elseif (
            !filter_var($email, FILTER_SANITIZE_EMAIL) ||
            !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'The email address is not in a valid format.';
    } /* Other validations here using elseif statements */

    // Insert a new record - if no errors yet.
    if (!isset($errors)) {
        $sql = 'INSERT INTO interested (
                    Name,
                    Surname,
                    Email
                ) VALUES (
                    ?, ?, ?
                )';

        $statement = $connection->prepare($sql);
        $statement->bind_param('sss', $name, $surname, $email);
        $statement->execute();

        // Signalize that the new record was successfully inserted.
        $recordInserted = TRUE;
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
        <meta charset="UTF-8" />
        <!-- The above 3 meta tags must come first in the head -->

        <title>Demo - Insert</title>
    </head>
    <body>

        <div class="messages">
            <?php
            if (isset($errors)) {
                echo implode('<br/>', $errors);
            } elseif ($recordInserted) {
                echo 'A new record was successfully inserted.';
            }
            ?>
        </div>

        <br/>

        <form action="" method="post">
            <label for="name1">Name</label>
            <input type="text" id="name1" name="name1" placeholder="Name" required>
            <br/>
            <label for="surname1">Surname</label>
            <input type="text" id="surname1" name="surname1" placeholder="Surname" required>
            <br/>
            <label for="email1">Email</label>
            <input type="email" id="email1" name="email1" placeholder="Email" required>

            <button type="submit" id="submit" name="submit" value="submit">
                Submit
            </button>
        </form>

    </body>
</html>

相关问题