我有一个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);
}
这里一定缺少某些东西。谁能帮我弄清楚这是什么吗?谢谢。
最佳答案
您在','
和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)",
....
关于php - MySQL的插入无法正常工作-无法找到答案,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29843618/