(PHP)什么';这是我的错吗?

(PHP)什么';这是我的错吗?,php,html,Php,Html,表单上的数据未能保存到数据库中。我找不到这里出了什么问题。我已经检查了输入表单的名称,并且都是正确的。我在用PDO if ($_POST) { $accountuname = ($_POST['accountuname']); $accountpassword = ($_POST['accountpassword']); $accounttype = ($_POST['accounttype']); $companyname = ($_POST['companyname']); $company

表单上的数据未能保存到数据库中。我找不到这里出了什么问题。我已经检查了输入表单的名称,并且都是正确的。我在用PDO

if ($_POST) {
$accountuname = ($_POST['accountuname']);
$accountpassword = ($_POST['accountpassword']);
$accounttype = ($_POST['accounttype']);
$companyname = ($_POST['companyname']);
$companyproduct = ($_POST['companyproduct']);
$companyaddress = ($_POST['companyaddress']);
$companycontactnum = ($_POST['companycontactnum']);

$query = "INSERT INTO user_accounts SET USER_NAME=?, USER_PASS=?, USER_ACC_TYPE=?, COMPANY_NAME=?, COMPANY_PRODUCT=?, COMPANY_ADDRESS=?, COMPANY_CONTACTNUM=?";
$stmt  = $conn->prepare($query);
$stmt  -> bindParam(1,$accountuname);
$stmt  -> bindParam(2,$accountpassword);
$stmt  -> bindParam(3,$accounttype);
$stmt  -> bindParam(4,$companyname);
$stmt  -> bindParam(5,$companyproduct);
$stmt  -> bindParam(6,$companyaddress);
$stmt  -> bindParam(7,$companycontactnum);
$stmt  -> execute();
}else{
header("location:index.php");
}

将SQL查询更改为:

INSERT INTO user_accounts SET USER_NAME=?, USER_PASS=?, USER_ACC_TYPE=?, COMPANY_NAME=?, COMPANY_PRODUCT=?, COMPANY_ADDRESS=?, COMPANY_CONTACTNUM=?
致:


如果您使用的是mysqli,根据,bind_param(而不是bindParam…可能您使用的是框架?)函数希望第一个参数是字符串,而不是int:

bind_param ( string $types , mixed &$var1 [, mixed &$... ] )
类型

对于相应的绑定变量:

i对应变量的类型为integer

d对应变量的类型为double

s对应的变量具有类型字符串

b对应的变量是blob,将以数据包的形式发送

您应该更改1,2,3,4。。。到'd,s,b'(变量类型),它应该可以工作


希望有帮助

您必须指定绑定的参数类型,而且您的查询不正确

以下是MySQLi中的正确版本:

$query = "INSERT INTO user_accounts (USER_NAME, USER_PASS, USER_ACC_TYPE, COMPANY_NAME, COMPANY_PRODUCT, COMPANY_ADDRESS, COMPANY_CONTACTNUM) VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt  = $conn->prepare($query);

$stmt->bindParam("sssssss", $accountuname, $accountpassword, $accounttype, $companyname, $companyproduct, $companyaddress, $companycontactnum);

// Set parameters and execute
$accountuname = $_POST['accountuname'];
$accountpassword = $_POST['accountpassword'];
$accounttype = $_POST['accounttype'];
$companyname = $_POST['companyname'];
$companyproduct = $_POST['companyproduct'];
$companyaddress = $_POST['companyaddress'];
$companycontactnum = $_POST['companycontactnum'];
$stmt->execute();
以下是PDO中的正确版本:

$query = "INSERT INTO user_accounts (USER_NAME, USER_PASS, USER_ACC_TYPE, COMPANY_NAME, COMPANY_PRODUCT, COMPANY_ADDRESS, COMPANY_CONTACTNUM) VALUES (:uname, :upass, :utype, :cname, :cproduct, :caddress, :ccontactnum)";
$stmt  = $conn->prepare($query);

$stmt->bindParam(':uname', $accountuname);
$stmt->bindParam(':upass', $accountpassword);
$stmt->bindParam(':utype', $accounttype);
$stmt->bindParam(':cname', $companyname);
$stmt->bindParam(':cproduct', $companyproduct);
$stmt->bindParam(':caddress', $companyaddress);
$stmt->bindParam(':ccontactnum', $companycontactnum);

// Set parameters and execute
$accountuname = $_POST['accountuname'];
$accountpassword = $_POST['accountpassword'];
$accounttype = $_POST['accounttype'];
$companyname = $_POST['companyname'];
$companyproduct = $_POST['companyproduct'];
$companyaddress = $_POST['companyaddress'];
$companycontactnum = $_POST['companycontactnum'];
$stmt->execute();
对于MYSQLi:在本例中,我假设所有发布的数据都是字符串,否则您必须更改bindParam函数中的“sss”

阅读有关准备好的声明的更多信息

阅读更多关于MySQLi插入语法的信息

您是否有任何错误,或者插入的
用户帐户记录是否不是您期望的?您正在使用PDO,对吗?Adam Forbis,是的,看起来Mike Wood的答案是正确的,我想您混淆了插入和更新语法。但您可能会检查是否发生了任何错误—从不存储纯文本密码。您应该使用和。如果您使用的是5.5之前的PHP版本,请不要使用MD5或SHA1散列密码。相反,您可以使用。
$query = "INSERT INTO user_accounts (USER_NAME, USER_PASS, USER_ACC_TYPE, COMPANY_NAME, COMPANY_PRODUCT, COMPANY_ADDRESS, COMPANY_CONTACTNUM) VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt  = $conn->prepare($query);

$stmt->bindParam("sssssss", $accountuname, $accountpassword, $accounttype, $companyname, $companyproduct, $companyaddress, $companycontactnum);

// Set parameters and execute
$accountuname = $_POST['accountuname'];
$accountpassword = $_POST['accountpassword'];
$accounttype = $_POST['accounttype'];
$companyname = $_POST['companyname'];
$companyproduct = $_POST['companyproduct'];
$companyaddress = $_POST['companyaddress'];
$companycontactnum = $_POST['companycontactnum'];
$stmt->execute();
$query = "INSERT INTO user_accounts (USER_NAME, USER_PASS, USER_ACC_TYPE, COMPANY_NAME, COMPANY_PRODUCT, COMPANY_ADDRESS, COMPANY_CONTACTNUM) VALUES (:uname, :upass, :utype, :cname, :cproduct, :caddress, :ccontactnum)";
$stmt  = $conn->prepare($query);

$stmt->bindParam(':uname', $accountuname);
$stmt->bindParam(':upass', $accountpassword);
$stmt->bindParam(':utype', $accounttype);
$stmt->bindParam(':cname', $companyname);
$stmt->bindParam(':cproduct', $companyproduct);
$stmt->bindParam(':caddress', $companyaddress);
$stmt->bindParam(':ccontactnum', $companycontactnum);

// Set parameters and execute
$accountuname = $_POST['accountuname'];
$accountpassword = $_POST['accountpassword'];
$accounttype = $_POST['accounttype'];
$companyname = $_POST['companyname'];
$companyproduct = $_POST['companyproduct'];
$companyaddress = $_POST['companyaddress'];
$companycontactnum = $_POST['companycontactnum'];
$stmt->execute();