2014-03-13 04:07
浏览 51


I'm new to PHP and MySQL and I'm trying to insert data in to a database, which had worked fine until i attempted to use PDO's prepare method. It returns the catch error and i cannot figure out why - i'd also like to know whether it's best practice to use unnnamed placeholders or named placeholders?

The username and password variables are defined earlier in my code and catches data from the user using $_POST.

EDIT: getMessage() error displays SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'username' cannot be null

if (!empty($user) && (!empty($password)) ) {
    try {
        $results = $db->prepare("INSERT INTO user_info (username, pass) VALUES (?,?)");
    } catch (Exception $e) {
        echo "Could not insert data in to the database";

图片转代码服务由CSDN问答提供 功能建议

我是PHP和MySQL的新手,我正在尝试将数据插入数据库, 直到我试图使用PDO的准备方法。 它返回catch错误,我无法弄清楚为什么 - 我还想知道使用未命名的占位符或命名占位符是否是最佳做法?

用户名和密码变量是先前定义的 在我的代码中,使用 $ _ POST 捕获用户的数据。

编辑:getMessage()错误显示 SQLSTATE [23000]:完整性约束违规: 1048列'用户名'不能为空

  if(!empty($ user)&&(!empty($ password))){
 $ results = $ db-> prepare(“INSERT INTO user_info(username,pass)VALUES(?,?)”); 
 $ results-> bindParam(1,$ username); 
 $  results-> bindParam(2,$ password); 
 $ results-> execute(); 
} catch(Exception $ e){
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题