SQL调用-这正确吗?

时间:2018-06-26 19:45:48

标签: php html mysql database

我最近搬到了另一台主机上,出现了问题。当我以网站的形式输入数据时,数据库不会更新。但是,当我的托管服务的运营商站在他们一边时,该表格似乎对他们有用。为什么会这样?

这是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>";
}
?>

1 个答案:

答案 0 :(得分:0)

我忘了提到您也必须激活报告mysql级别的错误。例如。设置<?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> ,如下所示。

因此,更改数据库凭据后,请按原样测试此代码。如果有效,则问题出在您的代码中。如果没有,那么它就在其他地方。

  $("#folio_input").bind("focus", function(event) {
    $(this).autocomplete( "search", "" );
  }).autocomplete({
    minLength: 0,
    source: function(req, add){
      $.ajax({
        url:'/api/parcels/search',
        type:"POST",
        dataType: 'json',
        data: 'type=parcels&folionumber='+folionumber+'&q='+req.term,
        async: true,
        cache: true,
        success: function(data){
          add($.map(data, function(item) {
            return item;
          }));
          load_docs();
        }
      });
    },