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

时间:2015-04-24 09:24:37

标签: 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);


    }  

这里肯定有一些东西不见了。任何人都可以帮我弄清楚它是什么?感谢。

2 个答案:

答案 0 :(得分:1)

您在','password;之后加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)",
....

答案 1 :(得分:0)

在sprintf中,不得不更换;通过,

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

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

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