我有一个php代码,应该插入到我的数据库中的表中。我尽了一切努力让它工作。
我以前使用过 smiliar 代码并且工作正常。这是代码。
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);
}
这里一定缺少了什么。谁能帮我弄清楚它是什么?谢谢。
你在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)",
....
在 sprintf 中,必须将 ; 替换为 ,
$insertSQL = sprintf("INSERT INTO restaurants (timestamp,
username,
password,)
VALUES (NOW(), %s, %s)",
GetSQLValueString($_POST['username_entry'], "text"),
GetSQLValueString($hashPass, "text"));