php - mySQL 插入不起作用 - 找不到答案

标签 php mysql database

我有一个 php 代码,应该插入到我的数据库中的表中。我已经尝试了一切方法来让它发挥作用。

我之前使用过类似的代码,效果很好。这是代码。

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

require '../scripts/php/db_connect.php';

$password = $_POST['password_entry'];

$hashPass = password_hash($password, PASSWORD_BCRYPT);

if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
        global $link;

        if (PHP_VERSION < 6) {
            $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
        }

        $theValue = mysqli_real_escape_string($link, $theValue);

        switch ($theType) {
            case "text":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "long":
            case "int":
                $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                break;
            case "double":
                $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
                break;
            case "date":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "defined":
                $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                break;
        }
        return $theValue;
    }
}

         $insertSQL = sprintf("INSERT INTO di_ssenisub (timestamp, username, password) VALUES (NOW(), %s, %s)",

        GetSQLValueString($_POST['username_entry'], "text"), 
        GetSQLValueString($hashPass, "text"));

          if (mysqli_query($link, $insertSQL)) {
            echo "New record created successfully $username_entry";

        } else {
            echo "Error: " . $insertSQL . "<br>" . mysqli_error($link);
        }

        mysqli_close($link);


    }  

这里一定有什么东西丢失了。谁能帮我弄清楚它是什么?谢谢。

最佳答案

您在 sprintf 内的密码;后面添加了',':

$insertSQL = sprintf("INSERT INTO restaurants (timestamp,
     username,
     password,)

    VALUES (NOW(), %s, %s)";

正确的方法应该是:

$insertSQL = sprintf("INSERT INTO restaurants (timestamp,
     username,
     password)

    VALUES (NOW(), %s, %s)",
....

关于php - mySQL 插入不起作用 - 找不到答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29843618/

相关文章:

sql - 相同的查询给出不同的结果

php - 使用 Magento 2.3.3 p1 后遇到的问题

php - 连接到 ACCESS 2007 (ODBC) 时如何在 PHP 上设置 UTF8

php - MySQL计算出多个列中最低的一个

php - INNER JOIN 停止查询工作

mysql - 以下哪个查询被优化/首选

database - Date 类型和 Android 数据库

php - 在创建动态菜单时需要帮助

php - 如何在 php form_submit 按钮中添加样式或放置类

php - 选择用户喜欢的推文中的所有喜欢