This is the table where I´m trying to the the insert.
When i try to make a insert using pdo I get the following error:
Array ( [0] => HY093 [1] => [2] => )
All of the information comes from a html form.
The connection to the db
is working because before I do this insert I do a fetch so that's not the problem.
I already checked all the variables with echo and they are correct.
Also tried to add the column 'id' to the sql , and give it the value NULL
, but the error is the same as the above. But since the column ' id
' is auto incremented i didn't put it in the sql. See my code above to understand what I did.
$nome = $_POST['name'];
$mail = $_POST['email'];
$psw = $_POST['pass'];
$ni = $_POST['nif'];
This two variables comes from a fetch, and it works. They are only here because the belong to the Insert statement.
$roleid = $row['id'];
$rolen = $row['nome'];
$sql = "INSERT INTO users ( nome, email, psw, nif, role_id, role_name)
VALUES ( :nome, :mail, :psw, :ni, :roleid, :rolen)";
$stmt = $db1->prepare($sql);
$stmt->bindValue('nome', $nome, PDO::PARAM_STR);
$stmt->bindValue('email', $mail, PDO::PARAM_STR);
$stmt->bindValue('psw', $psw, PDO::PARAM_STR);
$stmt->bindValue('nif', $ni, PDO::PARAM_INT);
$stmt->bindValue('role_id', $roleid, PDO::PARAM_INT);
$stmt->bindValue('role_name', $rolen, PDO::PARAM_STR);
$stmt->execute();
$error = $stmt->errorInfo();
print_r($error);
Executing this insert I get the following error:
Array ( [0] => HY093 [1] => [2] => )
This is the solution to the question, I wasn't using the same name in the bindValue()
function
$name = $_POST['name'];
$email = $_POST['email'];
$psw = $_POST['psw'];
$nif = $_POST['nif'];
$roleid = $row['id'];
$rolen = $row['nome'];
$sql = "INSERT INTO users ( nome, email, psw, nif, role_id, role_name)
VALUES ( :name, :email, :psw, :nif, :roleid, :rolen)";
$stmt = $db1->prepare($sql);
$stmt->bindValue(':name', $name, PDO::PARAM_STR);
$stmt->bindValue(':email', $email, PDO::PARAM_STR);
$stmt->bindValue(':psw', $psw, PDO::PARAM_STR);
$stmt->bindValue(':nif', $nif, PDO::PARAM_INT);
$stmt->bindValue(':roleid', $roleid, PDO::PARAM_INT);
$stmt->bindValue(':rolen', $rolen, PDO::PARAM_STR);
$stmt->execute();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.