register.php
<?php
include_once "pdo.php";
if(isset($_POST["submit"])){
$user_pw = hash("sha256", $_POST['password']);
$params = [
'pcode' => $pcode,
'password' => $user_pw,
'name' => $_POST['name'],
'phone' => $_POST['number'],
'grade' => $_POST['grade']
];
sql($db, "INSERT INTO member (pcode, password, name, phone, grade) VALUES (:pcode, :password, :name, :phone, :grade)", array($params));
}
?>
pdo.php
try {
$db = new PDO("mysql:host=".HOST.";dbname=".NAME.";charset=utf8", "".USER."", "".PASS."");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// PDO fetch docs: http://php.net/manual/en/pdostatement.fetch.php
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo $e->getMessage();
}
// Simple function to handle PDO prepared statements
function sql($db, $q, $params, $return) {
// Prepare statement
$stmt = $db->prepare($q);
// Execute statement
$stmt->execute($params);
// Decide whether to return the rows themselves, or just count the rows
if ($return == "rows") {
return $stmt->fetchAll();
}
elseif ($return == "count") {
return $stmt->rowCount();
}
错误
警告:缺少sql()的参数4,该参数在第55行的/home/vvvvvvv/html/summit/new.php中调用,并在第19行的/home/vvvvvv/html/summit/pdo.php中定义>
致命错误:未捕获的PDOException:SQLSTATE [HY093]:无效的参数编号:绑定变量的数量与/home/vvvvvv/html/summit/pdo.php中的令牌数量不匹配:24堆栈跟踪:#0 / home /vvvvvv/html/summit/pdo.php(24):PDOStatement-> execute(Array)#1 /home/vvvvvv/html/summit/new.php(55):sql(Object(PDO),'INSERT INTO mem ...',数组)#2 {main}在第24行的/home/vvvvvv/html/summit/pdo.php中抛出
出什么问题了?
答案 0 :(得分:1)
当您创建数据时,您的参数已经是一个数组,因此当您将其作为array($params)
传递时,它将嵌套数据。因此,只需在通话中将其删除...
sql($db, "INSERT INTO member (pcode, password, name, phone, grade)
VALUES (:pcode, :password, :name, :phone, :grade)",
$params, "rows" );
不确定最后一个参数是什么,但是我建议将其更改为其他参数。具有“行”或“计数”之类的文字可能容易出错。将其更改为布尔值-类似于true
表示返回行,false
表示计数。